【刷题】洛谷 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的小正方形.对于每个小正方 ...
随机推荐
- python3 拼接字符串的7种方法
1.直接通过(+)操作符拼接 1 2 >>> 'Hello' + ' ' + 'World' + '!' 'Hello World!' 使用这种方式进行字符串连接的操作效率低下,因为 ...
- 编写和调试Android下JNI程序流程
1,切换到Android目录下bin/classes,使用javah命令生成jni所需的头文件,命令类似于:javah com.xxx.ooo,其中,com.xxx为package名称,ooo为包含n ...
- mpstat命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858775.html 简介 mpstat是Multipro ...
- ltrace命令详解
原文链接:https://ipcmen.com/ltrace 用来跟踪进程调用库函数的情况 补充说明 NAME ltrace - A library call tracer ltrace命 ...
- centos7安装oracle的一些问题
在配置监听的时候尝试了很多次都是不能创建,最后将 /data/oracle/product/11.2.0/db_1/network/admin目录下的listener.ora和tnsname.ora两 ...
- [2017BUAA软工]结对项目:数独扩展
结对项目:数独扩展 1. Github项目地址 https://github.com/Slontia/Sudoku2 2. PSP估计表格 3. 关于Information Hiding, Inter ...
- 第二阶段每日站立会议Forth Day
昨天对于程序中的字体显示进行细化修改,使界面更美观 今天准备继续调试手机界面 遇到的问题:上几次Tomcat运行正常,今天突然出现问题,Tomcat服务可以打开,但是无法连接到数据库
- 根据C#编程经验思考编程核心
程序是对数据的各种操作.数据的表示,数据的组织结构,数据的存储,数据的处理,数据的传输等. 程序是由具体的编程语言编写的,不同的编程语言有编写,编译检查,解释执行等过程. 具体的编程语言都有: 1,变 ...
- 浅谈Java中的Hashmap
HashMap: java.lang.Object ∟ java.util.AbstractMap<K,V> ∟ java.util.HashMap<K,V> 类型参数: ...
- 西门子S7系列PLC的主要种类及应用软件
德国西门子(SIEMENS)公司生产的可编程序控制器在我国的应用也相当广泛,在冶金.化工.印刷生产线等领域都有应用.西门子(SIEMENS)公司的PLC产品包括LOGO,S7-200,S7-300,S ...