hdu 4983 Goffi and GCD(欧拉函数)

Problem Description

Goffi is doing his math homework and he finds an equality on his text book: gcd(na,n)×gcd(nb,n)=nk.

Goffi wants to know the number of (a,b) satisfy the equality, if n and k are given and 1a,bn.

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 (1n,k109).
 
Output
For each test case, output a single integer indicating the number of (a,b) modulo 109+7.
 
Sample Input
2 1 
3 2
 
Sample Output
2
1

Hint
For the first case, (2, 1) and (1, 2) satisfy the equality.
 
Source
 发现自己欧拉函数都给忘记了,所有赶紧补题。。。
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时个数,因为a<=n,所以gcd(a,n)的个数=u[n/x],u是欧拉函数。所以原式等于sigma(u[n/x]*u[x])其中x是n的约数。
 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
");
33             continue;
34         }
35         if(k>2)
36         {
37             printf("0
");
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
",ans);
53 
54     }
55     return 0;
56 }
View Code
原文地址:https://www.cnblogs.com/UniqueColor/p/4735021.html