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, ...
随机推荐
- <转>安卓软件测试的几个要点
1.界面 ① 文字错误.图片不显示或显示不正确.缺少输入项.按钮的大小和点击效果 ② 布局.图片和配色设计问题,测试人员很难进入 ③ 提示信息,提示信息语言准确简洁,有指导性.在应该提示的位置放入提示 ...
- php生成百度新闻源xml
<?php /* http://baike.baidu.com/view/125547.htm#2 百度网新闻开放协议 */ mysql_connect($CFG['db_host'] ,$CF ...
- 【Python学习笔记】with语句与上下文管理器
with语句 上下文管理器 contextlib模块 参考引用 with语句 with语句时在Python2.6中出现的新语句.在Python2.6以前,要正确的处理涉及到异常的资源管理时,需要使用t ...
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- CenotOS ip a
- incompatible
- JVM内存的那些事
前言 对于C语言开发的程序员来说,在内存管理方面,必须负责每一个对象的生命周期,从有到无. 对于Java程序员你来说,在虚拟机内存管理的帮助下,不需要为每个new对象都匹配free操作,内存泄露和内存 ...
- ArcObjects10.0MapControl不显示地图内容
添加MapControl控件,右键属性设置MXD 文档之后,运行显示一片空白.拖放另一个控件axLicenseControl之后OK.
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...
- MAT(3)获取dump文件
方式一:添加启动参数 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=E:\Java\dump 生成的文件例如:java_pid2080.hprof ...