Luogu P2522 [HAOI2011]Problem b 莫比乌斯反演
设$f(d)=\sum_{i=1}^N\sum_{j=1}^M[gcd(i,j)==d],\\F(n)=\sum_{n|d}f(d)=\lfloor \frac{N}{n} \rfloor \lfloor \frac{M}{n} \rfloor$
则$f(n)$
$=\sum_{n|d}\mu(\frac{n}{d})F(d)$
$=\sum_{n|d}\mu(\frac{n}{d})\lfloor \frac{N}{d} \rfloor \lfloor \frac{M}{d} \rfloor$
设$d=kn$
$=\sum_{k=1}^{min(\lfloor \frac{N}{n} \rfloor,\lfloor \frac{M}{n} \rfloor)}\space\mu(k)\lfloor \frac{N}{kn} \rfloor \lfloor \frac{M}{kn} \rfloor$
所以对$\lfloor \frac{N}{kn} \rfloor \lfloor \frac{M}{kn} \rfloor$整除分块,对$\mu(k)$搞一个前缀和。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define R register ll
using namespace std;
namespace Fread {
static char B[<<],*S=B,*D=B;
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
}using Fread::g;
int n,a,b,c,d,x,cnt;
int mu[],pri[];
bool v[];
inline void MU(int n) { mu[]=;
for(R i=;i<=n;++i) {
if(!v[i]) pri[++cnt]=i,mu[i]=-;
for(R j=;j<=cnt&&i*pri[j]<=n;++j) {
v[i*pri[j]]=true;
if(i%pri[j]==) break;
mu[i*pri[j]]=-mu[i];
}
} for(R i=;i<=n;++i) mu[i]+=mu[i-];
}
inline ll calc(int a,int b) { R ret=; a>b?swap(a,b):void();
for(R l=,r;l<=a;l=r+) {
r=min(a/(a/l),b/(b/l));
ret+=(ll)(mu[r]-mu[l-])*(a/l)*(b/l);
} return ret;
}
signed main() {
#ifdef JACK
freopen("NOIPAK++.in","r",stdin);
#endif
MU(); n=g(); while(n--) { R ans=;
a=g()-,b=g(),c=g()-,d=g(),x=g();
printf("%lld\n",calc(b/x,d/x)-calc(a/x,d/x)-calc(b/x,c/x)+calc(a/x,c/x));
}
}
2019.06.09
Luogu P2522 [HAOI2011]Problem b 莫比乌斯反演的更多相关文章
- P2522 [HAOI2011]Problem b (莫比乌斯反演)
题目 P2522 [HAOI2011]Problem b 解析: 具体推导过程同P3455 [POI2007]ZAP-Queries 不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j= ...
- 洛谷P2522 [HAOI2011]Problem b(莫比乌斯反演)
题目描述 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 输入输出格式 输入格式: 第一行一个整数 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- 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][ ...
- 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的, ...
- BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)
[Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...
- 【题解】Luogu P2522 [HAOI2011]Problem b
原题传送门 这题需要运用莫比乌斯反演(懵逼钨丝繁衍) 我们看题面,让求对于区间\([a,b]\)内的整数x和\([c,d]\)内的y,满足$ gcd(x,y)=k$的数对的个数 我们珂以跟容斥原理(二 ...
- [POI2007]ZAP-Queries && [HAOI2011]Problem b 莫比乌斯反演
1,[POI2007]ZAP-Queries ---题面---题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$ ...
随机推荐
- ss命令能识别的TCP状态的关键字
[TCP_ESTABLISHED] = "ESTAB", [TCP_SYN_SENT] = "SYN-SENT", [TCP_S ...
- Dat.gui 使用教程
官方站点:http://workshop.chromeexperiments.com/examples/gui/ Dat.gui 使用教程:Dat.gui 是一个 GUI 组件,他可以为你的 demo ...
- 常见排序算法-php
1.归并排序 $a = [1, 4, 6, 8, 10, 14, 16]; $b = [2, 3, 5, 8, 9, 11]; function merge_sort($a, $b) { $a_i = ...
- codeforces 659A A. Round House(水题)
题目链接: A. Round House time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 集训Day4
在bzoj刷了好几天杂题感觉手感不是很好 继续回来集训一下 好几天没更新了啊... bzoj1875 一个无向图,一个人要从起始点走$t$步走到终点,不能沿着刚走过来那条边回去,问有多少种走法 $m ...
- 「USACO15FEB」「LuoguP3121」审查(黄金)Censoring (Gold)(AC自动机
题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...
- 【LeetCode】031. Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- windows下vs2012用gsoap开发webservice实例
零:说明 1.本文是根据网上前人经验结合自己动手操作写成,开发工具用的vs2012,gsoap用的是gsoap-2.8: 2.gsoap提供的工具简单介绍 1)wsdl2h.exe:根据WSDL文件生 ...
- 物化视图基础概念、mview跨库迁移表
概念:物化视图是一种特殊的物理表,“物化”(Materialized)视图是相对普通视图而言的.普通视图是虚拟表,应用的局限性大,任何对视图的查询,Oracle都实际上转换为视图SQL语句的查询.这样 ...
- POJ1986(LCA应用:求两结点之间距离)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 11304 Accepted: 3985 ...