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. css transform常用变化解析

    本文旨在对常用变化做最直观的简析 translate 移动 translateX() X轴正方向移动(单位可为px,也可为%,为%时以自身为参照物) translateY() Y轴反方向移动 tran ...

  2. [Android]libpng error: Not a PNG file错误解决

    我在将以前在Eclipse中写的项目import到android studio中后,出现了 AAPT err(Facade for 157667509): libpng error: Not a PN ...

  3. dfs枚举

    深度优先搜索(DFS,Depth-First Search)是搜索手段之一.它从某个状态开始,不断的转移状态知道无法转移,然后退回到前一步的状态,继续转移到其他状态,如此不断重复,直到找到最终的解. ...

  4. Shell中数学计算/运算

    shell中的赋值和操作默认都是字符串处理. 1)使用let(只能进行整数运算)var=1let "var+=1"echo $var输出结果为2 注意:a)let几乎支持所有的运算 ...

  5. 【P2476】着色方案(记忆化搜索+特殊的DP数组)

    这个题代码难度几乎为0,然而思维难度对于蒟蒻来说简直是突破天际啊!首先我思考的是这个油漆的种类只有15种,是不是可以像一道叫做8数码难题的东西暴力15维数组呢..计算发现不可以....空间会直接让你学 ...

  6. 剑指Offer——反转链表

    Question 输入一个链表,反转链表后,输出链表的所有元素. Solution 如果空间复杂度要求为O(1)的话,可以考虑用三个指针来进行反转 如果没有空间复杂度限制的话,可以考虑用一个栈,将节点 ...

  7. R语言笔记005——计算描述性统计量

    数据的分布特征: 分布的集中趋势,反应各数据向其中心值靠拢或聚集的程度(平均数,中位数,四分位数,众数) 分布的离散程度,反应各数据远离其中心值的趋势(极差,四分位差,方差,标准差,离散系数) 分布的 ...

  8. ajax01简介

    (Asynchronous JavaScript and XML)Ajax :异步 JavaScript 和 XML,一种允许浏览器和服务器通信进行少量数据交换而无需重新加载整个网页,以实现更新部分网 ...

  9. ZC_问题

    1. [02][40:15]主键 还需要另外创建 序列? 一直到 [03][31:25]都已经成功的insert了两条数据了,也没见 手动创建 序列... 2. 貌似记得 有地方给 hibernate ...

  10. Spring Boot入门——JDBCTemplate使用及其相关问题解决

    1.在pom.xml文件中引入相应依赖 <!-- mysql依赖 --> <dependency> <groupId>mysql</groupId> & ...