GuGuFishtion
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 112 Accepted Submission(s): 38
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=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.
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.
1≤T≤3
1≤m,n≤1,000,000
max(m,n)<p≤1,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
Source
2018 Multi-University Training Contest 7
题意:给出n、m、p算出给定的公式。
思路:根据公式,ϕ(mn) = ϕ(m)ϕ(n)*d/ϕ(d),其中d=gcd(n,m)。于是原式子可以化简成d/ϕ(d),枚举最大公因数即可,用莫比乌斯算或者不用,O(nlogn)了。
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+30;
LL inv[maxn+3]={1,1}, mod, num[maxn+3];
int phi[maxn];
void table()
{
phi[1]=1;
for(int i=2;i<maxn;i++)
if(!phi[i])
for(int j=i;j<maxn;j+=i)
{
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
int main()
{
table();
int T;
LL m, n;
for(scanf("%d",&T);T;--T)
{
scanf("%lld%lld%lld",&m,&n, &mod);
memset(num, 0, sizeof(num));
LL imin = min(n,m), ans=0;
for(int i=2; i<=imin; ++i) inv[i] = inv[mod%i]*(mod-mod/i)%mod;
for(int i=imin; i>=1; --i)
{
num[i] = (n/i)*(m/i)%mod;
for(int j=i+i; j<=imin; j+=i)
{
num[i] -= num[j];
if(num[i] < 0) num[i] += mod;
}
ans += i*inv[phi[i]]%mod * num[i]%mod;
ans %= mod;
}
printf("%lld\n",ans);
}
return 0;
}