hdu 1695 GCD 求(1,b)和(1,d)中互素的数对的对数 求不大于b的数中与now不互素的数的个数(now>b) 容斥原理
2010-10-01 20:08
357 查看
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1507 Accepted Submission(s): 510
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 HintFor 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).用到了欧拉函数,素因子分解,筛选法,组合数学上的容斥原理等,也不失为一道好题!!!题目意思好懂,在[1...b]中选x,在[1....d]中选y,使gcd(x,y)=k,求不重复的对数有一个小小的变形:在[1...b/k]中选x,在[1....d/k]中选y,使gcd(x,y)=k,求不重复的对数我们让d>=b; 然后在[1....d/k]进行枚举,对于每一个i,我们只要在1...min(i-1,b)中找到与i互质数,记录个数,然后累加就得到结果了当i<=b/k时,我们可以直接用欧拉函数计算出与i互质的个数 (当然要先进行因子分解,才能求欧拉函数)当b/k<i<=d/k时,就比较难求了,我们用b/k减去与i不互质的数的个数得到,求与i不互质的数的个数时就用到容斥原理,设i的素因子分别的p1,p2...pk,则1..b/k中p1的倍数组成集合A1,p2的倍数组成集合A2,p3到A3.....pk到Ak, 由于集合中会出现重复的元素, 所以用容斥原理来求A1并A2并A3.....并Ak的元素的数的个数.进行素因子分解和求欧拉函数值都可通过筛选法来求, 进行容斥时,可以用dfs来求:本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liql2007/archive/2009/08/20/4464932.aspx#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int Max=100005; int a,b,c,d,k; __int64 elur[Max];//存放每个数的欧拉函数值 int num[Max];//存放数的素因子个数 int p[Max][10];//存放数的素因子 void init() { elur[1]=1; for(int i=2;i<Max;i++)//筛选法得到数的素因子及每个数的欧拉函数值 { if(!elur[i]) { for(int j=i;j<Max;j+=i) { if(!elur[j]) elur[j]=j; elur[j]=elur[j]*(i-1)/i; p[j][num[j]++]=i; } } } }__int64 dfs(int index,int b,int now)//求不大于b的数中与now不互素的数的个数(now>b) { __int64 res=0; for(int i=index;i<num[now];i++) { res+=b/p[now][i]-dfs(i+1,b/p[now][i],now); } return res; } int main() { init();//求欧拉函数并且将整数分解 for(int i=0;i<Max;i++) elur[i]+=elur[i-1]; int ci;scanf("%d",&ci); for(int pl=1;pl<=ci;pl++) { int a,b,c,d,k;//a=1,c=1; scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); printf("Case %d: ",pl); if(k==0) {printf("0/n");continue;} if(b>d) swap(b,d);//b<d b/=k,d/=k; __int64 cnt=elur[b]; for(int i=b+1;i<=d;i++) { cnt+=b-dfs(0,b,i);//求不大于b的数中,与i互素的数的个数 } printf("%I64d/n",cnt); } return 0; }
相关文章推荐
- 【HDU】1695 - GCD(欧拉函数 & 容斥原理)
- HDU 1695 GCD(gcd(x,y)=k无序对数/容斥原理)
- HDU 4466 Triangle (边长a+b+c=M的三角形个数->且gcd(a,b,c)=1的三角形个数)
- HDU 1695 GCD 【数论,容斥原理】
- HDU1695 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 (数论-整数和素数,组合数学-容斥原理)
- hdu1695 GCD(容斥原理+欧拉函数)
- HDU 1695 GCD(素因子分解+容斥原理+欧拉函数)
- HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)
- GCD HDU - 1695 容斥原理+欧拉函数打表
- hdu-1695 GCD(容斥原理+欧拉函数)
- hdu1695 GCD 容斥原理
- [ACM] HDU 1695 GCD (容斥原理)