hdu 1695 GCD(容斥原理+欧拉phi函数)
来源:互联网 发布:带眼镜的网络男歌手 编辑:程序博客网 时间:2024/05/07 07:04
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.
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.
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
21 3 1 5 11 11014 1 14409 9
Sample Output
Case 1: 9Case 2: 736427HintFor 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]用容斥原理求。
My Code:
#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;}
0 0
- hdu 1695 GCD(容斥原理+欧拉phi函数)
- 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(欧拉函数+容斥原理)
- HDU 1695 GCD (欧拉函数+容斥原理)
- HDU 1695 GCD(容斥原理 + 欧拉函数)
- HDU 1695 GCD (欧拉函数,容斥原理)
- Linux的.a、.so和.o文件
- 如何用Spingmvc 来对接和验证微信开发
- HDU 2054 A == B ? 字符串处理
- Linux异步通知fasync
- HDU —— 1161 Eddy's mistakes
- hdu 1695 GCD(容斥原理+欧拉phi函数)
- HDU 1789 Doing Homework again 贪心
- Java MySQL数据库编程详解
- Java由基础到高级面试题经典问答
- Eclipse 常用快捷键 (动画讲解)
- opencv配置
- YAML简单入门
- 【可持久化平衡树】Version Controlled IDE
- 学习编程有那么难吗?