hdu 1695 GCD(莫比乌斯)
2015-04-29 11:14
351 查看
GCD
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
Sample Output
Source
2008 “Sunline Cup”
National Invitational Contest
题意:
给你5的数a,b,c,d,k ,其中a和c默认为1,求[a,b]和[c,d]两个区间有多少对数(x,y)使得gcd(x,y)==k,其中x属于[a,b],y属于[c,d]。
容斥原理解法:
这个问题可以转换为x,y互质问题,因为gcd(x,y)==k,则有gcd(x/k,y/k)==1,x>k,y>k;
这样就是求[1,b/k]和[1,d/k]互质的对数了。
假设d>=b,则区间[1,d]可分为[1,b]U[b+1,d],两个区间分别对[1,b]求符合条件的对数。
[1,b]和[1,b]可以用欧拉函数求,[1,b]和[b+1,d]用容斥原理求。
莫比乌斯反演:
刚学莫比乌斯函数,只会gcd(x,y)==1对,感觉他就是模拟容斥原理的。
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).
Source
2008 “Sunline Cup”
National Invitational Contest
题意:
给你5的数a,b,c,d,k ,其中a和c默认为1,求[a,b]和[c,d]两个区间有多少对数(x,y)使得gcd(x,y)==k,其中x属于[a,b],y属于[c,d]。
容斥原理解法:
这个问题可以转换为x,y互质问题,因为gcd(x,y)==k,则有gcd(x/k,y/k)==1,x>k,y>k;
这样就是求[1,b/k]和[1,d/k]互质的对数了。
假设d>=b,则区间[1,d]可分为[1,b]U[b+1,d],两个区间分别对[1,b]求符合条件的对数。
[1,b]和[1,b]可以用欧拉函数求,[1,b]和[b+1,d]用容斥原理求。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> typedef long long ll; using namespace std; ll phi[100010]; ll ans; ll solve(ll a,ll n) { vector<ll>vet; for(ll i=2; i*i<=n; i++) { if(n%i==0) { vet.push_back(i); while(n%i==0) { n/=i; } } if(n==1) break; } if(n!=1) vet.push_back(n); ll sum=0; for(ll i=1; i<(1<<vet.size()); i++) { ll sets=0; ll mult=1; for(ll ii=0; ii<vet.size(); ii++) { if(i&(1<<ii)) { sets++; mult*=vet[ii]; } } sum+=sets%2?(a/mult):(-a/mult); } return a-sum; } void phi_table(ll n,ll *phi) { ///欧拉函数值表 for(ll i=2; i<=n; i++) phi[i]=0; phi[1]=1; for(ll i=2; i<=n; i++) { if(!phi[i]) { for(ll j=i; j<=n; j+=i) { if(!phi[j]) phi[j]=j; phi[j]=phi[j]/i*(i-1); } } } } int main() { ll a,b,c,d,k; phi_table(100010,phi); for(ll i=2; i<=100000; i++) phi[i]+=phi[i-1];///前缀和 int t; while(cin>>t) { int ca=1; while(t--) { cin>>a>>b>>c>>d>>k; if(k==0 || k > b || k > d) { printf("Case %d: 0\n",ca++); continue; } b/=k,d/=k; if(b>d) swap(b,d); ll tem=0; for(ll i=b+1; i<=d; i++) tem+=solve(b,i); printf("Case %d: %I64d\n",ca++,phi[b]+tem); } } return 0; }
莫比乌斯反演:
刚学莫比乌斯函数,只会gcd(x,y)==1对,感觉他就是模拟容斥原理的。
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<iostream> #define ll long long #define N 100050 using namespace std; bool check ; int prime ; int mu ; void Moblus() { memset(check,false,sizeof check); mu[1]=1; int tot=0; for(int i=2; i<N; i++) { if(!check[i]) { prime[tot++]=i; mu[i]=-1; } for(int j=0; j<tot; j++) { if(i*prime[j]>N)break; check[i*prime[j]]=true; if(i%prime[j]==0) { mu[i*prime[j]]=0; break; } else mu[i*prime[j]]=-mu[i]; } } } int sum ; ll solve(int n,int m) { ll ans=0; for(int i=1,la=0; i<=n; i=la+1) { la=min(n/(n/i),m/(m/i)); ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(m/i); } return ans; } int main() { //freopen("in.txt","r",stdin); Moblus(); sum[0]=0; for(int i=1; i<N; i++) { sum[i]=sum[i-1]+mu[i]; } int a,b,c,d,k; int t; cin>>t; int ca=1; while(t--) { scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); if(k==0) { printf("Case %d: 0\n",ca++); continue; } b/=k; d/=k; if(b>d)swap(b,d); ll ans=solve(b,d); ans-=solve(b,b)/2;//去重 printf("Case %d: %I64d\n",ca++,ans); } }
相关文章推荐
- HDU 1695 GCD 莫比乌斯,容斥原理
- HDU 1695 GCD【莫比乌斯反演】
- HDU 莫比乌斯反演 1695 GCD
- 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 [莫比乌斯反演]
- 【数论-莫比乌斯入门】hdu 1695 GCD
- hdu 1695 GCD 莫比乌斯
- hdu 1695: GCD 【莫比乌斯反演】
- hdu 1695 GCD(容斥原理+欧拉phi函数)
- HDU 1695 GCD 【数论,容斥原理】
- HDU 1695 GCD(莫比乌斯反演)
- HDU1695 GCD(容斥原理)