【刷题】洛谷 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的小正方形.对于每个小正方 ... 
随机推荐
- Docker--Dockerfile引用及指令集的功能用法
			Dockerfile引用的官网文档:https://docs.docker.com/engine/reference/builder/ 编写Dockerfiles的最佳实践的官网文档:https:// ... 
- 【坚持】Selenium+Python学习记录 DAY8
			2018/05/ 28 [来源:菜鸟教程](http://www.runoob.com/python3/python3-examples.html) 继续敲类相关的代码 #No.1 class peo ... 
- maven项目的标准目录结构
			maven项目的标准目录结构如下: 
- Windows下用HackRF和SDR#收听FM
			本文内容.开发板及配件仅限用于学校或科研院所开展科研实验! 淘宝店铺名称:开源SDR实验室 HackRF链接:https://item.taobao.com/item.htm?spm=a1z10.1- ... 
- 机器学习(一):记一次k一近邻算法的学习与Kaggle实战
			本篇博客是基于以Kaggle中手写数字识别实战为目标,以KNN算法学习为驱动导向来进行讲解. 写这篇博客的原因 什么是KNN kaggle实战 优缺点及其优化方法 总结 参考文献 写这篇博客的原因 写 ... 
- watch命令详解
			基础命令学习目录首页 原文链接:https://www.cnblogs.com/kaishirenshi/p/7727986.html watch 命令详解: author:headsen chen ... 
- linux命令系列 grep
			grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [ ... 
- 20172308 实验一《Java开发环境的熟悉》实验报告
			20172308 2017-2018-2 <程序设计与数据结构>实验1报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 周亚杰 学号:20172308 实验教师:王 ... 
- linux 环境配置要点
			cd root .bash_profile 这个是配置当前用户的环境变量 cd /etcprofile 这个是配置系统的环境变量 which xxx 查看命令的目录 source .bash_prof ... 
- Beta阶段团队项目开发篇章3
			例会时间 2016.12.6晚 例会照片 个人工作 上阶段任务验收 中英文切换功能已经实现,调查结果分析已经完成,博客基本撰写完成,在征求其他组员意见后发布.任务基本完成. 任务分配 组员 任务内容 ... 
