📜  与M配对时,使用GCD直到M的数字计数等于K

📅  最后修改于: 2021-04-22 06:49:56             🧑  作者: Mango

给定两个整数MK ,任务是计算[0,M]之间的整数数,以使该整数中M的GCD等于K。

例子:

方法:

  • GCD为K且M为M的整数的形式为K,2K,3K等,依此类推,直到M。
  • 让我们考虑K的系数,即1、2、3、4…直到(M / K)。
  • 现在我们只需要找到GCD的(M / K)= 1的系数的数量即可。所以现在问题减少了,找到了Gcd为(m / k)的1到(M / K)之间的整数。 )= 1。
  • 为了找到这个,我们将使用(M / K)的Euler totient函数。

下面是上述方法的实现:

C++
// C++ program to Count of numbers
// between 0 to M which have GCD
// with M equals to K.
  
#include 
using namespace std;
  
// Function to calculate GCD
// using euler totient function
int EulerTotientFunction(int limit)
{
    int copy = limit;
  
    // Finding the prime factors of
    // limit to calculate it's
    // euler totient function
    vector primes;
  
    for (int i = 2; i * i <= limit; i++) {
        if (limit % i == 0) {
            while (limit % i == 0) {
                limit /= i;
            }
            primes.push_back(i);
        }
    }
    if (limit >= 2) {
        primes.push_back(limit);
    }
  
    // Calculating the euler totien
    // function of (m/k)
    int ans = copy;
    for (auto it : primes) {
        ans = (ans / it) * (it - 1);
    }
    return ans;
}
  
// Function print the count of
// numbers whose GCD with M
// equals to K
void CountGCD(int m, int k)
{
  
    if (m % k != 0) {
        // GCD of m with any integer
        // cannot  be equal to k
        cout << 0 << endl;
        return;
    }
  
    if (m == k) {
        // 0 and m itself will be
        // the only valid integers
        cout << 2 << endl;
        return;
    }
  
    // Finding the number upto which
    // coefficient of k can come
    int limit = m / k;
  
    int ans = EulerTotientFunction(limit);
  
    cout << ans << endl;
}
// Driver code
int main()
{
  
    int M = 9;
    int K = 1;
    CountGCD(M, K);
  
    return 0;
}


Java
// Java program to Count of numbers
// between 0 to M which have GCD
// with M equals to K.
import java.util.*;
  
class GFG{
   
// Function to calculate GCD
// using euler totient function
static int EulerTotientFunction(int limit)
{
    int copy = limit;
   
    // Finding the prime factors of
    // limit to calculate it's
    // euler totient function
    Vector primes = new Vector();
   
    for (int i = 2; i * i <= limit; i++) {
        if (limit % i == 0) {
            while (limit % i == 0) {
                limit /= i;
            }
            primes.add(i);
        }
    }
    if (limit >= 2) {
        primes.add(limit);
    }
   
    // Calculating the euler totien
    // function of (m/k)
    int ans = copy;
    for (int it : primes) {
        ans = (ans / it) * (it - 1);
    }
    return ans;
}
   
// Function print the count of
// numbers whose GCD with M
// equals to K
static void CountGCD(int m, int k)
{
   
    if (m % k != 0) {
        // GCD of m with any integer
        // cannot  be equal to k
        System.out.print(0 +"\n");
        return;
    }
   
    if (m == k) {
        // 0 and m itself will be
        // the only valid integers
        System.out.print(2 +"\n");
        return;
    }
   
    // Finding the number upto which
    // coefficient of k can come
    int limit = m / k;
   
    int ans = EulerTotientFunction(limit);
   
    System.out.print(ans +"\n");
}
  
// Driver code
public static void main(String[] args)
{
   
    int M = 9;
    int K = 1;
    CountGCD(M, K);
   
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program to Count of numbers
# between 0 to M which have GCD
# with M equals to K.
  
# Function to calculate GCD
# using euler totient function
def EulerTotientFunction(limit):
    copy = limit
  
    # Finding the prime factors of
    # limit to calculate it's
    # euler totient function
    primes = []
  
    for i in range(2, limit + 1):
        if i * i > limit:
            break
        if (limit % i == 0):
            while (limit % i == 0):
                limit //= i
            primes.append(i)
  
    if (limit >= 2):
        primes.append(limit)
  
    # Calculating the euler totien
    # function of (m//k)
    ans = copy
    for it in primes:
        ans = (ans // it) * (it - 1)
  
    return ans
  
# Function print the count of
# numbers whose GCD with M
# equals to K
def CountGCD(m, k):
  
    if (m % k != 0):
          
        # GCD of m with any integer
        # cannot be equal to k
        print(0)
        return
  
    if (m == k):
          
        # 0 and m itself will be
        # the only valid integers
        print(2)
        return
  
    # Finding the number upto which
    # coefficient of k can come
    limit = m // k
  
    ans = EulerTotientFunction(limit)
  
    print(ans)
  
# Driver code
if __name__ == '__main__':
  
    M = 9
    K = 1
    CountGCD(M, K)
  
# This code is contributed by mohit kumar 29


C#
// C# program to Count of numbers
// between 0 to M which have GCD
// with M equals to K.
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to calculate GCD
// using euler totient function
static int EulerTotientFunction(int limit)
{
    int copy = limit;
  
    // Finding the prime factors of
    // limit to calculate it's
    // euler totient function
    List primes = new List();
  
    for (int i = 2; i * i <= limit; i++) 
    {
        if (limit % i == 0) 
        {
            while (limit % i == 0) 
            {
                limit /= i;
            }
            primes.Add(i);
        }
    }
    if (limit >= 2) 
    {
        primes.Add(limit);
    }
  
    // Calculating the euler totien
    // function of (m/k)
    int ans = copy;
    foreach (int it in primes) 
    {
        ans = (ans / it) * (it - 1);
    }
    return ans;
}
  
// Function print the count of
// numbers whose GCD with M
// equals to K
static void CountGCD(int m, int k)
{
    if (m % k != 0) 
    {
        // GCD of m with any integer
        // cannot be equal to k
        Console.Write(0 + "\n");
        return;
    }
  
    if (m == k) 
    {
        // 0 and m itself will be
        // the only valid integers
        Console.Write(2 + "\n");
        return;
    }
  
    // Finding the number upto which
    // coefficient of k can come
    int limit = m / k;
  
    int ans = EulerTotientFunction(limit);
  
    Console.Write(ans + "\n");
}
  
// Driver code
public static void Main(String[] args)
{
    int M = 9;
    int K = 1;
    CountGCD(M, K);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
6