1. 程式人生 > >【2018 Multi-University Training Contest 7】GuGuFishtion(莫比烏斯反演)

【2018 Multi-University Training Contest 7】GuGuFishtion(莫比烏斯反演)

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=1mb=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)<p1,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
窩的做法似乎很麻煩啊。沒有看出像題解那樣的。
先考慮

ϕ(ab)ϕ(a)ϕ(b)
當然a和b互質的話當然為1,如果不互質麼,根據尤拉函式的公式,顯然有
111p1111p2...111pk

p1到pk是a和b公有的不同質因子。這個似乎和有gcd有點關係但是又不太一樣。
假設gcd是4,但是用到的是2,gcd是12,用到的是6,即2*3,
那麼我們先來考慮這個東西,就是假設x是若干單個質因子的乘積。那麼上個表示式顯然就是xϕ(x)
窩設l(x)=pp|xp

那麼假設我們列舉每個gcd,那麼對應要乘的東西就是

l(x)ϕ(l(x))
窩設為g(x),即
g(x)=l(x)ϕ(l(x))
顯然這個函式是積性函式就可以篩了,
然而根本不用那麼麻煩,實際上g(x)=xϕ(x)
窩當時竟然沒有看出來,現在才想起來。不過去篩的時候可以完全忽略這些,因為g就是積性函式,不管哪種理解,對篩g這個函式沒有影響。
所以現在我們就要去列舉每個gcd的貢獻了,

那麼

ans=ijg(gcd(i,j))
把gcd拿出來:
ans=k=1g(k)ijgcd(i,j)==k
那麼就是求gcd貢獻的經典套路。
f(x)為gcd為x的個數,F(x)為gcd是x的倍數
反演一下: