杭电多校第七场 1005 HDU-6390 GuGuFishtion(欧拉函数 + 容斥 || 莫比乌斯反演)

28 篇文章 0 订阅

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=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.
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

 

题目大意:题目定义Gu(a,b)=\frac{\varphi(a*b)}{\varphi(a)*\varphi(b)},现在给出n,m,p,要求\sum_{a=1}^{m}\sum_{b=1}^{n}Gu(a,b)

题目思路:对于式子Gu(a,b)=\frac{\varphi(a*b)}{\varphi(a)*\varphi(b)},我们可以做如下的转化

Gu(a,b)=\frac{\varphi(a*b)}{\varphi(a)*\varphi(b)}=a*b*\frac{\prod_{p1|a}^{ } (1-\frac{1}{p1})*\prod_{p2|b}^{ } (1-\frac{1}{p2})}{\prod_{p3|gcd(a,b)}^{ } (1-\frac{1}{p3})}

=\frac{(a*\prod_{p1|a}^{ } (1-\frac{1}{p1}))*(b*\prod_{p2|b}^{ } (1-\frac{1}{p2}))}{\prod_{p3|gcd(a,b)}^{ } (1-\frac{1}{p3})}*\frac{gcd(a,b)}{gcd(a,b)}

=gcd(a,b)*\frac{(a*\prod_{p1|a}^{ } (1-\frac{1}{p1}))*(b*\prod_{p2|b}^{ } (1-\frac{1}{p2}))}{gcd(a,b)*\prod_{p3|gcd(a,b)}^{ } (1-\frac{1}{p3})}

=\varphi(a)*\varphi(b)*\frac{gcd(a,b)}{\varphi(gcd(a,b))}

Gu(a,b)=\frac{\varphi(a*b)}{\varphi(a)*\varphi(b)}=\frac{gcd(a,b)}{\varphi(gcd(a,b))}

推出这个式子之后,我们就可以直接枚举a和b的gcd,求出gcd(a,b)=g,g=1,...,n。接着可以进行容斥或者莫比乌斯反演进行容斥。

直接容斥的话,我们可以令F[d]表示gcd为d的倍数的方案数,f[d]表示gcd正好为d的方案数,那么就有

F(d)=\sum_{d|n}^{ }f(n)。对于这个式子,我们可以直接倒着进行计算,对于每个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;
}

 

当然,对于式子F(d)=\sum_{d|n}^{ }f(n),我们有可以很容易的做出莫比乌斯反演,使得f(d)=\sum_{d|n}^{ }\mu(\frac{n}{d})*F(n)。这样也可以进行容斥求解。

代码如下:

#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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值