HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)
2014-07-31 17:37
519 查看
GCD
Problem DescriptionGiven 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).
Source
2008 “Sunline Cup” National Invitational
Contest
题目大意:
从1~a区间取一个数x,从1~b区间取一个数y,问你gcd(x,y)=k有多少种方案?其中x1,y1和y1,x1算同一种方案。
解题思路:
那么就是 从1~b/k 取一个数x , 与 从1~d/k 取一个数y 互质的方案数,利用容斥,枚举 x,求出y的个数即可。
解题代码:
#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <map> #include <algorithm> using namespace std; typedef long long ll; const int maxn=450; bool isPrime[maxn]; vector <int> v; int tol,a,b,c,d,k; void get_prime(){ memset(isPrime,true,sizeof(isPrime)); for(int i=2;i<maxn;i++){ if(isPrime[i]){ tol++; v.push_back(i); } for(int j=0;j<tol && i*v[j]<maxn;j++){ isPrime[i*v[j]]=false; if(i%v[j]==0) break; } } } inline vector <int> getPrime(ll x){ vector <int> tmp; for(ll i=0;i<tol && x>=v[i]*v[i];i++){ if(x%v[i]==0){ tmp.push_back(v[i]); while(x>0 && x%v[i]==0) x/=v[i]; } } if(x>1) tmp.push_back(x); return tmp; } inline ll getCnt(int i,int y){//1-y与i不互质的数的个数 vector <int> tmp=getPrime(i); int vsize=tmp.size(); ll sum=0; for(int t=1;t<(1<<vsize);t++){ int cnt=0,all=1; for(int j=0;j<vsize;j++){ if(t&(1<<j)){ all*=tmp[j]; cnt++; } } if(cnt&1) sum+=y/all; else sum-=y/all; } return sum; } void solve(){ if(k==0 || k>b || k>d){ cout<<0<<endl; return; } int x=b/k,y=d/k; if(x<y) swap(x,y); ll ans=0; for(int i=1;i<=x;i++){ int r=i>y?y:i; ans+=r; ans-=getCnt(i,r); } cout<<ans<<endl; } int main(){ get_prime(); int t; scanf("%d",&t); for(int i=1;i<=t;i++){ scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); printf("Case %d: ",i); solve(); } return 0; }
相关文章推荐
- HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- hdu 1695 GCD(组合数学:容斥原理)
- Hdu 1695 GCD (数论 容斥原理)
- HDU 1695 GCD 【数论,容斥原理】
- hdu - GCD-1695 - 欧拉表+容斥 - 数论/组合学
- hdu 1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 帅呆了的一个题目 详解
- UVa 10290 {Sum+=i++} to Reach N (数论-整数和素数,组合数学-排列组合)
- hdu 1695 GCD (素数筛选 + 欧拉函数 + 容斥原理)
- 数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!
- 数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!
- HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)
- POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,整数快速幂)
- HDU 1796 How many integers can you find(组合数学-容斥原理)
- HDU 4497 GCD and LCM(拆素数+组合)
- HDU 1796 How many integers can you find(组合数学-容斥原理)
- hdu 1695 GCD(容斥原理+欧拉函数)
- HDU 1796 How many integers can you find(组合数学-容斥原理)
- POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,整数快速幂)
- HDU 1695 GCD(素因子分解+容斥原理+欧拉函数)