HDU 6390

Posted stranger-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("
")
#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
",ans);
    }
}

 























以上是关于HDU 6390的主要内容,如果未能解决你的问题,请参考以下文章

HDU 6390

E - GuGuFishtion HDU - 6390(欧拉函数 / 莫比乌斯反演)

GuGuFishtion HDU - 6390 (欧拉函数,容斥)

hdu6390 /// 欧拉函数+莫比乌斯反演 筛inv[] phi[] mu[]

HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解

2019.10.27 GMOJ6390 黑箱