📜  GCD等于K的四倍数

📅  最后修改于: 2021-05-06 21:46:59             🧑  作者: Mango

给定两个整数NK ,任务是找到(i,j,k,l)的四倍数,使得1≤i l≤Ngcd(i,j,k,l) = K。

例子:

方法:

  • 如果序列的gcd为K,那么当我们将所有这些数字除以K时,剩余数字的gcd将为1
  • 现在,为了满足最大数量为N的四足动物的约束,如果我们找出最大数量小于或等于N / K且gcd为1的所有四足动物的计数,则可以简单地将所有四足动物与K相乘得到答案。
  • 要使用gcd 1查找四倍计数,必须使用包含和排除原理。取N / K = M。
  • M C 4总数可能是四倍。 (M / 2) C 4个四联体的gcd是2的倍数。 (使用2的M / 2倍数)。类似地, (M / 3) C 4个四倍体的gcd是3的倍数。但是,如果我们同时减去两个数量,则gcd是6的倍数被减去两次,因此我们必须包括(M / 6) C 4才能将它们相加一次。
  • 因此,从2迭代到M ,如果一个数字的奇数除数的奇数(例如2,3、5、11),则减去gcd倍数该数的四倍数,如果它的奇数除数为偶数除数(例如6、10、33),然后将四倍数加gcd为该数的四倍。 (Number不能重复像4这样的素数除数)。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to calculate NC4
int nCr(int n)
{
    // Base case to calculate NC4
    if (n < 4)
        return 0;
  
    int answer = n * (n - 1) * (n - 2) * (n - 3);
    answer /= 24;
    return answer;
}
  
// Function to return the count of required
// quadruples using Inclusion Exclusion
int countQuadruples(int N, int K)
{
    // Effective N
    int M = N / K;
  
    int answer = nCr(M);
  
    // Iterate over 2 to M
    for (int i = 2; i < M; i++) {
        int j = i;
  
        // Number of divisors of i till M
        int temp2 = M / i;
  
        // Count stores the number of prime
        // divisors occurring exactly once
        int count = 0;
  
        // To prevent repetition of prime divisors
        int check = 0;
        int temp = j;
  
        while (j % 2 == 0) {
            count++;
            j /= 2;
            if (count >= 2)
                break;
        }
  
        if (count >= 2) {
            check = 1;
        }
  
        for (int k = 3; k <= sqrt(temp); k += 2) {
            int cnt = 0;
  
            while (j % k == 0) {
                cnt++;
                j /= k;
                if (cnt >= 2)
                    break;
            }
  
            if (cnt >= 2) {
                check = 1;
                break;
            }
            else if (cnt == 1)
                count++;
        }
  
        if (j > 2) {
            count++;
        }
  
        // If repetition of prime divisors present
        // ignore this number
        if (check)
            continue;
        else {
  
            // If prime divisor count is odd
            // subtract it from answer else add
            if (count % 2 == 1) {
                answer -= nCr(temp2);
            }
            else {
                answer += nCr(temp2);
            }
        }
    }
  
    return answer;
}
  
// Driver code
int main()
{
    int N = 10, K = 2;
  
    cout << countQuadruples(N, K);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to calculate NC4
static int nCr(int n)
{
    // Base case to calculate NC4
    if (n < 4)
        return 0;
  
    int answer = n * (n - 1) * (n - 2) * (n - 3);
    answer /= 24;
    return answer;
}
  
// Function to return the count of required
// quadruples using Inclusion Exclusion
static int countQuadruples(int N, int K)
{
    // Effective N
    int M = N / K;
  
    int answer = nCr(M);
  
    // Iterate over 2 to M
    for (int i = 2; i < M; i++) 
    {
        int j = i;
  
        // Number of divisors of i till M
        int temp2 = M / i;
  
        // Count stores the number of prime
        // divisors occurring exactly once
        int count = 0;
  
        // To prevent repetition of prime divisors
        int check = 0;
        int temp = j;
  
        while (j % 2 == 0) 
        {
            count++;
            j /= 2;
            if (count >= 2)
                break;
        }
  
        if (count >= 2) 
        {
            check = 1;
        }
  
        for (int k = 3; k <= Math.sqrt(temp); k += 2) 
        {
            int cnt = 0;
  
            while (j % k == 0)
            {
                cnt++;
                j /= k;
                if (cnt >= 2)
                    break;
            }
  
            if (cnt >= 2)
            {
                check = 1;
                break;
            }
            else if (cnt == 1)
                count++;
        }
  
        if (j > 2) 
        {
            count++;
        }
  
        // If repetition of prime divisors present
        // ignore this number
        if (check==1)
            continue;
        else
        {
  
            // If prime divisor count is odd
            // subtract it from answer else add
            if (count % 2 == 1)
            {
                answer -= nCr(temp2);
            }
            else
            {
                answer += nCr(temp2);
            }
        }
    }
  
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
  
    int N = 10, K = 2;
  
    System.out.println(countQuadruples(N, K));
}
}
  
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach 
from math import sqrt
  
# Function to calculate NC4 
def nCr(n) : 
  
    # Base case to calculate NC4 
    if (n < 4) :
        return 0; 
  
    answer = n * (n - 1) * (n - 2) * (n - 3); 
    answer //= 24; 
      
    return answer; 
  
# Function to return the count of required 
# quadruples using Inclusion Exclusion 
def countQuadruples(N, K) :
  
    # Effective N 
    M = N // K; 
  
    answer = nCr(M); 
  
    # Iterate over 2 to M 
    for i in range(2, M) :
        j = i; 
  
        # Number of divisors of i till M 
        temp2 = M // i; 
  
        # Count stores the number of prime 
        # divisors occurring exactly once 
        count = 0; 
  
        # To prevent repetition of prime divisors 
        check = 0; 
        temp = j; 
  
        while (j % 2 == 0) :
            count += 1; 
            j //= 2; 
            if (count >= 2) :
                break; 
  
        if (count >= 2) :
            check = 1;
  
        for k in range(3, int(sqrt(temp)), 2) :
            cnt = 0; 
  
            while (j % k == 0) :
                cnt += 1; 
                j //= k; 
                if (cnt >= 2) :
                    break; 
      
            if (cnt >= 2) :
                check = 1; 
                break; 
            elif (cnt == 1) :
                count += 1;
  
        if (j > 2) :
            count += 1; 
  
        # If repetition of prime divisors present 
        # ignore this number 
        if (check) :
            continue; 
        else : 
              
            # If prime divisor count is odd 
            # subtract it from answer else add 
            if (count % 2 == 1) :
                answer -= nCr(temp2); 
            else :
                answer += nCr(temp2); 
  
    return answer; 
  
# Driver code 
if __name__ == "__main__" : 
  
    N = 10; K = 2; 
  
    print(countQuadruples(N, K)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG 
{
  
// Function to calculate NC4
static int nCr(int n)
{
    // Base case to calculate NC4
    if (n < 4)
        return 0;
  
    int answer = n * (n - 1) * (n - 2) * (n - 3);
    answer /= 24;
    return answer;
}
  
// Function to return the count of required
// quadruples using Inclusion Exclusion
static int countQuadruples(int N, int K)
{
    // Effective N
    int M = N / K;
  
    int answer = nCr(M);
  
    // Iterate over 2 to M
    for (int i = 2; i < M; i++) 
    {
        int j = i;
  
        // Number of divisors of i till M
        int temp2 = M / i;
  
        // Count stores the number of prime
        // divisors occurring exactly once
        int count = 0;
  
        // To prevent repetition of prime divisors
        int check = 0;
        int temp = j;
  
        while (j % 2 == 0) 
        {
            count++;
            j /= 2;
            if (count >= 2)
                break;
        }
  
        if (count >= 2) 
        {
            check = 1;
        }
  
        for (int k = 3; k <= Math.Sqrt(temp); k += 2) 
        {
            int cnt = 0;
  
            while (j % k == 0)
            {
                cnt++;
                j /= k;
                if (cnt >= 2)
                    break;
            }
  
            if (cnt >= 2)
            {
                check = 1;
                break;
            }
            else if (cnt == 1)
                count++;
        }
  
        if (j > 2) 
        {
            count++;
        }
  
        // If repetition of prime divisors present
        // ignore this number
        if (check==1)
            continue;
        else
        {
  
            // If prime divisor count is odd
            // subtract it from answer else add
            if (count % 2 == 1)
            {
                answer -= nCr(temp2);
            }
            else
            {
                answer += nCr(temp2);
            }
        }
    }
  
    return answer;
}
  
// Driver code
public static void Main(String[] args)
{
  
    int N = 10, K = 2;
  
    Console.WriteLine(countQuadruples(N, K));
}
}
  
// This code is contributed by 29AjayKumar


输出:
5