E - GuGuFishtion HDU - 6390 (欧拉函数,反演)

Today XianYu is too busy with his homework, but the boring GuGu is still disturbing him!!!!!! 
At the break time, an evil idea arises in XianYu's mind. 
‘Come on, you xxxxxxx little guy.’ 
‘I will give you a function ϕ(x)ϕ(x) which counts the positive integers up to xx that are relatively prime to xx.’ 
‘And now I give you a fishtion, which named GuGu Fishtion, in memory of a great guy named XianYu and a disturbing and pitiful guy GuGu who will be cooked without solving my problem in 5 hours.’ 
‘The given fishtion is defined as follow: 
Gu(a,b)=ϕ(ab)ϕ(a)ϕ(b)Gu(a,b)=ϕ(ab)ϕ(a)ϕ(b)
And now you, the xxxxxxx little guy, have to solve the problem below given mm,nn,pp.’ 
(∑a=1m∑b=1nGu(a,b))(modp)(∑a=1m∑b=1nGu(a,b))(modp)
So SMART and KINDHEARTED you are, so could you please help GuGu to solve this problem? 
‘GU GU!’ GuGu thanks. 

InputInput contains an integer TT indicating the number of cases, followed by TT lines. Each line contains three integers mm,nn,pp as described above. 
1≤T≤31≤T≤3 
1≤m,n≤1,000,0001≤m,n≤1,000,000 
max(m,n)<p≤1,000,000,007max(m,n)<p≤1,000,000,007 
And given pp is a prime. 
OutputPlease output exactly TT lines and each line contains only one integer representing the answer. 
Sample Input

1
5 7 23

Sample Output

2

Solution:

phi(ab) = phi(a)*phi(b)*gcd(a,b)/phi(gcd(a,b))
然后在去枚举gcd就行了
递推逆元爆int?

Code:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+100;
typedef long long ll;
int fac[maxn],mu[maxn],phi[maxn];
int g[maxn];
ll inv[maxn];
void init()
{
    for(int i=1; i<maxn; ++i)
        fac[i]=i;
    phi[1]=mu[1]=1;
    for(int i=2; i<maxn; ++i)
    {
        if(fac[i]==i)
            for(int j=i<<1; j<maxn; j+=i)
                fac[j]=i;
        if(i/fac[i]%fac[i])
            phi[i]=(fac[i]-1)*phi[i/fac[i]],mu[i]=-mu[i/fac[i]];
        else
            phi[i]=fac[i]*phi[i/fac[i]],mu[i]=0;
    }
}
int n,m,p;

signed main()
{
    ios::sync_with_stdio(0);
    init();
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m>>p;
        int M=min(n,m);

        for(int i=1; i<=M; i++)
        {
            g[i]=0;
            int MM=min(n/i,m/i);
            int A=n/i;
            int B=m/i;
            for(int d=1; d<=MM; d++)
                g[i] += 1ll*mu[d]*(n/i/d)*(m/i/d)%p,g[i]%=p;
            if(g[i]<0)
                g[i] += p;
        }

        int ans=0;
        inv[1]=1;
        for( int i=2; i<=M; i++)
        {
            inv[i]=1ll*(p-p/i)*inv[p%i]%p;
        }
        for(int k=1; k<=M; k++)
        {
            ans=(1ll*ans+1ll*k*inv[phi[k]]%p*1ll*g[k]%p)%p;
        }
        cout<<ans<<endl;


    }
// 5 1000000 1000000 1000000007
}

  







上一篇:0.618法 求 最优化极小值


下一篇:android基础开发之RecycleView(1)---基本使用方式