题目描述

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)的定义一样,最后的式子是:

\[ans=f(d)=\sum_{T=1}^{min(a,b)}\mu(\frac{T}{n})\lfloor \frac{a}{T} \rfloor \lfloor \frac{b}{T} \rfloor
\]

一样的整除分块,一样的前缀和

#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的更多相关文章

  1. 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)

    题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...

  2. 洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301

    https://www.luogu.org/problemnew/show/P3455 就是https://www.cnblogs.com/hehe54321/p/9315244.html里面的方法2 ...

  3. 洛谷P3455 [POI2007]ZAP-Queries

    题目大意: 给定\(n,m,k,\) 求 \[\sum\limits_{x=1}^n\sum\limits_{y=1}^m[gcd(x,y)==k]\] 莫比乌斯反演入门题,先进行一步转化,将每个\( ...

  4. 洛谷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 ...

  5. 洛谷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}\ ...

  6. 莫比乌斯反演学习笔记+[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 ...

  7. 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]

    题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...

  8. [洛谷P3460] [POI2007]TET-Tetris Attack

    洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...

  9. [洛谷3457][POI2007]POW-The Flood

    洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...

随机推荐

  1. jmeter发送http请求(初学者)

    1.jmeter安装配置(百度,这里就不赘述了) 2.添加线程组 测试计划-->添加-->Threads-->线程组 3.线程组配置 线程数:用户数或者并发数,设置为100则有100 ...

  2. JUC——线程同步锁(ReentrantLock)

    ReentrantLock简介 ReentrantLock是一个可重复的互斥锁,又被称为独占锁,可重入的意思是:ReentrantLock锁可以被单个线程多次获取.但是在同一个时间点只能被一个线程锁持 ...

  3. SICP读书笔记 2.3

    SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...

  4. kubernetes高可用设计-CA,etcd

    环境准备: master01:192.168.150.128 master02:192.168.150.130 master03:192.168.150.131 node01:192.168.150. ...

  5. ipython快捷键操作及常用命令

    Ipython shell命令- Ctrl-P 或上箭头键 后向搜索命令历史中以当前输入的文本开头的命令- Ctrl-N 或下箭头键 前向搜索命令历史中以当前输入的文本开头的命令- Ctrl-R 按行 ...

  6. __construct 与 __destruct 区别

    其实这个问法是有问题的,__construct 与 __destruct 没什么可比性,两个方法一个在对象被创建的时候触发,另一个在对象被销毁的时候触发 具体可以翻阅PHP官方手册中的 http:// ...

  7. Beta阶段中间产物【欢迎来怼】

    一.版本控制 ①Git地址:https://git.coding.net/tianjiping/Android-tianjiping.git ②check in次数:7次. ③成员代码贡献 因为阚博文 ...

  8. final发布--PSP Daily软件功能书(最终版)

    一.开发背景 你在完成了一周的软件工程作业后,需要提交一个PSP图表,里面有4项,如下所示: 1.本周PSP表格,包含每项任务的开始.中断.结束.最终时间,格式如下: 2.本周进度条,包含从开始到现在 ...

  9. 必应词典手机版(IOS版)与有道词典(IOS版)之问卷分析

    我们制定了一个调查问卷: 1.年龄分布: 2.地域分布: 3.是否用过必应词典? 对于必应词典还是没用过的人数更多. 4.是否用过有道词典? 有道词典的使用率更高一点. 5.对于必应的基本功能给几分? ...

  10. vim相关命令单独记载

    1. 无敌的可扩展性 1.1 可扩展性给了软件强大的生命 曾几何时,Windows用户对软件的可扩展性没有概念,他们只能对他们使用的软件进行非常有限的定制.扩展软件的权利保留在软件开发者手中.软件的使 ...