HDU 1695 GCD 容斥
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 容斥的更多相关文章
- 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, ...
- HDU - 1695 GCD (容斥+枚举)
题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数.(i,j)与(j,i) 算一个. 分析:gcd(i,j)=k可以转化为 ...
- hdu 5514 Frogs(容斥)
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5213 分块 容斥
给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...
- HDU 2588 思维 容斥
求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...
- hdu 1695 GCD 欧拉函数 + 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K] 和 [L ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD(容斥定理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 数论 + 容斥 - 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, ...
随机推荐
- 【Android】创建、读取XML文件
创建: package webdomain; import java.io.File; import java.io.FileNotFoundException; import java.io.Fil ...
- Inverse是hibernate双向关系中的基本概念。inverse的真正作用就是指定由哪一方来维护之间的关联关系。当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之间的关联关系,说白了就是hibernate如何生成Sql来维护关联的记录
<set name ='students' table="students_table" inverse='false'(默认不用写) > <key column ...
- 黑马程序员——有关protocol的小结
在OC程序中经常会有这样的问题就是一个类想让其他类帮自己实现某些方法,然后再将结果返回给这个类:如何让一个类要找的代理去实现自己想要的方法呢? 这样就需要有一个协议,让能遵守协议的其他类都能实现协议中 ...
- MFC中TRACE
错误 1 error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shar ...
- 2013年19个最棒的HTML5网站模板免费下载
上次我们整理了14个HTML5奉献给大家下载了,今天我再给大家整理了19个2013最新的HTML5模板供有需要的朋友下载使用,它们涉及不同的行业的模板需求,支持手机设备,十分精美! 1. Affini ...
- 解决“运行arm-linux-gcc命令,提示No such file or directory”的问题
今天在ubuntu14.04上安装arm的交叉编译器arm-linux-gcc,环境变量配置好以后,运行arm-linux-gcc命令,总提示No such file or directory.然后去 ...
- 上传控件swfupload的使用笔记
1.下载下来的官方domo里不同的例子里会引入各自的JS,注意区分.可以直接拿官方例子来改成自己想要的例子. 2.注意PHP配置文件里也有最大上传文件限制,如果文件太大会上传不成功. 3.如果有问题可 ...
- 《学习OpenCV》练习题第四章第一题a
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- 【调试】路由器设置不了静态IP -- clwu
办公室的路由器是IP-COM,原来可以设置静态IP 的,但后来不知道为什么比较长一段时间内设置不了,原来设置的静态IP 也不见了.现象是这样 什么都没有. 今天的网络被网管重调整过了,需要重新设置静态 ...
- 怎么利用C#中的 webclient 创建cookie
Cookies are not limited only to web browsers. any http-aware client that supports cookies can deal w ...