2018 Multi-University Training Contest 7 HDU6390 GuGuFishtion【数论欧拉函数|gcd|莫比乌斯】

GuGuFishtion

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

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) G u ( a , b ) = ϕ ( a b ) ϕ ( 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) ( ∑ a = 1 m ∑ b = 1 n G u ( a , b ) ( mod p )

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 T indicating the number of cases, followed by T lines. Each line contains three integers m,n,p as described above.
1T3 1 ≤ T ≤ 3

1m,n1,000,000 1 ≤ m , n ≤ 1 , 000 , 000

max(m,n)<p1,000,000,007 max ⁡ ( m , n ) < p ≤ 1 , 000 , 000 , 007

并保证 p p 为质数

Output

Please output exactly T lines and each line contains only one integer representing the answer.

Examples

Input
1
5 7 23

Output
2


【题目链接】 GuGuFishtion

【题意】
给定 n n ,m, p p ,求

(a=1mb=1nϕ(ab)ϕ(a)ϕ(b))(modp)

其中:

1m,n1,000,000 1 ≤ m , n ≤ 1 , 000 , 000

max(m,n)<p1,000,000,007 max ⁡ ( m , n ) < p ≤ 1 , 000 , 000 , 007

并保证 p p 为质数

【思路】

首先通过化简容易得到

ϕ(ab)ϕ(a)ϕ(b)=gcd(a,b)ϕ(gcd(a,b))

我们设a[i]= iϕ(i) i ϕ ( i )
那么原式就转化为

f(m,n)=(i=1mj=1na[gcd(i,j)])(modp) f ( m , n ) = ( ∑ i = 1 m ∑ j = 1 n a [ g c d ( i , j ) ] ) ( mod p )

=(d=1min(n,m)i=1mj=1na[d][gcd(i,j)==d])(modp) = ( ∑ d = 1 m i n ( n , m ) ∑ i = 1 m ∑ j = 1 n a [ d ] ∗ [ g c d ( i , j ) == d ] ) ( mod p )

=(d=1min(n,m)i=1m/dj=1n/da[d][gcd(i,j)==1])(modp) = ( ∑ d = 1 m i n ( n , m ) ∑ i = 1 m / d ∑ j = 1 n / d a [ d ] ∗ [ g c d ( i , j ) == 1 ] ) ( mod p )

根据莫比乌斯函数的定义:
d|nμ(d)=1(n=1) ∑ d | n μ ( d ) = 1 ( 当 n = 1 时 )

d|nμ(d)=0(n>1) ∑ d | n μ ( d ) = 0 ( 当 n > 1 时 )

于是我们可以设
g(m,n)=(i=1mj=1n[gcd(a,b)==1]) g ( m , n ) = ( ∑ i = 1 m ∑ j = 1 n [ g c d ( a , b ) == 1 ] )

=i=1mj=1nd|gcd(i,j)μ(d) = ∑ i = 1 m ∑ j = 1 n ∑ d | g c d ( i , j ) μ ( d )

=d=1min(n,m)μ(d)ndmd = ∑ d = 1 m i n ( n , m ) μ ( d ) ∗ n d ∗ m d

则我们需要求的答案
f(m,n)=(d=1min(n,m)a[d]g(md,nd) f ( m , n ) = ( ∑ d = 1 m i n ( n , m ) a [ d ] ∗ g ( m d , n d )

时间复杂度: ni=1ni ∑ i = 1 n n i 约为 nlogn n l o g n

PS:需提前预处理出逆元

#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 1000005;
const ll INF = 1e18;
const double eps = 1e-9;

int n,m;
ll p;
char s[maxn];
ll a[maxn];
ll res[maxn],mu[maxn],inv[maxn];

void euler()
{
    mst(res,0);
    res[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if(!res[i])
        {
            for(int j=i;j<maxn;j+=i)
            {
                if(!res[j])
                    res[j]=j;
                res[j]=res[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++)
    {
        a[i]=(ll)i*inv[res[i]]%p;
    }
}

ll get(int n,int m)
{
    ll ans=0;
    for(int i=1;i<=min(n, m);i++)
    {
        ans+=(ll)mu[i]*(n/i)*(m/i);
        ans%=p;
    }
    return ans;
}

int main()
{
    euler();
    rush()
    {
        scanf("%d%d%lld",&n,&m,&p);
        init();
        ll ans=0;
        for(int i=1;i<=min(n, m);i++)
        {
            ans+=(ll)a[i]*get(n/i,m/i);
            ans%=p;
        }
        printf("%lld\n",ans);
    }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值