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:
And now you, the xxxxxxx little guy, have to solve the problem below given m,n,p.
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
indicating the number of cases, followed by T lines. Each line contains three integers
as described above.
并保证 为质数
Output
Please output exactly lines and each line contains only one integer representing the answer.
Examples
Input
1
5 7 23
Output
2
【题目链接】 GuGuFishtion
【题意】
给定
,
,
,求
其中:
并保证 为质数
【思路】
首先通过化简容易得到
我们设a[i]=
那么原式就转化为
根据莫比乌斯函数的定义:
于是我们可以设
则我们需要求的答案
时间复杂度: 约为
PS:需提前预处理出逆元
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
typedef long long ll;
const int maxn = 1000005;
const ll INF = 1e18;
const double eps = 1e-9;
int n,m;
ll p;
char s[maxn];
ll a[maxn];
ll res[maxn],mu[maxn],inv[maxn];
void euler()
{
mst(res,0);
res[1]=1;
for(int i=2;i<maxn;i++)
{
if(!res[i])
{
for(int j=i;j<maxn;j+=i)
{
if(!res[j])
res[j]=j;
res[j]=res[j]/i*(i-1);
}
}
}
mu[1]=1;
for(int i=1;i<maxn;i++)
for(int j=2*i;j<maxn;j+=i)
{
mu[j]-=mu[i];
}
}
void init()
{
inv[1]=1;
for(int i=2;i<=min(n,m);i++) inv[i]=inv[p%i]*(p-p/i)%p;
for(int i=1;i<=min(n,m);i++)
{
a[i]=(ll)i*inv[res[i]]%p;
}
}
ll get(int n,int m)
{
ll ans=0;
for(int i=1;i<=min(n, m);i++)
{
ans+=(ll)mu[i]*(n/i)*(m/i);
ans%=p;
}
return ans;
}
int main()
{
euler();
rush()
{
scanf("%d%d%lld",&n,&m,&p);
init();
ll ans=0;
for(int i=1;i<=min(n, m);i++)
{
ans+=(ll)a[i]*get(n/i,m/i);
ans%=p;
}
printf("%lld\n",ans);
}
}