HDU6390:GuGuFishtion(数学)

GuGuFishtion

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


 

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

 

 

Source

2018 Multi-University Training Contest 7

题意:给出n、m、p算出给定的公式。

思路:根据公式,ϕ(mn) = ϕ(m)ϕ(n)*d/ϕ(d),其中d=gcd(n,m)。于是原式子可以化简成d/ϕ(d),枚举最大公因数即可,用莫比乌斯算或者不用,O(nlogn)了。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+30;
LL inv[maxn+3]={1,1}, mod, num[maxn+3];
int phi[maxn];
void table()
{
    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);
            }
}
int main()
{
    table();
    int T;
    LL m, n;
    for(scanf("%d",&T);T;--T)
    {
        scanf("%lld%lld%lld",&m,&n, &mod);
        memset(num, 0, sizeof(num));
        LL imin = min(n,m), ans=0;
        for(int i=2; i<=imin; ++i) inv[i] = inv[mod%i]*(mod-mod/i)%mod;
        for(int i=imin; i>=1; --i)
        {
            num[i] = (n/i)*(m/i)%mod;
            for(int j=i+i; j<=imin; j+=i)
            {
                num[i] -= num[j];
                if(num[i] < 0) num[i] += mod;
            }
            ans += i*inv[phi[i]]%mod * num[i]%mod;
            ans %= mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值