📌  相关文章
📜  数组B的最小可能总和,使得对于所有1≤i <j≤N的AiBi = AjBj

📅  最后修改于: 2021-05-07 08:58:50             🧑  作者: Mango

给定大小为N的数组a [] 。任务是找到数组b []的元素的最小可能和,以使a [i] * b [i] = a [j] * b [j]对于所有1≤i 。答案可能很大。因此,打印答案modoulo 10 9 + 7

例子:

方法:假定满足给定条件的B i被确定。然后令K = A 1 * B 1 ,则对于所有j> 1 ,约束K = A 1 * B 1 = A j * B j成立。因此, KA 1 ,…,A N的公倍数。
相反,令lcmA 1 ,…,A N的最小公倍数,令B i = lcm / A i,则这样的B满足条件。
因此,期望的答案是∑lcm / A i 。但是, lcm可以是一个非常大的数字,因此无法直接计算。现在,让我们考虑进行计算,以因式分解的形式持有lcm 。令p i为质数,并假设因式分解由X = ∏p i g iY = ∏p i f i ( g i ,f i均可为0 )给出。然后XY最小公倍数∏p i max(g i ,f i )给出。通过使用它,可以以因式分解的形式获得A 1 ,…,A N的最小公倍数。因此,可以在总共O(N * sqrt(A))的时间内解决此问题,其中A = max(A i ) 。同样,通过使用适当的预先计算来加速素数分解,也可以在总计O(A + N * logA)的时间内获得答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
#define mod (int)(1e9 + 7)
#define N 1000005
  
// To store least prime factors
// of all the numbers
int lpf[N];
  
// Function to find the least prime
// factor of all the numbers
void least_prime_factor()
{
    for (int i = 1; i < N; i++)
        lpf[i] = i;
  
    for (int i = 2; i < N; i++)
        if (lpf[i] == i)
            for (int j = i * 2; j < N; j += i)
                if (lpf[j] == j)
                    lpf[j] = i;
}
  
// Function to return the ((a^m1) % mod)
int power(int a, int m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (1LL * a * a) % mod;
    else if (m1 & 1)
        return (1LL * a * power(power(a, m1 / 2), 2)) % mod;
    else
        return power(power(a, m1 / 2), 2) % mod;
}
  
// Function to return the sum of
// elements of array B
long long sum_of_elements(int a[], int n)
{
    // Find the prime factors of
    // all the numbers
    least_prime_factor();
  
    // To store each prime count in lcm
    map prime_factor;
  
    for (int i = 0; i < n; i++) {
  
        // Current number
        int temp = a[i];
  
        // Map to store the prime count
        // of a single number
        map single_number;
  
        // Basic way to calculate all prime factors
        while (temp > 1) {
            int x = lpf[temp];
            single_number[x]++;
            temp /= x;
        }
  
        // If it is the first number in the array
        if (i == 0)
            prime_factor = single_number;
  
        // Take the maximum count of 
        // prime in a number
        else {
            for (auto x : single_number)
                prime_factor[x.first] = max(x.second, 
                                prime_factor[x.first]);
        }
    }
  
    long long ans = 0, lcm = 1;
  
    // Calculate lcm of given array
    for (auto x : prime_factor)
        lcm = (lcm * power(x.first, x.second)) % mod;
  
    // Calculate sum of elements of array B
    for (int i = 0; i < n; i++)
        ans = (ans + (lcm * power(a[i], 
                      mod - 2)) % mod) % mod;
  
    return ans;
}
  
// Driver code
int main()
{
    int a[] = { 2, 3, 4 };
    int n = sizeof(a) / sizeof(int);
  
    cout << sum_of_elements(a, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
      
static int mod = 1000000007;
static int N = 1000005;
  
// To store least prime factors
// of all the numbers
static int lpf[] = new int[N];
  
// Function to find the least prime
// factor of all the numbers
static void least_prime_factor()
{
    for (int i = 1; i < N; i++)
        lpf[i] = i;
  
    for (int i = 2; i < N; i++)
        if (lpf[i] == i)
            for (int j = i * 2; j < N; j += i)
                if (lpf[j] == j)
                    lpf[j] = i;
}
  
// Function to return the ((a^m1) % mod)
static long power(long a, long m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (1l * a * a) % mod;
    else if ((m1 & 1) != 0)
        return (1l * a * power(power(a, m1 / 2), 2)) % mod;
    else
        return power(power(a, m1 / 2), 2) % mod;
}
  
// Function to return the sum of
// elements of array B
static long sum_of_elements(long a[], int n)
{
    // Find the prime factors of
    // all the numbers
    least_prime_factor();
  
    // To store each prime count in lcm
    HashMap prime_factor 
            = new HashMap<>();
  
    for (int i = 0; i < n; i++) 
    {
  
        // Current number
        long temp = a[i];
  
        // Map to store the prime count
        // of a single number
        HashMap single_number
            = new HashMap<>();
  
        // Basic way to calculate all prime factors
        while (temp > 1) 
        {
            long x = lpf[(int)temp];
            single_number.put(x,(single_number.get(x) == 
                        null ? 1:single_number.get(x) + 1));
            temp /= x;
        }
  
        // If it is the first number in the array
        if (i == 0)
            prime_factor = single_number;
  
        // Take the maximum count of 
        // prime in a number
        else {
            for (Map.Entry x : single_number.entrySet() )
                prime_factor.put(x.getKey(), Math.max(x.getValue(), 
                                (prime_factor.get(x.getKey()) == 
                                null ? 0:prime_factor.get(x.getKey()))));
        }
    }
  
    long ans = 0, lcm = 1;
  
    // Calculate lcm of given array
    for (Map.Entry x : prime_factor.entrySet())
        lcm = (lcm * power(x.getKey(), x.getValue())) % mod;
  
    // Calculate sum of elements of array B
    for (int i = 0; i < n; i++)
        ans = (ans + (lcm * power(a[i], 
                    mod - 2)) % mod) % mod;
  
    return ans;
}
  
// Driver code
public static void main(String args[])
{
    long a[] = { 2, 3, 4 };
    int n = a.length;
  
    System.out.println(sum_of_elements(a, n));
}
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
mod = 10 ** 9 + 7
N = 1000005
  
# To store least prime factors
# of all the numbers
lpf = [0 for i in range(N)]
  
# Function to find the least prime
# factor of all the numbers
def least_prime_factor():
    for i in range(1, N):
        lpf[i] = i
  
    for i in range(2,N):
        if (lpf[i] == i):
            for j in range(i * 2, N, i):
                if (lpf[j] == j):
                    lpf[j] = i
  
# Function to return the sum of
# elements of array B
def sum_of_elements(a, n):
      
    # Find the prime factors of
    # all the numbers
    least_prime_factor()
  
    # To store each prime count in lcm
    prime_factor=dict()
  
    for i in range(n):
  
        # Current number
        temp = a[i]
  
        # Map to store the prime count
        # of a single number
        single_number = dict()
  
        # Basic way to calculate all prime factors
        while (temp > 1):
            x = lpf[temp]
            single_number[x] = single_number.get(x, 0) + 1
            temp //= x
  
  
        # If it is the first number in the array
        if (i == 0):
            prime_factor = single_number
  
        # Take the maximum count of
        # prime in a number
        else:
            for x in single_number:
                if x in prime_factor:
                    prime_factor[x] = max(prime_factor[x], 
                                           single_number[x])
                else:
                    prime_factor[x] = single_number[x]
  
    ans, lcm = 0, 1
  
    # Calculate lcm of given array
    for x in prime_factor:
        lcm = (lcm * pow(x, prime_factor[x],mod)) % mod
  
    # Calculate sum of elements of array B
    for i in range(n):
        ans = (ans + (lcm * pow(a[i], 
                mod - 2,mod)) % mod) % mod
  
    return ans
  
# Driver code
if __name__ == '__main__':
    a = [2, 3, 4]
    n = len(a)
    print(sum_of_elements(a, n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System; 
using System.Collections.Generic; 
  
class GFG{         
              
static int mod = 1000000007;
static int N = 1000005;
  
// To store least prime factors
// of all the numbers
static int []lpf = new int[N];
  
// Function to find the least prime
// factor of all the numbers
static void least_prime_factor()
{
    for(int i = 1; i < N; i++)
        lpf[i] = i;
  
    for(int i = 2; i < N; i++)
        if (lpf[i] == i)
            for(int j = i * 2; 
                    j < N; j += i)
                if (lpf[j] == j)
                    lpf[j] = i;
}
  
// Function to return the ((a^m1) % mod)
static long power(long a, long m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (a * a) % mod;
          
    else if ((m1 & 1) != 0)
        return (a * power(power(a, m1 / 2),
                                  2)) % mod;
    else
        return power(power(a, m1 / 2), 2) % mod;
}
  
// Function to return the sum of
// elements of array B
static long sum_of_elements(long []a, int n)
{
      
    // Find the prime factors of
    // all the numbers
    least_prime_factor();
  
    // To store each prime count in lcm
    Dictionary prime_factor = new Dictionary();
  
    for(int i = 0; i < n; i++) 
    {
          
        // Current number
        long temp = a[i];
  
        // Map to store the prime count
        // of a single number
        Dictionary single_number = new Dictionary();
  
        // Basic way to calculate all prime factors
        while (temp > 1) 
        {
            long x = lpf[(int)temp];
            if (single_number.ContainsKey(x))
            {
                single_number[x]++;
            }
            else
            {
                single_number[x] = 1;
            }
            temp /= x;
        }
  
        // If it is the first number in the array
        if (i == 0)
            prime_factor = single_number;
  
        // Take the maximum count of 
        // prime in a number
        else
        {
            foreach(KeyValuePair ele in single_number)
            {
                if (prime_factor.ContainsKey(ele.Key))
                {
                    prime_factor[ele.Key] = Math.Max(
                        ele.Value, prime_factor[ele.Key]);
                }
                else
                {
                    prime_factor[ele.Key] = Math.Max(
                        ele.Value, 0);
                }
            }
        }
    }
  
    long ans = 0, lcm = 1;
  
    // Calculate lcm of given array
    foreach(KeyValuePair x in prime_factor)
    {
        lcm = (lcm * power(x.Key, x.Value)) % mod;
    }
      
    // Calculate sum of elements of array B
    for(int i = 0; i < n; i++)
        ans = (ans + (lcm * power(a[i],
               mod - 2)) % mod) % mod;
  
    return ans;
}     
          
// Driver Code         
public static void Main (string[] args)
{         
    long []a = { 2, 3, 4 };
    int n = a.Length;
  
    Console.Write(sum_of_elements(a, n));
}         
}
  
// This code is contributed by rutvik_56


输出:
13