HDU 6390 GuGuFishtion(莫比乌斯函数+积性函数)

GuGuFishtion

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


 

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

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6437 6436 6435 6434 6433 

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)

#define ll long long
/*
题目要求求给定 的函数。

首先对函数进行分析,欧拉函数是积性函数,
可以把a和b分解成质数的乘积形式,
然后欧拉函数可以进行分解,
这样最终要求和的函数可以表达成:i/euler(i),i=gcd(a,b)。
对付这种求和,要再枚举一层质数层,
里面的式子就可以分解出来,
先要知道一个经典的反演问题,就是1~a区间,1~b区间,有多少点对
满足互质条件,这样的求和最终是要进行容斥的,(容斥系数就是莫比乌斯函数),
那么本题也是一样的。
因为数学公式不会打出,详见https://blog.csdn.net/luyehao1/article/details/81672837的分析。

*/

const int  maxn =1e6+5;
const int mod=998244353;
///莫比乌斯反演配合筛法
int miu[maxn],phi[maxn];///莫比乌斯系数筛,欧拉函数预处理
int prim[maxn],vis[maxn],cnt=0;///素数筛
ll a[maxn],inv[maxn];///a数,逆元打表
void sieve(int N)
{
    memset(miu,0,sizeof(miu));
    memset(vis,0,sizeof(vis));
    miu[1]=phi[1]=1;
    for(int i=2;i<=N;i++)
    {
        if(!vis[i])
        {
            prim[cnt++]=i;
            miu[i]=-1;
            phi[i]=i-1;
        }
        for(int j=0;j<cnt;j++)
        {
            ll k=1LL*prim[j]*i;
            if(k>N) break;
            vis[k]=1;
            if(i%prim[j])
            {
                miu[k]=-miu[i];
                phi[k]=phi[i]*(prim[j]-1);
            }
            else
            {
                miu[k]=0;
                phi[k]=phi[i]*prim[j];
                break;
            }
        }
        ///miu[i]+=miu[i-1];
    }
}

int n,m,p,ub;
///初始化a的值和逆元的值
void init()
{        ///inv[i] = 1LL * inv[mod%i]*(mod-mod/i)%mod;
    inv[1]=1; for(int i=2;i<=ub;i++) inv[i]=1LL*inv[p%i]*(p-p/i)%p;
    for(int i=1;i<=ub;i++)  a[i]=1ll*i*inv[phi[i]]%p;///p是输入的模
}
///ll a(ll i) { return i/phi[i]; }
int main( )
{
    sieve(1000005);
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&p);
        ub=min(n,m);
        init();

        ll ans=0;
        for(int i=1;i<=ub;i++)
        {
            int tp=0;
            int tp1=n/i,tp2=m/i,lb=min(tp1,tp2);
            for(int j=1;j<=lb;j++)  tp=(tp+1LL*miu[j]*(tp1/j)*(tp2/j)+p)%p;
            ans=(ans+a[i]*tp)%p;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81949463