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. 《UNIX环境高级编程》笔记--信号集

    1.信号集基本操作 我们需要有一个能表示多个信号--信号集(signal set)的数据类型.POSIX.1定义了数据类型sigset_t以包含一个信号 集,并且定义了一下五个处理信号处理信号集函数. ...

  2. POJ 2513 Colored Sticks - from lanshui_Yang

    题目大意:给定一捆木棒,每根木棒的每个端点涂有某种颜色.问:是否能将这些棒子首位项链,排成一条直线,且相邻两根棍子的连接处的颜色一样. 解题思路:此题是一道典型的判断欧拉回路或欧拉通路的问题,以木棍的 ...

  3. parcel write boolean值

    http://stackoverflow.com/questions/6201311/how-to-read-write-a-boolean-when-implementing-the-parcela ...

  4. Android 开发 对话框Dialog dismiss和hide方法的区别

    http://ningtukun.blog.163.com/blog/static/186541445201310151539697/ dismiss和hide方法都可以隐藏对话框,在需要的时候也可以 ...

  5. C#_Socket网络编程实现的简单局域网内即时聊天,发送文件,抖动窗口。

    最近接触了C#Socket网络编程,试着做了试试(*^__^*) 实现多个客户端和服务端互相发送消息 发送文件抖动窗口功能 服务端: using System; using System.Collec ...

  6. Ruby与sass 与compass安装

     Ruby安装 windows平台下使用Rubyinstaller安装 1) 下载Rubyinstaller 2) 安装Rubyinstaller 记得勾选 add ruby executables ...

  7. C# 并行编程 之 并发集合 (.Net Framework 4.0)(转)

    转载地址:http://blog.csdn.net/wangzhiyu1980/article/details/45497907 此文为个人学习<C#并行编程高级教程>的笔记,总结并调试了 ...

  8. Dom4j之xPath

    XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...

  9. Django Meta内部类选项

    http://blog.csdn.net/yelbosh/article/details/7545335

  10. PHP页面中文乱码问题

    首先纯html页要用meta标签声明编码<meta http-equiv="Content-Type" content="text/html; charset=&q ...