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. ACM训练小结-2018年6月19日

    今天题目情况如下:  A题:考察图论建模+判割点.B题:考察基础数据结构的运用(STL).C题:考察数学建模+运算.(三分可解)D题:考察读题+建模+数据结构的运用.E题:考察图论+贪心.F题:考察图 ...

  2. 5.1深入理解计算机系统——系统级I/O

    一.UNIX I/O     在UNIX系统中有一个说法,一切皆文件.所有的I/O设备,如网络.磁盘都被模型化为文件,而所有的输入和输出都被当做对相应文件的读和写来执行.这种将设备映射为文件的方式,允 ...

  3. Bootstrap3组件--1

     目录 1. Glyphicons字体图标 2.下拉菜单 3.按钮组 4. 输入框组 5.导航 6. 导航条 7. 路径导航 1. Glyphicons字体图标 出于性能的考虑,所有图标都需要一个基类 ...

  4. 使用eclipse搭建第一个python+Django的web开发实例

    python+Django的web开发实例   一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的 ...

  5. C#反射第一天

    [转]C#反射   反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等. ...

  6. JavaWeb -- 邮件收发

    smtp协议: telnet smtp.qq.com 25 ehlo kevin auth login eGlhbmdqaWU1NUBxcS5jb20= a2V2aW5feGlhbmc1NQ== ma ...

  7. 理解i++和++i

    理解i++和++i i++和++i是C/C++基础知识,i++是先传值后自增,++i是先自增后传值.汇编源码如下: int xx; int x = 1; 00F61702 mov dword ptr ...

  8. java异常中的finally(一)

    finally是保证语句能一定执行的.不管程序是否会报错,我们把程序一定要执行的代码放在finally中. 比如说流的关闭,不管在读写的过程中是否报错,一定要关闭流,可以把流的关闭操作放在finall ...

  9. 路由器分配的IP地址

    在IP地址范围内,一部分地址将保留作为私人IP地址空间,专门用于内部局域网使用,这些地址如下表: A类 10.0.0.0-10.255.255.255 网络数:1 B类 172.16.0.0-172. ...

  10. CruiseControl初探

    一.背景 CruiseControl从<项目自动化之道>这本书中了解到,然后又从网上查资料做了一定尝试.同时,项目持续集成这部分我也计划在自己参与的项目上先试点实行,才有了这篇文章. 二. ...