[luogu2522][bzoj2301][HAOI2011]Problem b【莫比乌斯反演】
传送门:https://www.luogu.org/problemnew/show/P2522
题目描述
对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。
分析
特殊情况和POI2007 ZAP-Queries相同。
接下来的问题就是解决普遍情况,不难得到答案就是\(ans(b,d)-ans(b,c-1)-ans(a-1,d)+ans(a-1,c-1)\),这是容斥原理。
这道题目有毒,int和long long乱开会T掉,要注意。
ac代码
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
#define N 50005
int prime[N], mu[N], sum[N];
bool vis[N];
int a, b, c, d, k, cnt;
void get_mu(int MAXN) {
mu[1] = 1;
for (int i = 2; i <= MAXN; i ++) {
if (!vis[i]) {
prime[++ cnt] = i;
mu[i] = -1;
}
for (int j = 1; j <= cnt && prime[j] * i <= MAXN; j ++) {
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
else mu[i * prime[j]] = -mu[i];
}
}
for (int i = 1; i <= MAXN; i ++) sum[i] = sum[i - 1] + mu[i];
}
ll solve(int a, int b) {
ll res = 0;
for (int l = 1, r; l <= min(a, b); l = r + 1) {
r = min(a / (a / l) , b / (b / l));
res += 1ll * (a / (l * k)) * (b / (l * k)) * (sum[r] - sum[l - 1]);
}
return res;
}
int main() {
int cas;
read(cas);
get_mu(50000);
while (cas --) {
read(a); read(b); read(c); read(d); read(k);
printf("%lld\n", solve(b, d) - solve(b, c - 1) - solve(a - 1, d) + solve(a - 1, c - 1));
}
return 0;
}
[luogu2522][bzoj2301][HAOI2011]Problem b【莫比乌斯反演】的更多相关文章
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ2301: [HAOI2011]Problem b 莫比乌斯反演
分析:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 然后对于求这样单个的gcd(x,y)=k的, ...
- [bzoj2301][HAOI2011]Problem B —— 莫比乌斯反演+容斥原理
题意 给定a, b, c, d, k,求出: \[\sum_{i=a}^b\sum_{j=c}^d[gcd(i, j) = k]\] 题解 为方便表述,我们设 \[calc(\alpha, \beta ...
- BZOJ2301:[HAOI2011]Problem b(莫比乌斯反演,容斥)
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- [BZOJ1101&BZOJ2301][POI2007]Zap [HAOI2011]Problem b|莫比乌斯反演
对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 我们可以令F[n]=使得n|(x,y)的数对(x,y)个数 这个很容易得到,只需要让x, ...
- P2522 [HAOI2011]Problem b (莫比乌斯反演)
题目 P2522 [HAOI2011]Problem b 解析: 具体推导过程同P3455 [POI2007]ZAP-Queries 不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j= ...
- Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...
- BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1007 Solved: 415[Submit][ ...
- BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)
[Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...
- [POI2007]ZAP-Queries && [HAOI2011]Problem b 莫比乌斯反演
1,[POI2007]ZAP-Queries ---题面---题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$ ...
随机推荐
- Luogu P2059 [JLOI2013]卡牌游戏
一道比较简单的概率DP 首先看到这种题目和数据范围,就要毫不犹豫地列DP方程: 我们令\(f_{i,j}\)表示还剩下i个人时编号为j的人的胜率,那么首先我们可以知道边界条件\(f_{1,1}=1\) ...
- Pycharm: 代码跳转如何回退 (小技巧)
背景 玩Python已经有段时间了, 一般都是通过vim和Pycharm来开发, 真心觉得这两个是神器. Vim神器暂且不说, 今天来分享Pycharm的一个小技巧. 用Pycharm的童鞋都知道, ...
- 《Linux内核设计与实现》第八周读书笔记——第四章 进程调度
<Linux内核设计与实现>第八周读书笔记——第四章 进程调度 第4章 进程调度35 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配 ...
- 安装python包时报错
pip install numpy 时 报错: Traceback (most recent call last): File "d:\学习\python\python-3.6.5\l ...
- 【转】CSS颜色代码大全
转自http://www.cnblogs.com/axing/archive/2011/04/09/CSS.html FFFFFF #DDDDDD #AAAAAA #888888 #666666 #4 ...
- Linux&docker&cgroups
cgroup https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_manage ...
- SSO的定义、原理、组件及应用
定义: https://baike.baidu.com/item/SSO/3451380 原理: https://blog.csdn.net/cutesource/article/details/58 ...
- HTML使用button的一个小坑
https://www.w3schools.com/TAGs/att_button_type.asp Definition and Usage The type attribute specifies ...
- Docker for windows 入门一(下载安装)
预安装条件,可以查阅官方文档,本人是Win10 x64(必要条件)教育版+开启Hyper-V(Feature特性),具体可参考云栖社区的文章: https://yq.aliyun.com/articl ...
- Effective C++(第三版)笔记 ---- 第一部分让自己习惯C++
内容从侯捷译版的<Effective C++>(第三版)摘录 条款一 C++作为一个多种范式融合的语言,可以看成是语言的联邦,它包含了一下四种主要的次语言: C.C++以C为基础,很多时候 ...