hdu 1695 GCD【容斥+欧拉】
来源:互联网 发布:淘宝 广告 编辑:程序博客网 时间:2024/05/21 09:55
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9524 Accepted Submission(s): 3544
Problem Description
Given 5 integers: a, b, c, d, k, you’re to find x in a…b, y in c…d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you’re only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
Output
For each test case, print the number of choices. Use the format in the example.
Sample Input
2
1 3 1 5 1
1 11014 1 14409 9
Sample Output
Case 1: 9
Case 2: 736427
Hint
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
题意:
T组样例,a,b,c,d,k,a,c都为1,在(a~b)和(c~d)区间内分别找一个数字,组成GCD(n,m)=k,求有多少组,(5,7)和(7,5)只算一个;
思路:
wa到爆,一气之下全部——int64。这两个区间的GCD组合数,GCD=k,那么必定是k的倍数,先预处理把b,d除以k得到p1,p2,既然是从两个区间内选值,先算重合的部分,再算p2-p1的部分;第一部分就是
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define max_n 100010typedef long long LL;using namespace std;__int64 eul[max_n],arr[max_n],p[max_n];__int64 cnt=0;void euler(){ eul[1]=1; for(__int64 i=2;i<max_n;i++) { if(!eul[i]) { for(__int64 j=i;j<max_n;j+=i) { if(!eul[j]) eul[j]=j; eul[j]=eul[j]/i*(i-1); } } } __int64 ans=0; for(__int64 i=1;i<max_n;i++) { ans+=eul[i]; arr[i]=ans; }}void getp(__int64 n){ for(__int64 i=2;i*i<=n;i++) { if(n%i==0) { p[cnt++]=i; while(n%i==0) n/=i; } } if(n>1) p[cnt++]=n;}__int64 nop(__int64 m) //1~m里有多少与getp(n)中的n互质 { __int64 que[max_n],top; __int64 sum=0; que[top++]=-1; for(__int64 i=0;i<cnt;i++) { __int64 t=top; for(__int64 j=0;j<t;j++) { que[top++]=que[j]*p[i]*(-1); } } for(__int64 i=1;i<top;i++) { sum+=m/que[i]; } return sum;}int main(){ euler(); __int64 t,a,b,c,d,k,p1,p2; scanf("%I64d",&t); while(t--) { __int64 sum=0; scanf("%I64d %I64d %I64d %I64d %I64d",&a,&b,&c,&d,&k); static int p=1; printf("Case %d: ",p++); if(k==0) //注意k==0时 { printf("0\n"); continue; } p1=min(b,d)/k; p2=max(b,d)/k; sum=arr[p1];// printf("%d %d %d\n",sum,p1,p2); for(__int64 i=p1+1;i<=p2;i++) { cnt=0; getp(i); sum+=p1-nop(p1); } printf("%I64d\n",sum); } return 0;}
- hdu 1695 GCD 容斥+欧拉
- hdu 1695 GCD【容斥+欧拉】
- HDU/HDOJ 1695 GCD 欧拉函数+容斥原理
- HDU 1695 GCD 容斥原理+欧拉
- hdu 1695 GCD 欧拉方程 容斥理论
- HDU 1695 GCD 欧拉函数+容斥原理
- Hdu 1695 GCD - 欧拉函数 + 容斥
- hdu 1695(GCD) 容斥+欧拉函数
- hdu 1695 GCD 欧拉函数+容斥原理
- HDU 1695 GCD(欧拉函数+容斥原理)
- hdu 1695 GCD(容斥原理+欧拉函数)
- hdu 1695 GCD(欧拉函数+容斥原理)
- 【HDU】1695 GCD 欧拉函数+容斥原理
- hdu 1695 GCD 欧拉函数+容斥原理
- HDU 1695 GCD 欧拉函数+容斥定理
- hdu 1695 GCD(容斥原理+欧拉phi函数)
- HDU 1695 GCD(欧拉函数+容斥原理)
- hdu 1695 GCD【欧拉函数+容斥原理】
- mvn assembly:assembly出现错误信息解决方案
- Oracle sys用户登录权限不足insufficient privileges
- 激活win10系统
- 显示类型转换
- Linux C/C++ 多线程死锁的gdb调试方法
- hdu 1695 GCD【容斥+欧拉】
- PHP的输出:echo 和 print 语句
- 如何使用阿里云虚拟主机搭建博客(二)搭建篇
- C#之实现Scoket心跳机制
- 1775_采药
- 个人小程序智能对话查询工具完整代码分享--快递、身份证、词典、诗词等
- Keras学习(1)-识别手写体的例子
- 机房重构总结
- 烦