GCD

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=1695

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

T

a b c d k

Output

ans

Sample Input

1

1 3 1 5 1

Sample Output

9

Hint

题意

问你gcd(i,j)=k有多少对,其中b>=i>=a,d>=j>=c

其中a和c恒等于1

题解:

很显然,我们知道一个结论,gcd(i,j)=k,b>=i>=a,d>=j>=c这个恒等于

gcd(i,j)=1,b/k>=i>=1,d/k>=j>=1

然后怎么办呢?我们暴力枚举每一个1<=i<=b/k的数,看在1<=j<=d/k里面有多少个和他互质的就好了

这个我们可以用容斥来做。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
vector<int>pri[maxn];
void pre()
{
for(int i=2;i<100007;i++)
{
int now = i;
for(int j=2;j*j<=now;j++)
{
if(now%j==0)
{
pri[i].push_back(j);
while(now%j==0)
now/=j;
}
if(now==1)break;
}
if(now>1)
pri[i].push_back(now);
}
}
int solve(int x,int tot)
{
int res = 0;
for(int i=1;i<(1<<pri[x].size());i++)
{
int num = 0;
int tmp = 1;
for(int j=0;j<pri[x].size();j++)
{
if((i>>j)&1)
{
num++;
tmp*=pri[x][j];
}
}
if(num%2==1)res+=tot/tmp;
else res-=tot/tmp;
}
return tot-res;
}
int main()
{
pre();
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==0)
{
printf("Case %d: 0\n",cas);
continue;
}
b/=k,d/=k;
if(b<d)swap(b,d);
long long ans = 0;
for(int i=1;i<=b;i++)
ans+=solve(i,min(i,d));
printf("Case %d: %lld\n",cas,ans);
}
}

HDU 1695 GCD 容斥的更多相关文章

  1. hdu 1695 GCD 容斥+欧拉函数

    题目链接 求 $ x\in[1, a] , y \in [1, b] $ 内 \(gcd(x, y) = k\)的(x, y)的对数. 问题等价于$ x\in[1, a/k] , y \in [1, ...

  2. HDU - 1695 GCD (容斥+枚举)

    题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数.(i,j)与(j,i) 算一个. 分析:gcd(i,j)=k可以转化为 ...

  3. hdu 5514 Frogs(容斥)

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

  5. HDU 2588 思维 容斥

    求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...

  6. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

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

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

  8. HDU 1695 GCD(容斥定理)

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

  9. 数论 + 容斥 - HDU 1695 GCD

    problem's Link mean 给定五个数a,b,c,d,k,从1~a中选一个数x,1~b中选一个数y,使得gcd(x,y)=k. 求满足条件的pair(x,y)数. analyse 由于b, ...

随机推荐

  1. 用ASP.Net写一个发送ICQ信息的程序

    用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...

  2. [转]SqlPlus安装配置

    本文转载自http://blog.csdn.net/wuxiaoyan_home/article/details/4826440 一.下载oracle 10g sqlplus软件 http://www ...

  3. 新手须知设计的法则 Mark

    经常看到一些讲如何学习设计的文章,坦白讲感觉有些千篇一律.且不痛不痒,都说要看点书.学点画.练软件.多观察……唉,练软件这事还要说么,难道你还需要告诉一个人学开发是需要学习编程语言的? 学习是基于过往 ...

  4. Hadoop学习记录(6)|Eclipse安装Hadoop 插件

    下载 https://skydrive.live.com/redir.aspx?cid=cf7746837803bc50&resid=CF7746837803BC50!1277&par ...

  5. poj 1003 Hangover

    #include <iostream> using namespace std; int main() { double len; while(cin >> len & ...

  6. 怎么监视跟踪一个进程(Process)中的MS Unit Test DLL的详细性能(performance)【asp.net C#】

    Sample This tutorial will show how to instrument a unit test DLL for performance profiling. Visual S ...

  7. 第二百一十七天 how can I 坚持

    JavaScript  document.getElementByName()获取数组,for循环,搞了一天,好笨. 明天要下雪了,好冷. 双十一,天猫搞的挺特别啊,晚上抢了个小米红包,不知道买啥,哎 ...

  8. 【转】B树、B-树、B+树、B*树

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  9. 在VS2012中实现Ext JS的智能提示

    Visual Studio 2012太强大了,居然能自己会去提取Ext JS的类的属性和方法,从而实现只能提示.下面就来介绍一下实现这个功能. 在Visual Studio 2012中随便创建一个We ...

  10. hdu 4289 Control(最小割 + 拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Mem ...