题意:
思路: 莫比乌斯反演水题,可以把式子化成d/phi(d),时间复杂度比较紧,long long开多了会T,时间复杂度O(nloglogn)这样
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 1000000;
#define rush() int T;scanf("%d",&T);while(T--)
int m,n;int tot = 0;
ll p;
ll vis[maxn+5],phi[maxn+5],mu[maxn+5];
ll prime[maxn+5];
ll inv[maxn+5];
ll pre[maxn+5];
void euler()
{
memset(phi, 0, sizeof(phi));
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);
}
}
}
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++){
pre[i]=(ll)i*inv[phi[i]]%p;
}
}
ll get(int a,int b){
ll res = 0;
for (int i=1;i<=min(a,b);i++){
res += (ll)mu[i]*(a/i)*(b/i);
res%=p;
}
return res;
}
void solve (){
ll ans = 0;
for (int d=1;d<=min(n,m);d++){
ans +=(ll)pre[d]*get(n/d,m/d);
ans%=p;
//printf("%lld\n",ans);
}
printf("%lld\n",ans);
}
int main (){
euler();
rush()
{
scanf ("%d%d%lld",&n,&m,&p);
init ();
solve ();
}
}