📌  相关文章
📜  检查在给定范围内是否有任何GCD对可以被k整除

📅  最后修改于: 2021-04-26 18:37:16             🧑  作者: Mango

给定一个范围,我们需要检查GCD可被k整除的段中的任何对。
例子:

Input : l=4, r=6, k=2
Output : YES
There are two numbers 4 and 6 whose GCD is 2 which is divisible by 2.

Input : l=3 r=5 k=4
Output : NO
Their is no such pair whose gcd is divisible by 5.

基本上,我们需要对范围为l到r的数字进行计数,以使它们可以被k整除。因为如果我们选择任意两个数字,那么它们的gcd也是k的倍数。现在,如果这些数字的数量大于一个,那么我们就可以形成一对,否则就不可能形成一个对(x,y)以便gcd(x,y)被k整除。
下面是上述方法的实现:

C++
#include 
using namespace std;
 
// function to count such possible numbers
bool Check_is_possible(int l, int r, int k)
{
    int count = 0;
 
    for (int i = l; i <= r; i++) {
 
        // if i is divisible by k
        if (i % k == 0)
            count++;
    }
 
    // if count of such numbers
    // is greater than one
    return (count > 1);
}
 
// Driver code
int main()
{
    int l = 4, r = 12;
    int k = 5;
 
    if (Check_is_possible(l, r, k))
        cout << "YES\n";
    else
        cout << "NO\n";
    return 0;
}


Java
// function to count such
// possible numbers
 
class GFG {
 
    public boolean Check_is_possible(int l, int r,
            int k) {
        int count = 0;
 
        for (int i = l; i <= r; i++) {
 
            // if i is divisible by k
            if (i % k == 0) {
                count++;
            }
        }
 
        // if count of such numbers
        // is greater than one
        return (count > 1);
    }
 
    public static void main(String[] args) {
        GFG g = new GFG();
        int l = 4, r = 12;
        int k = 5;
 
        if (g.Check_is_possible(l, r, k)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
 
    }
}
// This code is contributed by RAJPUT-JI


Python3
# function to count such possible numbers
def Check_is_possible(l, r, k):
 
    count = 0;
 
    for i in range(l, r + 1):
 
        # if i is divisible by k
        if (i % k == 0):
            count += 1;
 
    # if count of such numbers
    # is greater than one
    return (count > 1);
 
# Driver code
l = 4;
r = 12;
k = 5;
if (Check_is_possible(l, r, k)):
    print("YES");
else:
    print("NO");
 
# This code is contributed by mits


C#
using System;
 
// function to count such
// possible numbers
class GFG
{
public bool Check_is_possible(int l, int r,
                              int k)
{
    int count = 0;
 
    for (int i = l; i <= r; i++)
    {
 
        // if i is divisible by k
        if (i % k == 0)
            count++;
    }
 
    // if count of such numbers
    // is greater than one
    return (count > 1);
}
 
// Driver code
public static void Main()
{
    GFG g = new GFG();
    int l = 4, r = 12;
    int k = 5;
 
    if (g.Check_is_possible(l, r, k))
        Console.WriteLine("YES\n");
    else
    Console.WriteLine("NO\n");
}
}
 
// This code is contributed
// by Soumik


PHP
 1);
}
 
// Driver code
$l = 4; $r = 12;
$k = 5;
 
if (Check_is_possible($l, $r, $k))
    echo "YES\n";
else
    echo "NO\n";
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


C++
// C++ program to count the numbers divisible
// by k in a given range
#include 
using namespace std;
 
// Returns count of numbers in [l r] that
// are divisible by k.
int Check_is_possible(int l, int r, int k)
{
    int div_count = (r / k) - (l / k);
 
    // Add 1 explicitly as l is divisible by k
    if (l % k == 0)
        div_count++;
 
    // l is not divisible by k
    return (div_count > 1);
}
 
// Driver Code
int main()
{
    int l = 30, r = 70, k = 10;
 
    if (Check_is_possible(l, r, k))
        cout << "YES\n";
    else
        cout << "NO\n";
    return 0;
}


Java
// Java program to count the numbers divisible
// by k in a given range
 
class GFG {
 
// Returns count of numbers in [l r] that
// are divisible by k.
    static boolean Check_is_possible(int l, int r, int k) {
        int div_count = (r / k) - (l / k);
 
        // Add 1 explicitly as l is divisible by k
        if (l % k == 0) {
            div_count++;
        }
 
        // l is not divisible by k
        return (div_count > 1);
    }
 
// Driver Code
    public static void main(String[] args) {
        int l = 30, r = 70, k = 10;
        if (Check_is_possible(l, r, k)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
 
    }
}
// This code is contributed by RAJPUT-JI


Python3
# Python3 program to count the numbers
# divisible by k in a given range
 
# Returns count of numbers in [l r]
# that are divisible by k.
def Check_is_possible(l, r, k):
 
    div_count = (r // k) - (l // k)
 
    # Add 1 explicitly as l is
    # divisible by k
    if l % k == 0:
        div_count += 1
 
    # l is not divisible by k
    return div_count > 1
 
# Driver Code
if __name__ == "__main__":
 
    l, r, k = 30, 70, 10
 
    if Check_is_possible(l, r, k) == True:
        print("YES")
    else:
        print("NO")
     
# This code is contributed
# by Rituraj Jain


C#
// C# program to count the numbers divisible
// by k in a given range
using System;
  
 
public class GFG {
 
// Returns count of numbers in [l r] that
// are divisible by k.
    static bool Check_is_possible(int l, int r, int k) {
        int div_count = (r / k) - (l / k);
 
        // Add 1 explicitly as l is divisible by k
        if (l % k == 0) {
            div_count++;
        }
 
        // l is not divisible by k
        return (div_count > 1);
    }
 
// Driver Code
    public static void Main() {
        int l = 30, r = 70, k = 10;
        if (Check_is_possible(l, r, k)) {
            Console.WriteLine("YES");
        } else {
            Console.WriteLine("NO");
        }
 
    }
}
// This code is contributed by RAJPUT-JI


PHP
 1);
}
 
// Driver Code
$l = 30;
$r = 70;
$k = 10;
 
if (Check_is_possible($l, $r, $k))
    echo "YES\n";
else
    echo "NO\n";
 
// This Code is contributed by mits
?>


Javascript


输出:
YES

时间复杂度:O(r – l + 1)
一个有效的解决方案基于此处讨论的有效方法。

C++

// C++ program to count the numbers divisible
// by k in a given range
#include 
using namespace std;
 
// Returns count of numbers in [l r] that
// are divisible by k.
int Check_is_possible(int l, int r, int k)
{
    int div_count = (r / k) - (l / k);
 
    // Add 1 explicitly as l is divisible by k
    if (l % k == 0)
        div_count++;
 
    // l is not divisible by k
    return (div_count > 1);
}
 
// Driver Code
int main()
{
    int l = 30, r = 70, k = 10;
 
    if (Check_is_possible(l, r, k))
        cout << "YES\n";
    else
        cout << "NO\n";
    return 0;
}

Java

// Java program to count the numbers divisible
// by k in a given range
 
class GFG {
 
// Returns count of numbers in [l r] that
// are divisible by k.
    static boolean Check_is_possible(int l, int r, int k) {
        int div_count = (r / k) - (l / k);
 
        // Add 1 explicitly as l is divisible by k
        if (l % k == 0) {
            div_count++;
        }
 
        // l is not divisible by k
        return (div_count > 1);
    }
 
// Driver Code
    public static void main(String[] args) {
        int l = 30, r = 70, k = 10;
        if (Check_is_possible(l, r, k)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
 
    }
}
// This code is contributed by RAJPUT-JI

Python3

# Python3 program to count the numbers
# divisible by k in a given range
 
# Returns count of numbers in [l r]
# that are divisible by k.
def Check_is_possible(l, r, k):
 
    div_count = (r // k) - (l // k)
 
    # Add 1 explicitly as l is
    # divisible by k
    if l % k == 0:
        div_count += 1
 
    # l is not divisible by k
    return div_count > 1
 
# Driver Code
if __name__ == "__main__":
 
    l, r, k = 30, 70, 10
 
    if Check_is_possible(l, r, k) == True:
        print("YES")
    else:
        print("NO")
     
# This code is contributed
# by Rituraj Jain

C#

// C# program to count the numbers divisible
// by k in a given range
using System;
  
 
public class GFG {
 
// Returns count of numbers in [l r] that
// are divisible by k.
    static bool Check_is_possible(int l, int r, int k) {
        int div_count = (r / k) - (l / k);
 
        // Add 1 explicitly as l is divisible by k
        if (l % k == 0) {
            div_count++;
        }
 
        // l is not divisible by k
        return (div_count > 1);
    }
 
// Driver Code
    public static void Main() {
        int l = 30, r = 70, k = 10;
        if (Check_is_possible(l, r, k)) {
            Console.WriteLine("YES");
        } else {
            Console.WriteLine("NO");
        }
 
    }
}
// This code is contributed by RAJPUT-JI

的PHP

 1);
}
 
// Driver Code
$l = 30;
$r = 70;
$k = 10;
 
if (Check_is_possible($l, $r, $k))
    echo "YES\n";
else
    echo "NO\n";
 
// This Code is contributed by mits
?>

Java脚本


输出:
YES