HDU 6390

GuGuFishtion

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1699    Accepted Submission(s): 627


Problem Description
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) which counts the positive integers up to x that are relatively prime to x.’
‘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)

And now you, the xxxxxxx little guy, have to solve the problem below given m,n,p.’
(a=1mb=1nGu(a,b))(modp)

So SMART and KINDHEARTED you are, so could you please help GuGu to solve this problem?
‘GU GU!’ GuGu thanks.
 
Input
Input contains an integer  T indicating the number of cases, followed by T lines. Each line contains three integers m,n,p as described above.
1T3
1m,n1,000,000
max(m,n)<p1,000,000,007
And given p is a prime.
 
Output
Please output exactly  T lines and each line contains only one integer representing the answer.
 
Sample Input
1
5 7 23
 
Sample Output
2
解析  我们容易得到 g(a,b)=ϕ(ab)/ϕ(a) ϕ(b)=gcd(a,b)/ ϕ(gcd(a,b)
1-n,1-m的 gcd最多有min(n,m)个所以转换成求1-n,1-m中gcd(a,b)=k,k的出现次数.
AC代码
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e6+10,inf=0x3f3f3f3f;
int check[maxn],phi[maxn],prime[maxn];
ll inv[maxn],f[maxn];
void philist()
{
    int cnt=0;
    phi[1]=1;
    for(int i=2; i<=maxn; i++)
    {
        if(!check[i])
        {
            prime[++cnt]=i;
            phi[i]=i-1;
        }
        for(int j=1; j<=cnt&&prime[j]*i<=maxn; j++)
        {
            check[i*prime[j]]=1;
            if(i%prime[j])
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            else
            {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
        }
    }
}
void init(int mod,int n)
{
    fillchar(f,0);
    inv[1]=1;
    for(int i=2;i<=n;i++)
        inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
}
int main()
{
    int t,n,m,p;
    scanf("%d",&t);
    philist();
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&p);
        ll ans=0;
        if(n>m)
            swap(n,m);
        init(p,n);
        for(ll i=n;i>=1;i--)       //容斥求1-n,1-m中gcd为i的个数
        {
            f[i]=(ll)(n/i)*(m/i);
            for(int j=i+i; j<=n; j+=i) //f[i]表示gcd为i的对数有f[i]个
                f[i]=f[i]-f[j];
            ans=(ans+f[i]%p*i%p*inv[phi[i]])%p;
        }
        printf("%lld\n",ans);
    }
}

猜你喜欢

转载自www.cnblogs.com/stranger-/p/9807329.html