@HDU多校训练7-1005@HDU6390 GuGuFishtion (欧拉函数 + 容斥|莫比乌斯)

GuGuFishtion

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


 

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:

 

@HDU多校训练7-1005@HDU6390 GuGuFishtion (欧拉函数 + 容斥|莫比乌斯)_第1张图片

 


And now you, the xxxxxxx little guy, have to solve the problem below given m,n,p.’

@HDU多校训练7-1005@HDU6390 GuGuFishtion (欧拉函数 + 容斥|莫比乌斯)_第2张图片


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

[题意]:  很明确  求 @HDU多校训练7-1005@HDU6390 GuGuFishtion (欧拉函数 + 容斥|莫比乌斯)_第3张图片

 

[思路]  根据 欧拉函数的性质   

ϕ(m) = p^k - p^{k-1}\Phi (m) = p^k - p^{k-1} = (p-1)p^{k-1} ; \Phi (mn ) = \Phi (m)\Phi (n)   

然后可以化简   

成 求 \frac{gcd(a,b)}{\Phi(gcd(a,b)) }

具体推导 : (参考的  (●'◡'●).jpg ) 

一个数肯定能表示成若干个质数的乘积,因此,设gcd(a,b)=p1^{k1}*p2^{k2}*p3^{k3}*...*pn^{kn}

Gu(a,b)=\frac{\phi (a,b)}{\phi (a)*\phi (b)}

 =\frac{\phi (p1^{2k1}*p2^{2k2}*p3^{2k3}*...*pn^{2kn})}{\phi (p1^{k1}*p2^{k2}*p3^{k3}*...*pn^{kn})^{2}} (其余的项上下展开后都可以约掉,因为它们互质)

=\frac{(p1-1)*(p2-1)*(p3-1)*...*(pn-1)*p1^{2k1-1}*p2^{2k2-1}*p3^{2k3-1}*...*pn^{2kn-1}}{(p1-1)^{2}*(p2-1)^{2}*(p3-1)^{2}*...*(pn-1)^{2}*p1^{2k1-2}*p2^{2k2-2}*p3^{2k3-2}*...*pn^{2kn-2}}

=\frac{p1*p2*p3*..*pn}{(p1-1)*(p2-1)*(p3-1)*...*(pn-1)}

=\frac{p1*p2*p3*...*pn*p1^{k1-1}*p2^{k2-1}*p3^{k3-1}*...*pn^{kn-1}}{(p1-1)*(p2-1)*(p3-1)*...*(pn-1)*p1^{k1-1}*p2^{k2-1}*p3^{k3-1}*...*pn^{kn-1}}

=\frac{p1^{k1}*p2^{k2}*p3^{k3}*...*pn^{kn}}{\phi (p1^{k1}*p2^{k2}*p3^{k3}*...*pn^{kn})}=\frac{gcd(a,b)}{\phi (gcd(a,b))}

////////////////////////////////////////

然后:

提前预处理下 phi 欧拉表,

然后变成求  a-b  中  gcd 为 i 的 有序对个数 

 

[代码]

#include 
#include 

#define rep(i,a,n) for(int i =a ;i<=n;i++)
#define per(i,a,n) for(int i =n ;i>=a;i--)


typedef long long ll;
const int maxn = 1e6+10;



using namespace std;

int phi[maxn+10];
ll mod;
ll inv[maxn+10];
ll f[maxn];
void init()
{
    memset(phi,0,sizeof(phi));
    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()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ll n,m;
        scanf("%lld %lld %lld",&n,&m,&mod);
        ll mn = min(n,m);
        inv[1] = 1;
        ll ans = 0;
        for(int i = 2 ;i < mn ; i++) inv[i] = (mod-mod/i)*inv[mod%i]%mod;
        memset(f,0,sizeof(f));
        for(int i = mn;i >=1 ;i--)
        {
            f[i] = 1ll*(n/i)*(m/i)%mod;
            for(int j = i+i; j <= mn;j+=i)
            {
                f[i] -= f[j];
                if(f[i] < 0) f[i] +=mod;
            }
            ans += i*inv[phi[i]]%mod * f[i]%mod;
            ans %=mod;
        }
        printf("%lld\n",ans%mod);
    }
    return 0;
}

@HDU多校训练7-1005@HDU6390 GuGuFishtion (欧拉函数 + 容斥|莫比乌斯)_第4张图片

你可能感兴趣的:(******,数论,******,ACM进阶之路)