bzoj2301-Problem b
题意
\(T\le 5\times 10^4\) 次询问,每次询问 \(a,b,c,d,k\le 5\times 10^4\),求
\]
分析
重新学了一次(可能跟第一次学没什么区别)莫比乌斯反演相关,这题还是很简单的。问题可以转化为求四次
\]
下面过程中设 \(n\le m\) 。
\sum _{i=1}^n\sum _{j=1}^m[gcd(i,j)=k]&=\sum _{i=1}^n\sum _{j=1}^m\sum _{k|i,k|j}\sum _{d|\frac{i}{k},d|\frac{j}{k}}\mu(d) \\
&=\sum _{d=1}^n\sum _{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum _{j=1}^{\lfloor\frac{m}{k}\rfloor}\mu (d) \\
&=\sum _{d=1}^n\mu (d) \lfloor\frac{\lfloor\frac{n}{k}\rfloor}{d}\rfloor \lfloor\frac{\lfloor\frac{m}{k}\rfloor}{d}\rfloor
\end{aligned}
\]
注意到 \(\lfloor\frac{n}{d}\rfloor\) 这种形式只有 \(2\sqrt n\) 种取值(对于 \(d\le \sqrt n\) ,显然;对于 \(d>\sqrt n\) ,\(\lfloor\frac{n}{d}\rfloor\le \sqrt n\) ,也最多只有 \(\sqrt n\) 种),所以 \(\lfloor\frac{\lfloor\frac{n}{k}\rfloor}{d}\rfloor \lfloor\frac{\lfloor\frac{m}{k}\rfloor}{d}\rfloor\) 最多只有 \(4\sqrt n\) 种取值,跳一下即可做到单次询问 \(O(\sqrt n)\) ,只要预处理 \(\mu\) 函数的前缀和即可。
代码
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int x=0,f=1;
char c=getchar_unlocked();
for (;!isdigit(c);c=getchar_unlocked()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar_unlocked()) x=x*10+c-'0';
return x*f;
}
typedef long long giant;
const int maxn=5e4+1;
bool np[maxn];
int p[maxn],mu[maxn],ps=0;
giant calc(int n,int m) {
giant ret=0;
if (n>m) swap(n,m);
for (int i=1,j;i<=n;i=j+1) {
j=min(n/(n/i),m/(m/i));
ret+=(giant)(mu[j]-mu[i-1])*(n/i)*(m/i);
}
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
mu[1]=1;
for (int i=2;i<maxn;++i) {
if (!np[i]) p[++ps]=i,mu[i]=-1;
for (int j=1;j<=ps && i*p[j]<maxn;++j) {
np[i*p[j]]=true;
if (i%p[j]==0) break;
mu[i*p[j]]=-mu[i];
}
}
for (int i=2;i<maxn;++i) mu[i]+=mu[i-1];
int T=read();
while (T--) {
int a=read(),b=read(),c=read(),d=read(),k=read();
a=(a-1)/k,b/=k,c=(c-1)/k,d/=k;
giant ans=(calc(b,d)-calc(b,c))-(calc(a,d)-calc(a,c));
printf("%lld\n",ans);
}
return 0;
}
bzoj2301-Problem b的更多相关文章
- [HAOI2011][bzoj2301] Problem b [莫比乌斯反演+容斥原理+分块前缀和优化]
题面: 传送门 有洛谷就尽量放洛谷链接呗,界面友好一点 思路: 和HDU1695比较像,但是这一回有50000组数据,直接莫比乌斯反演慢慢加的话会T 先解决一个前置问题:怎么处理a,c不是1的情况? ...
- [bzoj2301]Problem b莫比乌斯反演+分块优化
题意: $\sum\limits_{\begin{array}{*{20}{c}}{a < = x < = b}\\{c < = y < = d}\end{array}} {\ ...
- 【BZOJ2301】【HAOI2011】Problem B(莫比乌斯反演)
[BZOJ2301][HAOI2011]Problem B(莫比乌斯反演) 题面 Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y ...
- 【bzoj2301】 HAOI2011—Problem b
http://www.lydsy.com/JudgeOnline/problem.php?id=2301 (题目链接) 题意 给出${a,b,c,d,k}$,${n}$组询问,求$${\sum_{i= ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ2301:[HAOI2011]Problem b——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2301 https://www.luogu.org/problemnew/show/P2522 对于给 ...
- [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, ...
- Bzoj-2301 [HAOI2011]Problem b 容斥原理,Mobius反演,分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2301 题意:多次询问,求有多少对数满足 gcd(x,y)=k, a<=x<=b ...
- 【数论】【莫比乌斯反演】【线性筛】bzoj2301 [HAOI2011]Problem b
对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 100%的数据满足:1≤n≤50000,1≤a≤b ...
- 【BZOJ2301】【HAOI2011】Problem b [莫比乌斯反演]
Problem b Time Limit: 50 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 对于给出的n个询问,每次 ...
随机推荐
- 20155336虎光元实验四 Android开发基础
20155336虎光元实验四 Android开发基础 实验内容 1:完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号 2:创建 ThirdActivi ...
- 使用T4模板报错:“正在编译转换;当前上下文中不存在名称Host”
用T4模板生成多个文件的实体时,有一句代码是这样的 string curPath = Path.GetDirectoryName(Host.TemplateFile); ...
- mono for andorid第一个小应用
先上图 这个小应用根据两人的姓名算出两人的关系,当然仅仅娱乐. 本来想多写写,但是实在觉得没什么可写的,而且本人正处于感冒状态,脑袋不怎么灵光,所以就不写那么多废话了. 安装mono的话,就上吾乐吧软 ...
- jmeter逻辑控制器
刚开始学习,只写几种了解的逻辑控制器 1.简单控制器 只用来组合采样器和其他逻辑控制器,不影响jmeter的运行 2.循环控制器 用来循环执行采样器和其他逻辑控制器,例如一个用户发送特定请求多次,即可 ...
- Python教程 深入条件控制
while 和 if 条件句中可以使用任意操作,而不仅仅是比较操作. 比较操作符 in 和 not in 校验一个值是否在(或不在)一个序列里.操作符 is 和 is not 比较两个对象是不是同一个 ...
- idea下增加scala
1 idea工具下,下载scala插件 2 idea下新建scala工程 File——New——module 如果按照上图,设置后点击下载,出现下图下载过慢情况下, 这里我选择了等待,大概等了半小时才 ...
- sklearn中的交叉验证(Cross-Validation)
这个repo 用来记录一些python技巧.书籍.学习链接等,欢迎stargithub地址sklearn是利用python进行机器学习中一个非常全面和好用的第三方库,用过的都说好.今天主要记录一下sk ...
- Python常用模块之PIL
官方网址:http://www.pythonware.com/products/pil/index.htm Python Imaging Library (PIL) Python图像库(PIL)将图像 ...
- 动态语言的灵活性是把双刃剑 -- 以 Python 语言为例
本文有些零碎,总题来说,包括两个问题:(1)可变对象(最常见的是list dict)被意外修改的问题,(2)对参数(parameter)的检查问题.这两个问题,本质都是因为动态语言(动态类型语言)的特 ...
- python3【基础】-list&tuple
一.list概述 list (列表)是python中最常用的数据类型之一,通过列表可以对数据实现最方便的存储,修改等操作.在python3中,list支持如下方法: Help on class lis ...