HDU 6390

GuGuFishtion

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1699    Accepted Submission(s): 627


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.
1T3
1m,n1,000,000
max(m,n)<p1,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
解析  我们容易得到 g(a,b)=ϕ(ab)/ϕ(a) ϕ(b)=gcd(a,b)/ ϕ(gcd(a,b)
1-n,1-m的 gcd最多有min(n,m)个所以转换成求1-n,1-m中gcd(a,b)=k,k的出现次数.
AC代码
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e6+10,inf=0x3f3f3f3f;
int check[maxn],phi[maxn],prime[maxn];
ll inv[maxn],f[maxn];
void philist()
{
    int cnt=0;
    phi[1]=1;
    for(int i=2; i<=maxn; i++)
    {
        if(!check[i])
        {
            prime[++cnt]=i;
            phi[i]=i-1;
        }
        for(int j=1; j<=cnt&&prime[j]*i<=maxn; j++)
        {
            check[i*prime[j]]=1;
            if(i%prime[j])
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            else
            {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
        }
    }
}
void init(int mod,int n)
{
    fillchar(f,0);
    inv[1]=1;
    for(int i=2;i<=n;i++)
        inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
}
int main()
{
    int t,n,m,p;
    scanf("%d",&t);
    philist();
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&p);
        ll ans=0;
        if(n>m)
            swap(n,m);
        init(p,n);
        for(ll i=n;i>=1;i--)       //容斥求1-n,1-m中gcd为i的个数
        {
            f[i]=(ll)(n/i)*(m/i);
            for(int j=i+i; j<=n; j+=i) //f[i]表示gcd为i的对数有f[i]个
                f[i]=f[i]-f[j];
            ans=(ans+f[i]%p*i%p*inv[phi[i]])%p;
        }
        printf("%lld\n",ans);
    }
}

 

转载于:https://www.cnblogs.com/stranger-/p/9807329.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值