[HAOI2011]Problem b&&[POI2007]Zap
题目大意:
$q(q\leq50000)$组询问,对于给定的$a,b,c,d(a,b,c,d\leq50000)$,求$\displaystyle\sum_{i=a}^b\sum_{j=c}^d[\gcd(i,j)=k]$。
思路:
首先可以利用容斥原理,将$(a,b,c,d)$的询问拆成$(b,d)$、$(a-1,d)$、$(b,c-1)$和$(a-1,c-1)$四个询问,对于询问$(n,m)$,有:
$$
\begin{align*}
&\sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)=k]\\
&=\sum_{k=1}^{\min(n,m)}\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{k}\rfloor}[\gcd(i,j)=1]\\
&=\sum_{k=1}^{\min(n,m)}\sum_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{k}\rfloor}\sum_{d|\gcd(i,j)}\mu(d)\\
&=\sum_{k=1}^{\min(n,m)}\sum_{d=1}^{\min(\lfloor\frac{n}{k}\rfloor,\lfloor\frac{m}{k}\rfloor)}\mu(d)\sum_{1\leq i\leq\lfloor\frac{n}{k}\rfloor且d|i}\sum_{1\leq i\leq\lfloor\frac{m}{k}\rfloor且d|j}1\\
&=\sum_{k=1}^{\min(n,m)}\sum_{d=1}^{\min(\lfloor\frac{n}{k}\rfloor,\lfloor\frac{m}{k}\rfloor)}\mu(d)\lfloor\frac{\lfloor\frac{n}{k}\rfloor}{d}\rfloor\lfloor\frac{\lfloor\frac{m}{k}\rfloor}{d}\rfloor
\end{align*}
$$
$\mu$的前缀和可以预处理。因此到最后一步时,复杂度已经是$O(n)$的了,然而$q\leq50000$,还是会TLE。考虑某些时候对于某一范围内的$k$,$\lfloor\frac{\lfloor\frac{n}{k}\rfloor}{d}\rfloor$和$\lfloor\frac{\lfloor\frac{m}{k}\rfloor}{d}\rfloor$分别各自相等,因此我们可以将相等的情况一起考虑,这样总共有$O(\sqrt n)$种情况,时间复杂度$O(q\sqrt n)$。
#include<cstdio>
#include<cctype>
#include<algorithm>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,M=;
bool vis[N];
int mu[N],sum[N],p[M];
inline void sieve() {
mu[]=;
for(register int i=;i<N;i++) {
if(!vis[i]) {
p[++p[]]=i;
mu[i]=-;
}
for(register int j=;j<=p[]&&i*p[j]<N;j++) {
vis[i*p[j]]=true;
if(i%p[j]==) {
mu[i*p[j]]=;
break;
}
mu[i*p[j]]=-mu[i];
}
}
for(register int i=;i<N;i++) {
sum[i]=sum[i-]+mu[i];
}
}
inline int64 calc(int n,int m,const int &k) {
int64 ret=;
n/=k,m/=k;
const int lim=std::min(n,m);
for(register int i=,j;i<=lim;i=j+) {
j=std::min(n/(n/i),m/(m/i));
ret+=(int64)(sum[j]-sum[i-])*(n/i)*(m/i);
}
return ret;
}
int main() {
sieve();
for(register int i=getint();i;i--) {
const int a=getint(),b=getint(),c=getint(),d=getint(),k=getint();
printf("%lld\n",calc(b,d,k)-calc(a-,d,k)-calc(b,c-,k)+calc(a-,c-,k));
}
return ;
}
[HAOI2011]Problem b&&[POI2007]Zap的更多相关文章
- [POI2007]ZAP-Queries && [HAOI2011]Problem b 莫比乌斯反演
1,[POI2007]ZAP-Queries ---题面---题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$ ...
- 莫比乌斯反演学习笔记+[POI2007]Zap(洛谷P3455,BZOJ1101)
先看一道例题:[POI2007]Zap BZOJ 洛谷 题目大意:$T$ 组数据,求 $\sum^n_{i=1}\sum^m_{j=1}[gcd(i,j)=k]$ $1\leq T\leq 50000 ...
- P2522 [HAOI2011]Problem b (莫比乌斯反演)
题目 P2522 [HAOI2011]Problem b 解析: 具体推导过程同P3455 [POI2007]ZAP-Queries 不同的是,这个题求的是\(\sum_{i=a}^b\sum_{j= ...
- Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...
- 【BZOJ】2301: [HAOI2011]Problem b(莫比乌斯+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2301 和这题不是差不多的嘛--[BZOJ]1101: [POI2007]Zap(莫比乌斯+分块) 唯 ...
- 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 Submit: 3757 Solved: 1671 [Submit] ...
- BZOJ 1101: [POI2007]Zap
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2262 Solved: 895[Submit][Status] ...
- [BZOJ1101][POI2007]Zap
[BZOJ1101][POI2007]Zap 试题描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd ...
随机推荐
- oracle 11g 版本自带移除,省时省力
---oracle删除 app\Administrator\product\11.2.0\dbhome_1\deinstall.bat 指定要取消配置的所有单实例监听程序 [LISTENER]: En ...
- tensorboard页面显示No dashboards are active for current data set 问题win10系统
如果问题如上所示,可以试下如下方法: 在文件夹中找到你的logs文件, 在空白处按住“shift”键,右键鼠标(注意鼠标不要选中任何文件),点击“Powershell”打开win10powershel ...
- privoxy+ss5实现 HTTP 代理协议转socks5代理
一.系统准备资源 二.ss5安装部署 1.SOCK5代理服务器部署环境准备 IP:10.0.0.100 官网: http://ss5.sourceforge.net/ 下载 yum - ...
- netcfg.exe
netcfg.exe 编辑 本词条缺少信息栏.名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 目录 1 简介 2 可能出现问题 简介编辑 netcfg.exe是Kaspersky的 ...
- Careercup - Microsoft面试题 - 23123665
2014-05-12 07:44 题目链接 原题: Given an array having unique integers, each lying within the range <x&l ...
- 【Clone Graph】cpp
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- Python+Selenium框架设计篇之-简单介绍unittest单元测试框架
前面文章已经简单介绍了一些关于自动化测试框架的介绍,知道了什么是自动化测试框架,主要有哪些特点,基本组成部分等.在继续介绍框架设计之前,我们先来学习一个工具,叫unittest. unit ...
- php 实现栈与队列
<?php class queueOp{ /* * 队尾入队 * Return:处理之后队列的元素个数 */ public function tailEnquque($arr,$val){ re ...
- Spring整合hibernate -SessionFactory
本文目录 1 本文采用 hibernate4 整合 Spring3.1 2 把Spring获取datasource通过class="org.springframework.orm.hibe ...
- Spring Cloud 目录
Spring Cloud Eureka Spring Cloud Config Spring Cloud Feign Spring Cloud Hystrix Spring Cloud Ribbon ...