GCD

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

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
 
思路:题意可转化为求[1,b/k]与[1,d/k]组成数对(x,y)。x,y互质的对数。当x与y均不大于min(b/k,d/k)时,需要将答案除以2。
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN=;
typedef long long LL;
LL b,d,k;
vector<LL> divisor[MAXN];
void prep()
{
for(LL e=;e<MAXN;e++)
{
LL x=e;
for(LL i=;i*i<=x;i++)
{
if(x%i==)
{
divisor[e].push_back(i);
while(x%i==) x/=i;
}
}
if(x>) divisor[e].push_back(x);
}
}
LL sieve(LL m,LL n)
{
LL ans=;
for(LL mark=;mark<(<<divisor[n].size());mark++)
{
LL mul=;
LL odd=;
for(LL i=;i<divisor[n].size();i++)
{
if(mark&(<<i))
{
mul*=divisor[n][i];
odd++;
}
}
LL cnt=m/mul;
if(odd&) ans+=cnt;
else ans-=cnt;
}
return m-ans;
}
int main()
{
int T;
scanf("%d",&T);
prep();
for(int cas=;cas<=T;cas++)
{
scanf("%*d%lld%*d%lld%lld",&b,&d,&k);
printf("Case %d: ",cas);
if(k==)
{
printf("%d\n",);
continue;
}
b/=k;
d/=k;
if(b>d) swap(d,b);
LL res=;
for(LL i=;i<=b;i++)
{
LL cnt=sieve(b,i);
res+=cnt;
}
res=(res+)/;
for(LL i=b+;i<=d;i++)
{
LL cnt=sieve(b,i);
res+=cnt;
}
printf("%lld\n",res);
}
return ;
}

HDU1695(容斥原理)的更多相关文章

  1. hdu1695 容斥原理 莫比乌斯反演

    给定两个数b,d,问[1,b]和[1,d]区间上有多少对互质的数.(x,y)和(y,x)算一个. 对于[1,b]部分,用欧拉函数直接求.对于大于b的部分,求n在[1,b]上有多少个互质的数,用容斥原理 ...

  2. GCD hdu1695容斥原理

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

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

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

  4. HDU1695 GCD (欧拉函数+容斥原理)

    F - GCD Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. hdu1695 GCD 容斥原理

    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) ...

  6. HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to ...

  7. hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。

    GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...

  8. ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)

    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) = ...

  9. 容斥原理应用(求1~r中有多少个数与n互素)

    问题:求1~r中有多少个数与n互素. 对于这个问题由容斥原理,我们有3种写法,其实效率差不多.分别是:dfs,队列数组,位运算. 先说说位运算吧: 用二进制1,0来表示第几个素因子是否被用到,如m=3 ...

随机推荐

  1. php数组函数-array_diff()

    array_diff()函数返回两个数组的差集数组.该数组包括了所有在被比较数组中,但是不在任何其他参数数组中的键值在返回数组中,键名保持不变. array_diff(array1,array2,ar ...

  2. 20145230java实验报告二

    20145230实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 ...

  3. iOS清除缓存功能开发

    在APP开发中,大量的图片或消息占用系统内存,造成一堆垃圾信息,所以开发清除缓存功能就显得必不可少了. 代码段1:获取文件的大小 - (long long) fileSizeAtPath:(NSStr ...

  4. windows蓝屏错误小全

    作者:siyizhu 日期:2005-11-27 字体大小: 小 中 大  引用内容 0 0x00000000 作业完成.  1 0x00000001 不正确的函数.  2 0x00000002 系统 ...

  5. DP问题分类总结

    http://m.blog.csdn.net/y990041769/article/details/24194605 此博客总结了动态规划相关问题,学习一下!

  6. poj 1273 ---&&--- hdu 1532 最大流模板

    最近在换代码布局,因为发现代码布局也可以引起一个人的兴趣这个方法是算法Edmonds-Karp 最短增广路算法,不知道的话可以百度一下,基于Ford-Fulkerson算法的基础上延伸的 其实我不是很 ...

  7. shell中嵌套执行expect命令实例(利用expect实现自动登录)

    expect是 #!/bin/bashpasswd='123456'/usr/bin/expect <<EOFset time 30spawn ssh root@192.168.76.10 ...

  8. 关于发邮件报错535 Error:authentication failed&553 authentication is required

    553 authentication is required:这个错误的意思是你必须需要认证. 也就是说,你连接smtp服务器的时候必须使用密码来连接:下面代码红色那句 代码: @Override p ...

  9. How does asp.net web api work?

    https://hub.packtpub.com/working-aspnet-web-api/ https://docs.microsoft.com/en-us/aspnet/web-api/ove ...

  10. Luogu-3222 [HNOI2012]射箭

    几何题,二次函数,化一下式子吧 设二次函数\(y=ax^2+bx\),对于一个线段\((x,y1)\),\((x,y2)\),与他相交的条件是\(y1<=ax^2+bx<=y2\) 对于\ ...