Wu Chengyao's common divisor

Insert picture description here

You cannot first find ans = gcd(ans,xi) for all bases, and then find how many ans there are in each xi^pi, so the answer you get is wrong.

For example

Input
2
8 4
2 3
Output
64

As a result, I did that and output 16

That is, the remaining number of xi/ans after ans = gcd(ans,xi) is not considered, and ans can be obtained after the power of p,
such as x = 8, p = 2, x /4 = 2; 2^2 = 4;

The correct method is to decompose the base x by the prime factor
x = a b c......
x^p = a^p * b^p * c^p......;
so just record the minimum base Pmin of each prime factor to get
ans = a^Pmin * b^Pmin……;
ans%mod is the final answer, just use fast exponentiation and mod

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num[20000][2];//x,p
ll key[20000];//下标为质因数,存最小质因数指数
ll mod = 1e9 + 7;

ll min(ll x,ll y){
    
    
    return x < y? x:y;
}
ll max(ll x,ll y){
    
    
    return x < y? y:x;
}
ll qqow(ll x,ll y){
    
    
    ll ans = 1;
    while(y){
    
    
        if(y & 1)
            ans = ans*x%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
int main(){
    
    
    ll n;
    ll ans = 1;
    
    ll x;
   
    cin >> n;
    for(int i = 0;i < n;i++){
    
    
        cin >> num[i][0];
    }
    for(int i = 0;i <= 20000;i++)
        key[i] = 10000000;
    for(int i = 0;i < n;i++)
        cin >> num[i][1];
    for(int i = 0;i < n;i++){
    
    
        for(ll j= 2;j <= 10000;j++){
    
    
            x = 0;
            while(num[i][0]%j == 0){
    
    
                num[i][0]/=j;
                x++;
            }
            key[j] = min(key[j],x*num[i][1]);
            //确保每个质因数都覆盖,如果质因数不是x的因数,那么该质因数的指数就应该是0
        }
        
    }
    for(int i = 2;i <= 10000;i++){
    
    
        if(key[i])
            ans = ans * qqow(i,key[i])%mod;
    }
    cout << ans%mod << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/RunningBeef/article/details/113874068