hdoj4893Goffi and GCD【欧拉函数】
2016-02-17 15:21
627 查看
Goffi and GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 951 Accepted Submission(s): 319
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 1
3 2
Sample Output
2
1
Hint
For the first case, (2, 1) and (1, 2) satisfy the equality.
Source
BestCoder Round #6
题意: 给你 N 和 K,问有多少个数对满足 gcd(N-A, N) * gcd(N - B, N) = N^K
分析: 由于 gcd(a, N) <= N,于是 K>2 都是无解,K=2 只有一个解 A=B=N,只要考虑 K = 1 的情况就好了 其实上式和这个是等价的 gcd(A, N) * gcd(B, N) = N^K,我们枚举 gcd(A, N) = g,那么gcd(B, N) = N / g。问题转化为统计满足 gcd(A, N) = g 的 A 的个数。这个答案就是 ɸ(N/g) 只要枚举 N 的 约数就可以了。
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<vector> #define MOD 1000000007 using namespace std; const int maxn=10010; long long Euler(long long n){ long long ret=1,i; for(i=2;i*i<=n;++i){ if(n%i==0){ n/=i;ret*=i-1; while(n%i==0){ n/=i;ret*=i; } } } if(n>1)ret*=n-1; return ret; } int main() { long long n,k,i,j; while(scanf("%lld%lld",&n,&k)!=EOF){ if(n==1||k==2){ printf("1\n"); } else if(k>2){ printf("0\n"); } else { long long ans=0; for(i=1;i*i<=n;++i){ if(n%i==0){ long long num=Euler(i)*Euler(n/i)%MOD; if(i*i==n){ ans+=num; } else { ans=ans+num*2; } ans%=MOD; } } printf("%lld\n",ans); } } return 0; }
相关文章推荐
- Varnish Install And Configure
- Insert Date and Time into Access
- asp的SQL语句中and和or同时使用的注意事项
- 拖动Html元素集合 Drag and Drop any item
- 学习drag and drop js实现代码经典之作
- .NET垃圾回收器(GC)原理浅析
- 深入理解PHP原理之Session Gc的一个小概率Notice
- And,Where使用提示以及用+进行左关联的提示及注意事项
- 浅析Java中的GC垃圾回收器的意义及与GC的交互
- 浅谈关于Java的GC垃圾回收器的一些基本概念
- Android开发adb.exe' and can be executed.错误解决方法
- Table ‘xxx’ is marked as crashed and should be repaired 错误解决方法参考
- Android垃圾回收机制及程序优化System.gc
- drag-and-drop实现图片浏览器预览
- 从JVM的内存管理角度分析Java的GC垃圾回收机制
- Java中垃圾回收器GC对吞吐量的影响测试
- 全面解析Java中的GC与幽灵引用
- python关键字and和or用法实例
- Python and、or以及and-or语法总结
- Python中AND、OR的一个使用小技巧