博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1695 GCD(欧拉函数+容斥)
阅读量:4311 次
发布时间:2019-06-06

本文共 2625 字,大约阅读时间需要 8 分钟。

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
 
题意:给定a,b,c,d,k,要求从a到b选出一个数i,从b到d中选出一个数j,使得gcd(i,j)=k,求总方案数
 
思路:

第一个区间:[1,2,...,b/k] 第二个区间:[b/k+1,b/k+2,...,d/k]

读第一个区间我们只要利用欧拉函数求质因数的个数即可,第二个区间我们任取x,
要求[1,2,...,b/k]中所有与x互质的数的个数,这里我们用到容斥原理:先将x质因数分解,
求得[1,2,...,b/k] 里所有能被x的质因数整除的数的个数,然后用b/k减去即可。

 

1 #include
2 #include
3 #include
4 using namespace std; 5 #define N 100006 6 #define ll long long 7 ll a,b,c,d,k; 8 ll fac[N]; 9 ll eular(ll n)10 {11 ll res=1;12 for(ll i=2;i*i<=n;i++)13 {14 if(n%i==0)15 {16 n/=i,res*=i-1;17 while(n%i==0)18 {19 n/=i;20 res*=i;21 }22 }23 }24 if(n>1)25 res*=n-1;26 return res;27 }28 ll solve()29 {30 ll ans=0;31 for(ll i=b+1;i<=d;i++)32 {33 ll n=i;34 ll num=0;35 ll cnt=0;36 for(ll j=2;j*j<=n;j++)37 {38 if(n%j==0)39 {40 fac[num++]=j;41 while(n%j==0)42 {43 n/=j;44 }45 }46 }47 if(n>1) fac[num++]=n;48 49 for(ll j=1;j<(1<
d)83 swap(b,d);84 b/=k;85 d/=k;86 //printf("---%d %d\n",b,d);87 ll ans=0;88 for(ll i=1;i<=b;i++)89 {90 ans+=eular(i);91 }92 //printf("-%d\n",ans);93 ans=ans+solve();94 printf("%I64d\n",ans);95 }96 return 0;97 }
View Code

 

 

转载于:https://www.cnblogs.com/UniqueColor/p/4734907.html

你可能感兴趣的文章
解决vmware与主机无法连通的问题
查看>>
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>
获取设备实际宽度
查看>>
图的算法专题——最短路径
查看>>
SQL批量删除与批量插入
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
C语言之一般树
查看>>
懂了很多大道理,却依旧过不好一生
查看>>
手工数据结构系列-C语言模拟队列 hdu1276
查看>>
【PyQt5 学习记录】008:改变窗口样式之二
查看>>
android EditText长按屏蔽ActionMode context菜单但保留选择工具功能
查看>>
BUAA 111 圆有点挤
查看>>
c++ 继承产生的名字冲突问题 (1)
查看>>
SQL中on条件与where条件的区别
查看>>
Ubuntu下查看软件版本及安装位置
查看>>
安装IIS
查看>>
动态加载JS(转)
查看>>