HDU 1695 容斥
又是求gcd=k的题,稍微有点不同的是,(i,j)有偏序关系,直接分块好像会出现问题,还好数据规模很小,直接暴力求就行了。
/** @Date : 2017-09-15 18:21:35
* @FileName: HDU 1695 容斥 或 莫比乌斯反演.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; LL pri[N];
LL mu[N];
LL sum[N];
int c = 0;
bool vis[N]; void prime()
{
MMF(vis);
MMF(sum);
mu[1] = 1;
for(int i = 2; i < N; i++)
{
if(!vis[i])
pri[c++] = i, mu[i] = -1;
for(int j = 0; j < c && i * pri[j] < N; j++)
{
vis[i * pri[j]] = 1;
if(i % pri[j] == 0)
{
mu[i * pri[j]] = 0;
break;
}
else mu[i * pri[j]] = -mu[i];
}
}
sum[0] = 0;
for(int i = 1; i < N; i++)
sum[i] += sum[i - 1] + mu[i];
} LL get_sum(LL n, LL m)
{
if(n > m) swap(n, m);
int mi = min(n, m);
LL ans = 0;
for(int i = 1, last; i <= mi; i++, last = last + 1)
{
last = min(n/(n/i), m/(m/i));//由于有重复情况 不能直接分块?
ans += (LL)(n / i) * (m / i) * (sum[i] - sum[i - 1]);
}
return ans;
} int main()
{
int T;
prime();
cin >> T;
int cnt = 0;
while(T--)
{
LL a, b, c, d, k;
scanf("%lld%lld%lld%lld%lld", &a, &b, &c, &d, &k);
if(k == 0)
{
printf("Case %d: 0\n", ++cnt);
continue;
}
a = (a - 1) / k;
b = b / k;
c = (c - 1) / k;
d = d / k;
LL ans = get_sum(a, c) + get_sum(b, d) - get_sum(a, d) - get_sum(b, c) - get_sum(min(b,d), min(b,d)) / 2;
printf("Case %d: %lld\n", ++cnt, ans);
/*LL ans = 0;
LL t = 0;
for(int i = 1; i <= b; i++)
ans += (b / i) * (d / i) * mu[i];
for(int i = 1; i <= d; i++)
t += (min(b,d)/ i) * (min(b, d) / i) * mu[i];
printf("Case %d: %lld\n", ++cnt, ans - t / 2);*/
}
return 0;
}
HDU 1695 容斥的更多相关文章
- HDU 4135 容斥
问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...
- HDU 2841 容斥 或 反演
$n,m <= 1e5$ ,$i<=n$,$j<=m$,求$(i⊥j)$对数 /** @Date : 2017-09-26 23:01:05 * @FileName: HDU 284 ...
- HDU 4059 容斥初步练习
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...
- hdu 1220 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1220 Cube Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Co-prime HDU - 4135_容斥计数
Code: #include<cstdio> #include<cstring> #include<cmath> #include<iostream> ...
- How many integers can you find HDU - 1796_容斥计数
Code: #include<cstdio> using namespace std; typedef long long ll; const int R=13; ll a[R]; ll ...
- 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 欧拉函数 + 容斥
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 ...
随机推荐
- Python学习之路8 - 内置方法
abs(-230) #取绝对值 all([0,1,-5]) #如果参数里面的所有值都为真就返回真,否则返回假 any([0,1,-5]) #如果参数里面有一个值为真则返回真,否则返回假 ascii([ ...
- HDU 5661 Claris and XOR 贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5661 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- HDU 5651 xiaoxin juju needs help 逆元
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- 严重: Failed to destroy end point associated with ProtocolHandler ["http-nio-8080"] java.lang.NullPointer
刚接触servlet类,按照课本的方法使用eclipse新建了一个servlet类. 新建完成后,在web.xml里面进行注册 这时候就会报错了. 五月 07, 2016 11:23:28 上午 or ...
- vs快捷键代码格式化或代码对齐名字
开发人员,换个电脑后环境要重装,vs的环境也需要重新设置. 快捷键需要重新设置,插件也需要重装,在这里备注下,换个环境就可以直接用了. 由于vs不同版本,代码对齐或者代码格式化的快捷键都不一样,所以导 ...
- 如何解决Unsupported Architecture. Your executable contains unsupported architecture '[x86_64, i386]
APP改版测试后准备Archive发布时,结果居然出现题目中的错误提示.查了一下,如果archive的时候没有选[iOS](http://lib.csdn.net/base/ios) Devices ...
- iOS- Swift和Object-C的混合编程
1.前言 事实证明,在短时间内Swift还取代不了Object-C, 而且.... Apple Swift 开发小组的人说的:「We'll recommend people start with ...
- bootstrap心得
最近在弄个人的博客,之前对bootstrap的使用老是感觉使用的一般 幸好在看了慕课网的一个老师的实例教程之后,才感觉是真正对前端使用bootstrap有了一点理解 首先就是. 这些标签,其实都是相当 ...
- Java中int与String间的类型转换
int -> String int i=12345;String s=""; 除了直接调用i.toString();还有以下两种方法第一种方法:s=i+"" ...
- BZOJ 1562 变换序列(二分图匹配)
显然每个位置只有两个情况,所以用二分图最大匹配来求解. 如果二分图有完全匹配,则有解. 关键是如何求最小的字典序解. 实际上用匈牙利算法从后面开始找增广路,并优先匹配字典序小的即可. # includ ...