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:

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

并保证 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 , m , p ,求

( a = 1 m b = 1 n ϕ ( a b ) ϕ ( a ) ϕ ( b ) ) ( mod p )

其中:

1 m , n 1 , 000 , 000

max ( m , n ) < p 1 , 000 , 000 , 007

并保证 p 为质数

【思路】

首先通过化简容易得到

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

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

f ( m , n ) = ( i = 1 m j = 1 n a [ g c d ( i , j ) ] ) ( mod p )

= ( d = 1 m i n ( n , m ) i = 1 m j = 1 n a [ d ] [ g c d ( i , j ) == d ] ) ( mod p )

= ( 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 ) = 0 ( n > 1 )

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

= i = 1 m j = 1 n d | g c d ( i , j ) μ ( d )

= d = 1 m i n ( n , m ) μ ( d ) n d m d

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

时间复杂度: i = 1 n n i 约为 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);
    }
}
发布了259 篇原创文章 · 获赞 100 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/81635187