HDU - 1695 GCD (容斥+枚举)
题意:求区间1<=i<=b与区间1<=j<=d之间满足gcd(i,j) = k 的数对 (i,j) 个数。(i,j)与(j,i) 算一个。
分析:gcd(i,j)=k可以转化为gcd(i/k,j/k)=1。枚举每个1<=i<=b/k 的 i,用容斥原理统计区间[1,d]中与其互素的个数。需要预处理筛出2~1e5中每个数的质因子。
*注意当k=0时,数对不存在。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn =1e5+;
vector<int> p[maxn];
bool is[maxn]; void pre()
{
for(int i=;i<maxn;i+=) {
p[i].clear();
p[i].push_back();
}
for(int i=;i<maxn;i+=){
if(is[i]) continue;
for(int j=i;j<maxn;j+=i){
is[j] = true;
p[j].push_back(i);
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
pre();
int T,cas=; scanf("%d",&T);
while(T--){
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==){
printf("Case %d: 0\n",cas++);
continue;
}
b/=k,d/=k;
if(b>d) swap(b,d);
LL ans=;
for(int i=;i<=d;++i){
int tot = min(i,b);
ans += tot;
int up = <<p[i].size(),len = p[i].size();
for(int j=;j<up;++j){
int cnt=,ji=; for(int k=;k<len;++k){
if(j&(<<k)){
cnt++;
ji *=p[i][k];
}
} if(cnt&) ans -= tot/ji;
else ans +=tot/ji;
}
}
printf("Case %d: %lld\n",cas++,ans);
}
return ;
}
HDU - 1695 GCD (容斥+枚举)的更多相关文章
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- 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 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
problem's Link mean 给定五个数a,b,c,d,k,从1~a中选一个数x,1~b中选一个数y,使得gcd(x,y)=k. 求满足条件的pair(x,y)数. analyse 由于b, ...
- HDU 1695 GCD(容斥定理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
随机推荐
- CNN图像分割总结
深度学习(三十三)CRF as RNN语义分割-未完待续 http://blog.csdn.net/hjimce/article/details/50888915 Fully Convolutiona ...
- <!>连结格式
<base href=位址>(预设好连结路径) <a href=位址></a>外部连结 <a href=位址 target=’_blank’></ ...
- MySQL防止重复插入唯一限制的数据 4种方法
MySQL防止重复插入唯一限制的数据,下面我们逐一分析 : 1.insert ignore into 当插入数据时,如出现错误时,如重复数据,将不返回错误,只以警告形式返回.所以使用ignore请确保 ...
- 使用Array的原型使对象具有length,和数组的内容
var elems = { length: , add: function (elem) { Array.prototype.push.call(this, elem); }, gather: fun ...
- bootstrap基础学习九篇
现在学学bootstrap响应式实用工具 Bootstrap 提供了一些辅助类,以便更快地实现对移动设备友好的开发.这些可以通过媒体查询结合大型.小型和中型设备,实现内容对设备的显示和隐藏. 需要谨慎 ...
- ROS导航之参数配置和自适应蒙特卡罗定位
我们的机器人使用两种导航算法在地图中移动:全局导航(global)和局部导航(local).这些导航算法通过代价地图来处理地图中的各种信息,导航stack使用两种costmaps http://www ...
- 5、手把手教React Native实战之盒子模型BoxApp
用HTML5和React Native分别实现盒子模型显示 写法不一样: 1.样式 ; fs=Fs; ...
- Java定时任务:利用java Timer类实现定时执行任务的功能
一.概述 在java中实现定时执行任务的功能,主要用到两个类,Timer和TimerTask类.其中Timer是用来在一个后台线程按指定的计划来执行指定的任务. TimerTask一个抽象类,它的子类 ...