GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5064    Accepted Submission(s): 1818

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
 
Recommend
wangye   |   We have carefully selected several similar problems for you:  1689 1691 1692 1697 1696 
 

参考: http://blog.csdn.net/xiaotaoqibao/article/details/5772486

思路:

题目意思不难已知给定k,x,y求 1<=a<=x 1<=b<=y 中满足 gcd(a,b)=k 的(a,b)对数。(注意数对是无序的)。 1<=x,y<=10w, 0<=k<=10w

题目有比较恶心的一点,数据有k==0的,这时显然答案是0,没有2个数的gcd为0。

首先,gcd是没啥用的。因为约掉gcd后两个数互质。于是我们可以让x/=k y/=k并且假设 x<=y

然后题目变成了 2个数分别在区间[1..x]和[1..y]中的互质数有多少对。

大体思路:

枚举[1..y]中每个数i 判断[1..min(x,i)]中有多少数与i互质,统计个数。(注意,枚举的是比较大的区间[1..y])。

显然如果i是质数,则[1..min(x,i)]中与i互质的个数是全体的个数或者i-1个。(取决于x和i的大小)。

当i不是质数时,i分解质因数后,质因数的次数不影响结果。我们看另外那个区间有多少个和i不互质(减一下就好了),于是我们只要看另外那个区间中有多少个数是i质因数的倍数就好了。

区间[1..w]中 p的倍数 显然有 w/p个。

我们枚举i的质因数利用容斥原理:

看另外那个区间有多少个数与i不互质。

容斥原理的具体如下:

区间中与i不互质的个数 = (区间中i的每个质因数的倍数个数)-(区间中i的每两个质因数乘积的倍数)+(区间中i的每3个质因数的成绩的倍数个数)-(区间中i的每4个质因数的乘积)+...

于是问题变成了统计每个数的不同质因数的个数而忽略次数。这个可以用筛法。具体做法如下:

对每个数保存一个真质因数的列表。初始每个列表的长度为0。然后从2开始,分别检查每个数的列表长度,如果列表长度不为0,则这个数是合数,跳过;如果这个长度为0,则我们找到了一个质数,同时再把这个数的倍数(不包含本身)的列表里加入这个数。

这样筛一次下来,我们保存了每个数的真质因数列表,问题得到解决,还要注意结果用要用__int64。

 ///218MS    7256K    1385 B    G++
//容斥原理+欧拉函数
#include<stdio.h>
#include<string.h>
#include<string.h>
#define N 100005
int ss[N][]; //质因数
int num[N]; //不同质因数个数
__int64 euler[N]; //euler[i]:[1,i]的欧拉数和
void init()
{
memset(ss,,sizeof(ss));
memset(euler,,sizeof(euler));
euler[]=;
for(int i=;i<N;i++){
if(!euler[i]){ //质数
for(int j=i;j<N;j+=i){
if(!euler[j]) euler[j]=j;
euler[j]=euler[j]*(i-)/i;
ss[j][num[j]++]=i; //记录质因数
}
}
euler[i]+=euler[i-];
//printf("*%d %d\n",i,euler[i]);
}
}
__int64 dfs(int a,int b,int q) //容斥原理
{
__int64 res=;
for(int i=a;i<num[q];i++){
res+=b/ss[q][i]-dfs(i+,b/ss[q][i],q);
}
return res;
}
int main(void)
{
int t,cas=;
int a,b,c,d,k;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==){
printf("Case %d: 0\n",cas++);continue;
}
b/=k;
d/=k; //题目变成[1,b]与[1,d]间的互质的数有多少对
if(b>d){
int temp=b;b=d;d=temp;
}
__int64 res=euler[b];
for(int i=b+;i<=d;i++){
res+=b-dfs(,b,i);
}
printf("Case %d: %I64d\n",cas++,res);
}
return ;
}

hdu 1695 GCD (欧拉函数+容斥原理)的更多相关文章

  1. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

  2. HDU 1695 GCD (欧拉函数,容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

  4. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  6. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  7. [hdu1695] GCD ——欧拉函数+容斥原理

    题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...

  8. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  9. HDU 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. 模块importlib介绍

    importlib包的目的是双重的.一个是在Python源代码中提供import语句(以及扩展名为__import__()函数)的实现.这提供了可以移植到任何Python解释器的import的实现.这 ...

  2. mysql数据库的基本使用命令总结

    mysql数据库是一个常用的关系型数据库 关系型数据库核心元素有哪些? 主键:特殊字段,用来唯一标识记录的唯一性 字段:数据列 记录:数据行 数据表:数据行的集合 数据库:数据表的集合 安装.启动.停 ...

  3. 码云配置webhooks自动触发拉取代码

    webhooks的使用 码云和github的钩子叫webhooks 每次您 push 代码后,都会给远程 HTTP URL 发送一个 POST 请求 码云项目管理页面的webhooks设置: http ...

  4. hack游戏攻略(梦之光芒黑客小游戏)

    2019.2.11 继续玩~~还是黑客游戏闯关类的 地址:http://monyer.com/game/game1/ 直接查看页面代码: first.php就是了: 查看源代码: 这里尝试输入 两个空 ...

  5. Windows10 快捷键

    windows 10快捷键: F1 打开帮助 F2 重命名 F3 打开搜索文件和文件夹 F4 打开地址栏常用的地址 F5 刷新 F11   全屏 选择文件和内容: shift + 上下左右键选择连续的 ...

  6. Go语言中的UDP应用

    Go语言中的UDP应用 Go语言中使用UDP是很方便的,net包提供了UDP和TCP的功能,这里使用UDP做了一个UDP广播,然后接收各个设备的返回信息.实现起来很快,总体感觉比使用C#中的UDP更优 ...

  7. ibdata1文件损坏时恢复InnoDB单表测试

      Preface       ibdata1 file is a shared system tablespace of innodb engine.Although we always set v ...

  8. oracle12c 新建表空间

    第1步:创建临时表空间 create temporary tablespace jeeplus_temp tempfile 'D:\app\Administrator\virtual\product\ ...

  9. 类和实例属性的查找顺序 mro查找

    如果多个类继承父类,然后又被多个类继承这种复杂的问题,可以使用 mro方法 例如: class A: pass class C(D): pass class B(D): pass class A(B, ...

  10. docker简单命令

    查看镜像 docker images 查看正在使用的容器 docker ps -a 进入容器控制台 docker exec -it 容器ID bash 启动镜像 docker run -it -d.. ...