hdu
来源:互联网 发布:网络女主播圈子很乱 编辑:程序博客网 时间:2024/06/05 20:38
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).
Source
2008 “Sunline Cup” National Invitational Contest
题解
- 要求gcd(x,y)=k,转化为gcd(x/k,y/k)=1,即缩小区间为1~b/k,1~d/k.下面都是b<=d为例.b/=k,d/=k.
- 对于y∈[1-b]区间,我们直接求x∈[1,b]内每个数与y属于互质的个数,累加起来即可。
- 对于y∈[b+1,d]区间,我们就要用到容斥原理,我们要求x∈[1,b]与y互质个数,反过来就是求b-所有x∈[1,b]与y不互质的个数。
- 特别的,如果k=0,不存在gcd(x,y)=0,输出答案0。
#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <cstring>using namespace std;typedef long long LL;const int MAXN=100005;int euler[MAXN],fac[1005];void InitEuler()//筛法欧拉函数{//对[1~100000]的互质个数打表。 memset(euler,0,sizeof(euler)); euler[1]=1; for(int i=2;i<MAXN;i++){ if(!euler[i]){ for(int j=i;j<MAXN;j+=i){ if(!euler[j]) euler[j]=j; euler[j]=euler[j]/i*(i-1); } } }}LL cal(int b,int N)//计算x[1,b]与N互质的个数{ int n=N; int i,j,cnt=0; for(i=2;i*i<=n;i++){ if(n&&n%i==0){ fac[cnt++]=i; while(n&&n%i==0) n/=i; } } if(n>1) fac[cnt++]=n; LL ans=0;//得到在[1,b]中gcd(N,[1,b])>1的个数 for(i=1;i<(1<<cnt);i++){ LL mult=1,ones=0; for(j=0;j<cnt;j++){ if(i&(1<<j)){ ones++; mult*=fac[j]; } } if(ones%2) ans+=b/mult; else ans-=b/mult; } return b-ans;//得到gcd(N,[1,b])=1的个数}int main(){ //freopen("E:\\debug.txt","r",stdin); InitEuler(); int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++){ LL ans=0; int a,b,c,d,k; scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); printf("Case %d: ",cas); if(k==0||k>b||k>d){ printf("0\n");continue; } b/=k;d/=k; if(b>d) swap(b,d); for(int i=1;i<=b;i++) ans+=euler[i]; for(int i=b+1;i<=d;i++) ans+=cal(b,i); printf("%lld\n",ans); } return 0;}
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- win7下批处理bat文件:切换网络设置
- 3.2—字符串—Implement strStr()
- Redis---- php简单操作
- QuartZ Cron表达式
- Charles辅助调试接口
- hdu
- 初学linux,在Linux上创建c程序并运行!
- 单例模式还是静态类
- java基础复习-多线程UDP
- HDU6073 Matching In Multiplication【拓扑】
- 穆年小菜鸟的日常随记——socket+php聊天室
- springMvc拦截器的使用
- SQLAlchemy 和其他的 ORM 框架
- 3.3—字符串—String to Integer (atoi)