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

 Sample Output
Case :  
Case :
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减去即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100006
#define ll long long
ll a,b,c,d,k;
ll fac[N];
ll eular(ll n)
{
ll res=;
for(ll i=;i*i<=n;i++)
{
if(n%i==)
{
n/=i,res*=i-;
while(n%i==)
{
n/=i;
res*=i;
}
}
}
if(n>)
res*=n-;
return res;
}
ll solve()
{
ll ans=;
for(ll i=b+;i<=d;i++)
{
ll n=i;
ll num=;
ll cnt=;
for(ll j=;j*j<=n;j++)
{
if(n%j==)
{
fac[num++]=j;
while(n%j==)
{
n/=j;
}
}
}
if(n>) fac[num++]=n; for(ll j=;j<(<<num);j++)
{
ll tmp=;
ll sum=;
for(ll k=;k<num;k++)
{
if((<<k)&j)
{
tmp*=fac[k];
sum++;
}
}
if(sum&) cnt+=b/tmp;
else cnt-=b/tmp;
}
ans=ans+b-cnt;
}
return ans;
}
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--)
{
printf("Case %d: ",++ac);
scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&k);
if(k==)
{
printf("0\n");
continue;
}
if(b>d)
swap(b,d);
b/=k;
d/=k;
//printf("---%d %d\n",b,d);
ll ans=;
for(ll i=;i<=b;i++)
{
ans+=eular(i);
}
//printf("-%d\n",ans);
ans=ans+solve();
printf("%I64d\n",ans);
}
return ;
}

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

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

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

  2. 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 和 ...

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

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

  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 1695 GCD 欧拉函数+容斥原理+质因数分解

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

  7. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  8. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  9. HDU 2588 GCD (欧拉函数)

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

随机推荐

  1. lesson7:java线程池

    在jdk1.5的版本中,java提供了语言级别的线程池,对于需要使用线程池的业务系统和中间件框架等提供了方便的选择.我认为线程池主要有两个方面的作用:第一就是防止因为外部条件的变化,造成线程数的瞬间飙 ...

  2. 如何用SVN进行个人版本管理

    事实上SVN的确是我用过的最好的源码管理工具,虽然我用过的这类工具并不多,只有VSS.CVS和SVN,其它像PVCS. TeamSource.ClearCase之类的只有耳闻,因为它们都是商业产品,并 ...

  3. 华为OJ:2041 放苹果

    这道题难点不在于代码怎么写,而是思路怎么想. 感觉一般这样的题要么你理好一个思路要么你最后总结出一个公式,要么你自己模拟它的运作方式,用迭代,或者递归的方式来做. 有点像我们曾经学的排列组合. 对于m ...

  4. Zend Framework 留言本实战(转)

    一.环境搭建和ZF安装              *[注]本节内容大部分来至Zend Framework官方手册       1.1 Zend Framework下载 Zend Framework 使 ...

  5. [小工具] Command-line CPU Killer(附源码及下载链接)

    博主有次在拆卸自己的笔记本电脑后,发现电脑如果静置时间长了有时会重启,但奇怪的是当我自己在电脑前工作的时候从来没有重启过.据此推测可能 CPU 完全空闲的时候风扇完全停转了,虽然 CPU 温度不高,但 ...

  6. openwrt interface

    orige : http://www.cnblogs.com/preorder69/p/3959187.html 这篇算是对openwrt网络接口的一个翻译吧,源地址:http://wiki.open ...

  7. 把Nginx加入系统服务 service nginx (start | stop | restart | reload)

    vim /etc/init.d/nginx 1 #!/bin/bash  2 # nginx Startup script for the Nginx HTTP Server  3 # it is v ...

  8. 带搜索功能,支持绑定对象到节点的TreeView辅助类

    特点: 1.支持数叶子节点与对象绑定 2.支持xml导入,且数据类相关的xml可自定义,只和泛型的实现有关 3.支持节点搜索功能,可在树结构上要求只显示部分节点 4.用C#编写,但与平台关联性低,可移 ...

  9. XML中 添加或修改时 xmlns="" 怎么删除

    //创建节点时 记得加上  ---> xmldoc.DocumentElement.NamespaceURI XmlElement url = xmldoc.CreateElement(&quo ...

  10. C#SaveFileDialog的使用

    SaveFileDialog sfd = new SaveFileDialog(); //默认打开的路径 sfd.InitialDirectory = "C:\\Users\\Adminis ...