【Luogu3455】【POI2007】ZAP-Queries(莫比乌斯反演)
【Luogu3455】【POI2007】ZAP-Queries(莫比乌斯反演)
题面
题目描述
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 nn (1\le n\le 50\ 0001≤n≤50 000),denoting the number of queries.
The following nn lines contain three integers each: aa, bb and dd(1\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 nn lines to the standard output. The ii'th line should contain a single integer: theanswer to the ii'th query from the standard input.
输入输出样例
输入样例#1:
2
4 5 2
6 4 3
输出样例#1:
3
2
题解
和前面那一道HDU1695GCD是一样的
直接蒯过代码
然后就会获得70分
这样做的复杂度是\(O(Tn)\)
这题会超时
那么,考虑计算的时候。
\(g(i)=(\frac bk/i)·(\frac dk/i)\)
其中一定会有连续的一段使得\(g(i)\)的值是不会变化的
(Gay神说这叫数论分块,复杂度\(O(\sqrt{n}\))
因此,预处理出\(\mu\)的前缀和
利用数论分块即可在\(O(T\sqrt{n})的复杂度里计算出来\)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 101000
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int mu[MAX],pri[MAX],tot,s[MAX];
long long g[MAX],n,a,b,K;
bool zs[MAX];
void Get()
{
zs[1]=true;mu[1]=1;
for(int i=2;i<=n;++i)
{
if(!zs[i])pri[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot&&i*pri[j]<=n;++j)
{
zs[i*pri[j]]=true;
if(i%pri[j])mu[i*pri[j]]=-mu[i];
else {mu[i*pri[j]]=0;break;}
}
}
for(int i=1;i<=n;++i)s[i]=s[i-1]+mu[i];
}
long long Calc(int a,int b,int K)
{
a/=K;b/=K;
long long ans=0;
int i=1;
if(a>b)swap(a,b);
while(i<=a)
{
int j=min(a/(a/i),b/(b/i));
ans+=1ll*(s[j]-s[i-1])*(a/i)*(b/i);
i=j+1;
}
return ans;
}
int main()
{
n=100000;
Get();
int T=read();
while(T--)
{
a=read();b=read();K=read();
printf("%lld\n",Calc(a,b,K));
}
return 0;
}
【Luogu3455】【POI2007】ZAP-Queries(莫比乌斯反演)的更多相关文章
- [luogu3455][POI2007]ZAP-Queries【莫比乌斯反演】
题目描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得 ...
- 【BZOJ】1101 [POI2007]Zap(莫比乌斯反演)
题目 传送门:QWQ 分析 莫比乌斯反演. 还不是很熟练qwq 代码 //bzoj1101 //给出a,b,d,询问有多少对二元组(x,y)满足gcd(x,y)=d.x<=a,y<=b # ...
- BZOJ1101 POI2007 Zap 【莫比乌斯反演】
BZOJ1101 POI2007 Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b, ...
- 【BZOJ1101】[POI2007] Zap(莫比乌斯反演)
点此看题面 大致题意: 求\(\sum_{x=1}^N\sum_{y=1}^M[gcd(x,y)==d]\). 一道类似的题目 推荐先去做一下这道题:[洛谷2257]YY的GCD,来初步了解一下莫比乌 ...
- BZOJ 1101 [POI2007]Zap(莫比乌斯反演)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1101 [题目大意] 求[1,n][1,m]内gcd=k的情况 [题解] 考虑求[1,n ...
- ☆ [POI2007] ZAP-Queries 「莫比乌斯反演」
题目类型:莫比乌斯反演 传送门:>Here< 题意:求有多少对正整数对\((a,b)\),满足\(0<a<A\),\(0<b<B\),\(gcd(a,b)=d\) ...
- 洛谷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}\ ...
- 【BZOJ】1101: [POI2007]Zap(莫比乌斯+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=1101 无限膜拜数论和分块orz 首先莫比乌斯函数的一些性质可以看<初等数论>或<具 ...
- [POI2007]ZAP-Queries (莫比乌斯反演+整除分块)
[POI2007]ZAP-Queries \(solution:\) 唉,数论实在有点烂了,昨天还会的,今天就不会了,周末刚证明的,今天全忘了,还不如早点写好题解. 这题首先我们可以列出来答案就是: ...
- 洛谷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 ...
随机推荐
- ubuntu 开发板ping通虚拟机挂载nfs服务器
先.nfs服务配置1.设置开发板ip ,同一网段2.开发板上操作:ifconfig eth0 192.168.1.203.测试是否能够ping通:ping 192.168.1.194.测试开发板ip是 ...
- shared_lock and unique_lock
简单的说: shared_lock是read lock.被锁后仍允许其他线程执行同样被shared_lock的代码.这是一般做读操作时的需要. unique_lock是write lock.被锁后不允 ...
- Phalcon调试大杀器之phalcon-debugbar安装
Phalcon 是一款非常火的高性能C扩展php开发框架.特点是高性能低耦合,但遗憾的是长期缺少一款得力的调试辅助工具. 目前版本主要以Laravel debugbar的具有功能为蓝本开发,并针对ph ...
- 05-Git
[Git] [安装git] $ yum install git #安装git $ ssh-keygen #遇到输入符直接回车 $ cat ~/.ssh/id_rsa.pub #将这里的信息添加 ...
- POJ 1023 The Fun Number System
Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...
- 【考试】java基础知识测试,看你能得多少分?
1 前言 共有5道java基础知识的单项选择题,每道20分,共计100分.解析和答案在最后. 2 试题 2.1 如下程序运行结果是什么? class Parent { public Parent(St ...
- Mybatis查询,resultMap="Map" 查询数据有空值,导致整个map为空的问题
解决方法,不要使用Map接收,使用HashMap或者LinkHashMap,都可以. resultMap="Map" 替换为: resultMap="HashMap&qu ...
- Ubantu16.04 redis安装
通过FTP方式将redis的安装包从windows上传到linux上 解压命令:$sudo tar -zxf ~/Downloads/redis-3.2.7.tar.gz -C /usr/local ...
- CentOs 7 中安装tomcat8
1,下载tomcat8.0 进入tomcat的下载地址:http://tomcat.apache.org/download-80.cgi 2,上传到linux服务器 cd /usr/local/jav ...
- JVM性能监控与故障处理命令汇总(jps、jstat、jinfo、jmap、jhat、jstack)
给一个系统定位问题的时候,知识.经验是关键基础,数据是依据,工具才是运用知识处理数据的手段 使用适当的虚拟机监控和分析的工具可以加快我们分析数据.定位解决问题的速度,本文主要介绍了几款服 务器上常用的 ...