📌  相关文章
📜  任意两个数与第三个数的乘积之和等于 N 的三元组数

📅  最后修改于: 2022-05-13 01:56:07.249000             🧑  作者: Mango

任意两个数与第三个数的乘积之和等于 N 的三元组数

给定一个正整数N ,任务是找到三元组(X, Y, Z)的数量,使得任意两个数与第三个数的乘积之和为N。

例子:

方法:这个给定的问题可以通过重新排列方程来解决:

从上面的等式中,想法是在[1, N]范围内迭代Z的所有可能值,并通过计算满足上述等式的 (N - Z) 的素数因子来添加所有可能的XY的计数。请按照以下步骤解决问题:

  • 初始化一个变量countTriplets以存储满足给定条件的三元组的结果计数。
  • 使用埃拉托色尼筛法为[1, 10 5 ]范围内的所有元素找到最小的素因子。
  • 使用变量K迭代范围[1, N]并执行以下步骤:
    • 使用本文讨论的方法找到乘积为(N – K)的对数,并将获得的计数添加到变量countTriplet中。
  • 完成上述步骤后,打印countTriplets的值作为三元组的结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
vector s(11,0);
 
// Function to find the SPF[i] using the
// Sieve Of Eratosthenes
void sieveOfEratosthenes(int N){
      
    // Stores whether i is prime or not
    bool prime[N+1];
    memset(prime,false,sizeof(false));
     
    // Initializing smallest factor as
    // 2 for all even numbers
    for (int i=2;i< N + 1; i+=2)
        s[i] = 2;
     
    // Iterate for all odd numbers < N
    for(int i =3;i cnt;
    cnt[s[N]] = 1;
     
    // Find all the prime factors and
    // their powers
    while (N > 1){
        N /= s[N];
        if (N and s[N])
            if(cnt.find(s[N]) == cnt.end())
                cnt[s[N]] = 1;
            else
                cnt[s[N]] += 1;
 
    }
     
    if (cnt.find(0) != cnt.end())
        cnt.erase(0);
         
    int totfactor = 1;
     
    for (auto i: cnt)
        totfactor *= i.second + 1;
     
    // Return the total count of factors
    return totfactor; 
}
     
     
 
// Function to count the number of triplets
// satisfying the given criteria
int countTriplets(int N){
   
    // Stores the count of resultant triplets
    int CountTriplet = 0;
   
    for (int z=1;z 1)
            CountTriplet += p;
    }
     
    // Return total count of triplets
    return CountTriplet + 1;  
   
  }
 
// Driver Code
int main(){
 
int N = 10;
 
// S[i] stores the smallest prime factor
// for each element i
 
 
// Find the SPF[i]
sieveOfEratosthenes(N);
 
// Function Call
cout<


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
static int[] s = new int[11];
 
// Function to find the SPF[i] using the
// Sieve Of Eratosthenes
static void sieveOfEratosthenes(int N){
      
    // Stores whether i is prime or not
    boolean []prime = new boolean[N+1];
 
     
    // Initializing smallest factor as
    // 2 for all even numbers
    for (int i = 2; i < N + 1; i += 2)
        s[i] = 2;
     
    // Iterate for all odd numbers < N
    for(int i = 3; i < N + 1; i += 2){
        if (prime[i] == false){
              
            // SPF of i for a prime is
            // the number itself
            s[i] = i;
     
            // Iterate for all the multiples
            // of the current prime number
            for(int j= i; j < N / i + 1; j += 2){
                if (prime[i * j] == false){
                    prime[i * j] = true;
                     
                    // The value i is smallest
                    // prime factor for i * j
                    s[i * j] = i;
                }
            }
        }
    }
}
 
// Function to generate prime factors
// and its power
static int generatePrimeFactors(int N){
   // Current prime factor of N
    int curr = s[N];
      
    // Stores the powers of the current
    // prime factor
    HashMap cnt = new HashMap<>();
    cnt.put(s[N],1);
     
    // Find all the prime factors and
    // their powers
    while (N > 1){
        N /= s[N];
        if (N != 0 && s[N] != 0)
            if(!cnt.containsKey(s[N]))
                cnt.put(s[N], 1);
            else
                cnt.put(s[N], cnt.get(s[N]) + 1);
 
    }
     
    if (cnt.containsKey(0))
        cnt.remove(0);
         
    int totfactor = 1;
     
    
        for (Map.Entry i : cnt.entrySet())
        totfactor *= i.getValue() + 1;
     
    // Return the total count of factors
    return totfactor; 
}
     
     
 
// Function to count the number of triplets
// satisfying the given criteria
static int countTriplets(int N){
   
    // Stores the count of resultant triplets
    int CountTriplet = 0;
   
    for (int z=1;z 1)
            CountTriplet += p;
    }
     
    // Return total count of triplets
    return CountTriplet + 1;  
   
  }
 
// Driver Code
public static void main(String[] args){
 
int N = 10;
 
// S[i] stores the smallest prime factor
// for each element i
 
 
// Find the SPF[i]
sieveOfEratosthenes(N);
 
// Function Call
System.out.print(countTriplets(N));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python program for the above approach
 
# Function to find the SPF[i] using the
# Sieve Of Eratosthenes
def sieveOfEratosthenes(N, s):
      
    # Stores whether i is prime or not
    prime = [False] * (N + 1)
     
    # Initializing smallest factor as
    # 2 for all even numbers
    for i in range(2, N + 1, 2):
        s[i] = 2
     
    # Iterate for all odd numbers < N
    for i in range(3, N + 1, 2):
        if (prime[i] == False):
              
            # SPF of i for a prime is
            # the number itself
            s[i] = i
     
            # Iterate for all the multiples
            # of the current prime number
            for j in range(i, int(N / i) + 1, 2):
                if (prime[i * j] == False):
                    prime[i * j] = True
                     
                    # The value i is smallest
                    # prime factor for i * j
                    s[i * j] = i
 
# Function to generate prime factors
# and its power
def generatePrimeFactors(N):
     
    # Current prime factor of N
    curr = s[N]
      
    # Stores the powers of the current
    # prime factor
    cnt = {s[N]:1}
     
    # Find all the prime factors and
    # their powers
    while (N > 1):
     
        N //= s[N]
        if N and s[N]:
            if cnt.get(s[N], 0) == 0:
                cnt[s[N]] = 1
            else:
                cnt[s[N]] += 1
     
    if 0 in cnt:
        cnt.pop(0)
         
    totfactor = 1
     
    for i in cnt.values():
        totfactor *= i + 1
     
    # Return the total count of factors
    return totfactor 
 
# Function to count the number of triplets
# satisfying the given criteria
def countTriplets(N):
   
    # Stores the count of resultant triplets
    CountTriplet = 0
   
    for z in range(1, N + 1):
         
        # Add the count all factors of N-z
        # to the variable CountTriplet
        p = generatePrimeFactors(N-z)
        if p > 1:
            CountTriplet += p 
     
    # Return total count of triplets
    return CountTriplet + 1   
   
 
# Driver Code
N = 10
 
# S[i] stores the smallest prime factor
# for each element i
s = [0] * (N + 1)
 
# Find the SPF[i]
sieveOfEratosthenes(N, s)
 
# Function Call
print(countTriplets(N))


Javascript


输出:
23

时间复杂度: O(N*log N)
辅助空间: O(N)