P3455 [POI2007]ZAP-Queries
题目描述
Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He has alreadyfound out that whilst deciphering a message he will have to answer multiple queries of the form"for givenintegers aaa, bbb and ddd, find the number of integer pairs (x,y)(x,y)(x,y) satisfying the following conditions:
1≤x≤a1\le x\le a1≤x≤a,1≤y≤b1\le y\le b1≤y≤b,gcd(x,y)=dgcd(x,y)=dgcd(x,y)=d, where gcd(x,y)gcd(x,y)gcd(x,y) is the greatest common divisor of xxx and yyy".
Byteasar would like to automate his work, so he has asked for your help.
TaskWrite a programme which:
reads from the standard input a list of queries, which the Byteasar has to give answer to, calculates answers to the queries, writes the outcome to the standard output.
FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。作为FGD的同学,FGD希望得到你的帮助。
输入输出格式
输入格式:
The first line of the standard input contains one integer nnn (1≤n≤50 0001\le n\le 50\ 0001≤n≤50 000),denoting the number of queries.
The following nnn lines contain three integers each: aaa, bbb and ddd(1≤d≤a,b≤50 0001\le d\le a,b\le 50\ 0001≤d≤a,b≤50 000), separated by single spaces.
Each triplet denotes a single query.
输出格式:
Your programme should write nnn lines to the standard output. The iii'th line should contain a single integer: theanswer to the iii'th query from the standard input.
输入输出样例
2
4 5 2
6 4 3
3
2
Solution:
本题莫比乌斯反演板子题。
题意就是求$\sum_\limits{i=1}^{i\leq n}\sum_\limits{j=1}^{j\leq m} gcd(i,j)==d$。
令$f(n)$表示满足约束条件的且$gcd(i,j)=n$的个数,令$F(n)$表示满足约束条件的且$n|gcd(i,j)$的个数。
于是有$F(n)=\sum_\limits{n|d} {f(d)}$,这个式子显然可以反演,得到$f(n)=\sum_\limits{n|d} {\mu(\frac{d}{n})*F(d)}$。
又因为对于数$x$,显然$n$中含有$\frac{n}{x}$个$x$的倍数,同理$m$中有$\frac{m}{x}$个,所以$F(x)=\frac{n}{x}*\frac{m}{x}$。
则原式变为$f(d)=\sum_\limits{d|k}^{k\leq min(n,m)}{\mu(\frac{k}{d})\frac{n}{k}\frac{m}{k}}$。
令$t=\frac{k}{d}$,则原式变为$f(d)=\sum_\limits{t=1}^{\frac{min(n,m)}{d}}{\frac{n}{td}\frac{m}{td}}$,对于$\lfloor \frac{n}{td}\rfloor \lfloor \frac{m}{td} \rfloor$直接数论分块求就好了,所以还得预处理下$\mu$的前缀和。
时间复杂度$O(q\sqrt n)$。
代码:
/*Code by 520 -- 9.10*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int n,a,b,d,mu[N],prime[N],cnt;
bool isprime[N]; int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void pre(){
mu[]=;
For(i,,){
if(!isprime[i]) mu[i]=-,prime[++cnt]=i;
for(RE int j=;j<=cnt&&prime[j]*i<=;j++){
isprime[i*prime[j]]=;
if(i%prime[j]==) break;
mu[i*prime[j]]=-mu[i];
}
}
For(i,,) mu[i]+=mu[i-];
} int solve(){
if(a>b) swap(a,b);
a/=d,b/=d;
int pos=,ans=;
for(int i=;i<=a;i=pos+){
pos=min(a/(a/i),b/(b/i));
ans+=(mu[pos]-mu[i-])*(a/i)*(b/i);
}
return ans;
} int main(){
pre();
n=gi();
while(n--){
a=gi(),b=gi(),d=gi();
printf("%d\n",solve());
}
return ;
}
P3455 [POI2007]ZAP-Queries的更多相关文章
- 莫比乌斯反演学习笔记+[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 ...
- 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 ...
- BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )
求 answer = ∑ [gcd(x, y) = d] (1 <= x <= a, 1 <= y <= b) . 令a' = a / d, b' = b / d, 化简一下得 ...
- P3455 [POI2007]ZAP-Queries(莫比乌斯反演)
题目 P3455 [POI2007]ZAP-Queries 解析 莫比乌斯反演. 给定\(n\),\(m\),\(d\),求\[\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j ...
- BZOJ1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2951 Solved: 1293[Submit][Status ...
- [POI2007]Zap
bzoj 1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descriptio ...
- Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...
- BZOJ1101 POI2007 Zap 【莫比乌斯反演】
BZOJ1101 POI2007 Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b, ...
- 1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...
随机推荐
- angularjs中audio/video 路径赋值问题
之前解决这个问题都是通过js的attr赋值解决的,但是也一直不明白为什么audio直接在HTML中赋值报错.解决方法就是通过添加$sce过滤效果 app.filter("trustUrl&q ...
- cookie记住密码/base64加密(js控制)
cookie记住密码/base64加密(js控制) • 配置cookie //设置cookie function setCookie ( name, value, expdays ) { var ex ...
- Frida----基本代码
代码来自官网:https://www.frida.re/docs/examples/android/ import frida, sys def on_message(message, data): ...
- C#四则运算器(多态方法实现)
在上一节C#课上,我们学习了用类的继承的方式来做一个四则运算器,然而老师的代码在课上演示的效果并不理想,而且没有使用多态的思想实现,今天我们就来用多态的方式实现四则运算器. 1. 题目及要求 2. A ...
- Halcon三 依据点关系计算物体三维位姿Halcon
1.set_origin_pose( : : PoseIn, DX, DY, DZ : PoseNewOrigin) 平移POSEIN的原点,输出为新的原点.注意,平移沿着OBJ的坐标新进行,而非沿着 ...
- 我看微软收购GitHub
今天是微软收购GitHub的第三天,之前很多人担心被微软收购的GitHub会步Skype,诺基亚等企业的后尘,凡此种种我觉得更多人的担心是:GitHub不再开源免费罢了. GitHub今年4月刚成立十 ...
- 关于docker线上部署时间问题
背景 公司线上部署采用docker swarm方式,这几天线上项目时间突然出了问题(ps:第一反应,我去,这也能出问题,代码里肯定藏毒了),线上时间总跟实际时间差八个小时.本着速战速决的原则,把所有时 ...
- 使用NNI的scikit-learn以及Tensorflow分析
一.NNI简介 NNI (Neural Network Intelligence) 是自动机器学习(AutoML)的工具包. 它通过多种调优的算法来搜索最好的神经网络结构和(或)超参,并支持单机.本地 ...
- 基于C#的机器学习--惩罚与奖励-强化学习
强化学习概况 正如在前面所提到的,强化学习是指一种计算机以“试错”的方式进行学习,通过与环境进行交互获得的奖赏指导行为,目标是使程序获得最大的奖赏,强化学习不同于连督学习,区别主要表现在强化信号上,强 ...
- 自制session
原理 1.面向对象中通过索引的方式访问对象,需要内部实现 __getitem__ .__delitem__.__setitem__方法 2.Tornado框架中,默认执行Handler的get/pos ...