GuGuFishtion
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 324 Accepted Submission(s): 126
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
题目大意:题目定义,现在给出n,m,p,要求
。
题目思路:对于式子,我们可以做如下的转化
。
推出这个式子之后,我们就可以直接枚举a和b的gcd,求出gcd(a,b)=g,g=1,...,n。接着可以进行容斥或者莫比乌斯反演进行容斥。
直接容斥的话,我们可以令F[d]表示gcd为d的倍数的方案数,f[d]表示gcd正好为d的方案数,那么就有
。对于这个式子,我们可以直接倒着进行计算,对于每个d枚举d所有小于max(n,m)的倍数,再减去d的倍数方案数即可。
代码如下:
#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) x&-x
#define pb push_back
#define MP make_pair
#define clr(a) memset(a,0,sizeof(a))
#define INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<"["<<x<<"]"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
typedef vector<int> VI;
//head
const int mod=1e9+7;
const int MX=1e6+7;
const int maxn=1e5+7;
const int inf=0x3f3f3f3f;
int n,m,p,_;
int prime[maxn],pcnt;
int phi[MX];
ll inv[MX],f[MX];
bool is_prime[MX];
void phi_init(){
for(int i = 0;i < MX;i++) is_prime[i] = true;
pcnt = 0;
phi[1]=1;
for(int i = 2;i < MX;i++){
if(is_prime[i]){
prime[pcnt++] = i;
phi[i] = i - 1;
}
for(int j = 0;j < pcnt;j++){
if(i*prime[j] >= MX) break;
is_prime[i*prime[j]] = false;
if(i%prime[j] == 0){
phi[i*prime[j]] = phi[i] * prime[j];
break;
} else{
phi[i*prime[j]] = phi[i] *(prime[j] - 1);
}
}
}
}
int main(){
phi_init();
for(scanf("%d",&_);_;_--){
scanf("%d%d%d",&n,&m,&p);
if(n<m) swap(n,m);
inv[1]=1;
for(int i=2; i<=n; ++i) inv[i]=(ll)inv[p%i]*(p-p/i)%p;
ll ans=0;
for(int i=n;i>=1;i--){
f[i]=(ll)(n/i)*(m/i);
for(int j=i*2;j<=n;j+=i) f[i]-=f[j];
ans=(ans+f[i]%p*i%p*inv[phi[i]])%p;
}
printf("%lld\n",ans);
}
return 0;
}
当然,对于式子,我们有可以很容易的做出莫比乌斯反演,使得
。这样也可以进行容斥求解。
代码如下:
#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) x&-x
#define pb push_back
#define MP make_pair
#define clr(a) memset(a,0,sizeof(a))
#define INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<"["<<x<<"]"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
typedef vector<int> VI;
//head
const int mod=1e9+7;
const int MX=1e6+7;
const int maxn=1e5+7;
const int inf=0x3f3f3f3f;
int n,m,p,_;
int prime[maxn],pcnt;
int phi[MX],mu[MX]={0,1};
ll inv[MX];
bool is_prime[MX];
void init(){
for(int i = 0;i < MX;i++) is_prime[i] = true;
pcnt = 0;
phi[1]=1;
for(int i = 2;i < MX;i++){
if(is_prime[i]){
prime[pcnt++] = i;
phi[i] = i - 1;
mu[i]=-1;
}
for(int j = 0;j < pcnt;j++){
if(i*prime[j] >= MX) break;
is_prime[i*prime[j]] = false;
if(i%prime[j] == 0){
phi[i*prime[j]] = phi[i] * prime[j];
mu[i*prime[j]]=0;
break;
} else{
phi[i*prime[j]] = phi[i] *(prime[j] - 1);
mu[i*prime[j]]=-mu[i];
}
}
}
}
int main(){
init();
for(scanf("%d",&_);_;_--){
scanf("%d%d%d",&n,&m,&p);
if(n<m) swap(n,m);
inv[1]=1;
for(int i=2; i<=n; ++i) inv[i]=(ll)inv[p%i]*(p-p/i)%p;
ll ans=0;
for(int i=1;i<=n;i++){
ll res=0;
for(int j=i;j<=n;j+=i) res+=(ll)mu[j/i]*(n/j)*(m/j);
ans=(ans+res%p*i%p*inv[phi[i]])%p;
}
printf("%lld\n",ans);
}
return 0;
}