📜  最多 N 对的计数,其 LCM 不等于 Q 查询的乘积

📅  最后修改于: 2021-09-03 13:38:48             🧑  作者: Mango

给定一个数字N ,任务是在[1, N]范围内找到(a, b)对的数量,使得它们的 LCM 不等于它们的乘积,即LCM(a, b) != (a* b)(b > a) 。可以有多个查询需要回答。

例子:

方法:想法是使用 Euler 的 Totient函数。

1. 找出可以使用从 1 到 N 之间的数字形成的对的总数。形成的对的数量等于 ( N * (N – 1)) / 2

2. 对于每个整数 i ≤ N,使用 Euler 的 Totient函数找到所有与 i 互质的对并将它们存储在数组中。

例子:

arr[10] = 10 * (1-1/2) * (1-1/5)
        = 4

3.

4. 现在构建前缀总和表,该表存储所有 1 到 N 之间的所有 i 的所有 phi(i) 的总和。使用它,我们可以在 O(1) 时间内回答每个查询。

5. 最后,任何 i ≤ N 的答案由形成的对总数与 pref[i] 之间的差值给出。

下面是给定方法的实现:

C++
// C++ program to find the count of pairs
// from 1 to N such that their LCM
// is not equal to their product
#include 
using namespace std;
 
#define N 100005
 
// To store Euler's Totient Function
int phi[N];
 
// To store prefix sum table
int pref[N];
 
// Compute Totients of all numbers
// smaller than or equal to N
void precompute()
{
    // Make phi[1]=0 since 1 cannot form any pair
    phi[1] = 0;
 
    // Initialise all remaining phi[] with i
    for (int i = 2; i < N; i++)
        phi[i] = i;
 
    // Compute remaining phi
    for (int p = 2; p < N; p++) {
 
        // If phi[p] is not computed already,
        // then number p is prime
        if (phi[p] == p) {
 
            // phi of prime number is p-1
            phi[p] = p - 1;
 
            // Update phi of all multiples of p
            for (int i = 2 * p; i < N; i += p) {
 
                // Add the contribution of p
                // to its multiple i by multiplying
                // it with (1 - 1/p)
                phi[i] = (phi[i] / p) * (p - 1);
            }
        }
    }
}
 
// Function to store prefix sum table
void prefix()
{
    // Prefix Sum of all Euler's Totient Values
    for (int i = 1; i < N; i++)
        pref[i] = pref[i - 1] + phi[i];
}
 
void find_pairs(int n)
{
 
    // Total number of pairs that can be formed
    int total = (n * (n - 1)) / 2;
 
    int ans = total - pref[n];
 
    cout << "Number of pairs from 1 to "
         << n << " are " << ans << endl;
}
 
// Driver Code
int main()
{
 
    // Function call to compute all phi
    precompute();
 
    // Function call to store all prefix sum
    prefix();
 
    int q[] = { 5, 7 };
    int n = sizeof(q) / sizeof(q[0]);
 
    for (int i = 0; i < n; i++) {
        find_pairs(q[i]);
    }
 
    return 0;
}


Java
// Java program to find the count of pairs
// from 1 to N such that their LCM
// is not equal to their product
 
class GFG{
 
static final int N = 100005;
 
// To store Euler's Totient Function
static int []phi = new int[N];
 
// To store prefix sum table
static int []pref = new int[N];
 
// Compute Totients of all numbers
// smaller than or equal to N
static void precompute()
{
    // Make phi[1] = 0 since 1 cannot form any pair
    phi[1] = 0;
 
    // Initialise all remaining phi[] with i
    for (int i = 2; i < N; i++)
        phi[i] = i;
 
    // Compute remaining phi
    for (int p = 2; p < N; p++) {
 
        // If phi[p] is not computed already,
        // then number p is prime
        if (phi[p] == p) {
 
            // phi of prime number is p-1
            phi[p] = p - 1;
 
            // Update phi of all multiples of p
            for (int i = 2 * p; i < N; i += p) {
 
                // Add the contribution of p
                // to its multiple i by multiplying
                // it with (1 - 1/p)
                phi[i] = (phi[i] / p) * (p - 1);
            }
        }
    }
}
 
// Function to store prefix sum table
static void prefix()
{
    // Prefix Sum of all Euler's Totient Values
    for (int i = 1; i < N; i++)
        pref[i] = pref[i - 1] + phi[i];
}
 
static void find_pairs(int n)
{
 
    // Total number of pairs that can be formed
    int total = (n * (n - 1)) / 2;
 
    int ans = total - pref[n];
 
    System.out.print("Number of pairs from 1 to "
        + n + " are " + ans +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Function call to compute all phi
    precompute();
 
    // Function call to store all prefix sum
    prefix();
 
    int q[] = { 5, 7 };
    int n = q.length;
 
    for (int i = 0; i < n; i++) {
        find_pairs(q[i]);
    }
 
}
}
 
// This code contributed by Rajput-Ji


Python3
# Python 3 program to find the count of pairs
# from 1 to N such that their LCM
# is not equal to their product
 
N = 100005
 
# To store Euler's Totient Function
phi = [0 for i in range(N)]
 
# To store prefix sum table
pref = [0 for i in range(N)]
 
# Compute Totients of all numbers
# smaller than or equal to N
def precompute():
 
    # Make phi[1]=0 since 1 cannot form any pair
    phi[1] = 0
 
    # Initialise all remaining phi[] with i
    for i in range(2, N, 1):
        phi[i] = i
 
    # Compute remaining phi
    for p in range(2,N):
        # If phi[p] is not computed already,
        # then number p is prime
        if (phi[p] == p):
            # phi of prime number is p-1
            phi[p] = p - 1
 
            # Update phi of all multiples of p
            for i in range(2*p, N, p):
 
                # Add the contribution of p
                # to its multiple i by multiplying
                # it with (1 - 1/p)
                phi[i] = (phi[i] // p) * (p - 1)
 
# Function to store prefix sum table
def prefix():
 
    # Prefix Sum of all Euler's Totient Values
    for i in range(1, N, 1):
        pref[i] = pref[i - 1] + phi[i]
 
def find_pairs(n):
    # Total number of pairs that can be formed
    total = (n * (n - 1)) // 2
 
    ans = total - pref[n]
 
    print("Number of pairs from 1 to",n,"are",ans)
 
# Driver Code
if __name__ == '__main__':
    # Function call to compute all phi
    precompute()
 
    # Function call to store all prefix sum
    prefix()
 
    q =  [5, 7]
    n = len(q)
 
    for i in range(n):
        find_pairs(q[i])
         
# This code is contributed by Surendra_Gangwar


C#
// C# program to find the count of pairs
// from 1 to N such that their LCM
// is not equal to their product
using System;
 
class GFG{
 
static readonly int N = 100005;
 
// To store Euler's Totient Function
static int []phi = new int[N];
 
// To store prefix sum table
static int []pref = new int[N];
 
// Compute Totients of all numbers
// smaller than or equal to N
static void precompute()
{
 
    // Make phi[1] = 0 since 1
    // cannot form any pair
    phi[1] = 0;
 
    // Initialise all remaining
    // phi[] with i
    for(int i = 2; i < N; i++)
       phi[i] = i;
 
    // Compute remaining phi
    for(int p = 2; p < N; p++)
    {
        
       // If phi[p] is not computed already,
       // then number p is prime
       if (phi[p] == p)
       {
            
           // phi of prime number is p-1
           phi[p] = p - 1;
            
           // Update phi of all multiples of p
           for(int i = 2 * p; i < N; i += p)
           {
               
              // Add the contribution of p
              // to its multiple i by multiplying
              // it with (1 - 1/p)
              phi[i] = (phi[i] / p) * (p - 1);
           }
       }
    }
}
 
// Function to store prefix sum table
static void prefix()
{
     
    // Prefix Sum of all
    // Euler's Totient Values
    for(int i = 1; i < N; i++)
       pref[i] = pref[i - 1] + phi[i];
}
 
static void find_pairs(int n)
{
 
    // Total number of pairs
    // that can be formed
    int total = (n * (n - 1)) / 2;
    int ans = total - pref[n];
 
    Console.Write("Number of pairs from 1 to " +
                      n + " are " + ans + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
 
    // Function call to compute all phi
    precompute();
 
    // Function call to store
    // all prefix sum
    prefix();
 
    int []q = {5, 7};
    int n = q.Length;
 
    for(int i = 0; i < n; i++)
    {
       find_pairs(q[i]);
    }
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Number of pairs from 1 to 5 are 1
Number of pairs from 1 to 7 are 4