hdu 4983 Goffi and GCD(欧拉函数)
生活随笔
收集整理的這篇文章主要介紹了
hdu 4983 Goffi and GCD(欧拉函数)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Problem Description
Goffi is doing his math homework and he finds an equality on his text book:?gcd(n?a,n)×gcd(n?b,n)=nk.Goffi wants to know the number of (a,b) satisfy the equality, if?n?and?k?are given and?1≤a,b≤n.
Note:?gcd(a,b)?means greatest common divisor of?a?and?b.
?
Input Input contains multiple test cases (less than 100). For each test case, there's one line containing two integers?n?and?k?(1≤n,k≤109).?
Output For each test case, output a single integer indicating the number of (a,b) modulo?109+7.?
Sample Input 2 13 2
?
?
Sample Output 2 1?
Hint For the first case, (2, 1) and (1, 2) satisfy the equality.
?
Source BestCoder Round #6 ?發(fā)現(xiàn)自己歐拉函數(shù)都給忘記了,所有趕緊補(bǔ)題。。。 1、k!=1時情況很簡單,記住將if(k==2 || n==1)這個特判放在if(k>2)的前面,因為這個WA了很久,各種原因自己思考。 2、下面討論k=1時情況。x=gcd(n-a,n),則n/x=gcd(n-b,n),因為n-a可以取到0...n-1也就是1....n,所以完全可以去掉n-這個限制條件,即gcd(a,n)=x、gcd(b,n)=n/x時個數(shù),因為a<=n,所以gcd(a,n)的個數(shù)=u[n/x],u是歐拉函數(shù)。所以原式等于sigma(u[n/x]*u[x])其中x是n的約數(shù)。?
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define MOD 1000000007 6 #define ll long long 7 ll eular(ll n) 8 { 9 ll res=1; 10 for(ll i=2;i*i<=n;i++) 11 { 12 if(n%i==0) 13 { 14 n/=i,res*=i-1; 15 while(n%i==0) 16 { 17 n/=i; 18 res*=i; 19 } 20 } 21 } 22 if(n>1) res*=n-1; 23 return res; 24 } 25 ll n,k; 26 int main() 27 { 28 while(scanf("%I64d%I64d",&n,&k)==2) 29 { 30 if(k==2 || n==1) 31 { 32 printf("1\n"); 33 continue; 34 } 35 if(k>2) 36 { 37 printf("0\n"); 38 continue; 39 } 40 41 ll ans=0; 42 for(ll i=1;i*i<=n;i++) 43 { 44 if(n%i==0) 45 { 46 if(i*i!=n) 47 ans=(ans+eular(n/i)*eular(i)*2)%MOD; 48 else 49 ans=(ans+eular(n/i)*eular(i))%MOD; 50 } 51 } 52 printf("%I64d\n",ans); 53 54 } 55 return 0; 56 } View Code?
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的hdu 4983 Goffi and GCD(欧拉函数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS中常见的内存问题
- 下一篇: 通过java.net.URLConnec