【刷题】洛谷 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 aa , bb and dd , find the number of integer pairs \((x,y)\) satisfying the following conditions:
$1\le x\le a $,\(1\le y\le b\) ,\(gcd(x,y)=d\), where \(gcd(x,y)\) is the greatest common divisor of xx and yy ".
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 \(n\) (\(1\le n\le 50\ 000\) ),denoting the number of queries.
The following \(n\) lines contain three integers each: \(a\) , \(b\) and \(d\) (\(1\le d\le a,b\le 50\ 000\) ), separated by single spaces.
Each triplet denotes a single query.
输出格式:
Your programme should write nn lines to the standard output. The \(i\) 'th line should contain a single integer: theanswer to the \(i\) 'th query from the standard input.
输入输出样例
输入样例#1:
2
4 5 2
6 4 3
输出样例#1:
3
2
题解
这题其实还是上一篇的弱化版,f(x)和F(x)的定义一样,最后的式子是:
\]
一样的整除分块,一样的前缀和
#include<bits/stdc++.h>
#define ll long long
const int MAXN=50000+10;
ll T,mu[MAXN],s[MAXN],prime[MAXN],cnt;
bool vis[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(c!='\0')putchar(c);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void init()
{
memset(vis,1,sizeof(vis));
vis[0]=vis[1]=0;
mu[1]=1;
for(register int i=2;i<MAXN;++i)
{
if(vis[i])
{
prime[++cnt]=i;
mu[i]=-1;
}
for(register int j=1;j<=cnt&&i*prime[j]<MAXN;++j)
{
vis[i*prime[j]]=0;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else break;
}
}
for(register int i=1;i<MAXN;++i)s[i]=s[i-1]+mu[i];
}
inline ll solve(ll a,ll b,ll d)
{
ll res=0;
for(register ll i=1;;)
{
if(i>min(a,b))break;
ll j=min(a/(a/i),b/(b/i));
res+=(a/i)*(b/i)*(s[j/d]-s[(i-1)/d]);
i=j+1;
}
return res;
}
int main()
{
init();
read(T);
while(T--)
{
ll a,b,d;
read(a);read(b);read(d);
write(solve(a,b,d),'\n');
}
return 0;
}
【刷题】洛谷 P3455 [POI2007]ZAP-Queries的更多相关文章
- 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)
题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...
- 洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301
https://www.luogu.org/problemnew/show/P3455 就是https://www.cnblogs.com/hehe54321/p/9315244.html里面的方法2 ...
- 洛谷P3455 [POI2007]ZAP-Queries
题目大意: 给定\(n,m,k,\) 求 \[\sum\limits_{x=1}^n\sum\limits_{y=1}^m[gcd(x,y)==k]\] 莫比乌斯反演入门题,先进行一步转化,将每个\( ...
- 洛谷P3455 [POI2007]ZAP-Queries(莫比乌斯反演)
传送门 设$$f(k)=\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)=k]$$ $$g(n)=\sum_{n|k}f(k)=\lfloor\frac{a}{n}\rflo ...
- 洛谷P3455 [POI2007]ZAP-Queries (莫比乌斯反演)
题意:求$\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)==d]$(1<=a,b,d<=50000). 很套路的莫比乌斯反演. $\sum_{i=1}^{n}\ ...
- 莫比乌斯反演学习笔记+[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 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...
- [洛谷P3460] [POI2007]TET-Tetris Attack
洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...
- [洛谷3457][POI2007]POW-The Flood
洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...
随机推荐
- Java 中的接口
接口概念 接口可以理解为一种特殊的类,由全局常量和公共抽象方法组成.类是一种具体实现体,而接口定义了某一些类所要遵守的规范,接口不必关心这些类的内部数据,也不关心这些类里方法的实现细节,它只规定这些类 ...
- 模拟websocket推送消息服务mock工具二
模拟websocket推送消息服务mock工具二 在上一篇博文中有提到<使用electron开发一个h5的客户端应用创建http服务模拟后端接口mock>使用electron创建一个模拟后 ...
- Kickstart Round H 2018
打了ks好久都没有更新 诶,自己的粗心真的是没救了,A题大数据都能错 A #include <iostream> #include <cstdio> #include < ...
- Python输出格式全总结
输入输出 有几种方法可以显示程序的输出:数据可以以人类可读的形式打印出来,或者写入文件以供将来使用.本章将讨论一些可能性. 更漂亮的输出格式 到目前为止,我们遇到了两种写入值的方法:表达式语句 和 p ...
- python-分叉树枝
import turtle def draw_branch(length): #绘制右侧树枝 if length >5: if length == 10: turtle.pencolor('gr ...
- React Native (0.57)开发环境搭建(注意:Node不要随便更新到最新版,更新完后莫名其妙的问题一大堆)
搭建开发环境 一.安装依赖 必须安装的依赖有:Node.Watchman 和 React Native 命令行工具以及 Xcode. 1.首先安装 Homebrew 2.安装 Node, Watchm ...
- 互评Beta版本(Hello World!——SkyHunter)
1 基于NABCD评论作品,及改进建议 SkyHunter这款游戏我很喜欢,小时候总玩飞机类的游戏,这款游戏我上课的时候试玩了,在我电脑上运行是很好玩的,音乐震撼,画面玄幻,富有金属音乐的味道,游戏内 ...
- [2019BUAA软工]第0次代码作业
Visual Studio 单元测试的简单应用 写在前面 笔者根据作业的介绍以及Visual Studio 2017 文档的相关说明,进行了Visual Studio 单元测试的简单应用. Tip ...
- 第一阶段Spring个人总结
通过这一阶段的冲刺,我感到的是名义上的团队,而实际上却是一个人的事,每个人跟每个人都不一样,都有自己的特点,总会出些不必要的麻烦. 还有团队的进展也是看不到什么东西,说实话,这次我并没有太多关注团队的 ...
- GIT的使用及心得
先给大家个很棒的GIT使用教程链接http://blog.jobbole.com/78960/ 这个链接是我在学习使用GIT时看的教程博客,分享给大家,里面还有介绍SVN与GIT的区别 GITHUB上 ...