[Luogu3455][POI2007]ZAP-Queries
BZOJ(权限题)
Luogu
题目描述
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 aaa, bbb and ddd, find the number of integer pairs (x,y) satisfying the following conditions:
1≤x≤a,1≤y≤b,gcd(x,y)=d, where gcd(x,y)is the greatest common divisor of x and y".
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 nnn (1≤n≤50 000),denoting the number of queries.
The following nnn lines contain three integers each: aaa, bbb and ddd(1≤d≤a,b≤50 000), separated by single spaces.
Each triplet denotes a single query.
输出格式:
Your programme should write nnn lines to the standard output. The iii'th line should contain a single integer: theanswer to the iii'th query from the standard input.
输入输出样例
输入样例#1:
2
4 5 2
6 4 3
输出样例#1:
3
2
sol
参见莫比乌斯反演总结中的举个栗子、骚操作以及奇技淫巧部分
这个题我们要求$$\sum_{i=1}{a}\sum_{j-1}{b}[gcd(i,j)==d]$$
首先使用骚操作把式子化成
\]
以下令\(a/d=n\),\(b/d=m\),且\(n\le m\)。
接着设两个函数\(f(x)\),\(F(x)\),其中
\]
\]
那么我们可以手动脑补出这个关系
\]
所以\(F(x)\)是可以\(O(1)\)算的,处理出全部的\(F(x)\),总时间复杂度是\(O(n)\)
又根据莫比乌斯反演可得
\]
因为我们需要求的是\(f(1)\)所以把\(x=1\)代入得
\]
所以我们只要搞出所有的\(F(x)\)跟所有的\(\mu(x)\)就可以了
但是每组数据都这么搞一遍,\(O(Tn)\)的时间复杂度承受不了啊怎么办。
这里就运用到奇技淫巧中提到的数论分块。
可以证明,\(F(x)\)在\([1,n]\)上只有\(O(\sqrt n)\)种取值。
我们维护一个\(\mu(x)\)的前缀和,然后把\(F(x)\)值相同的放在一起算贡献。
总复杂度变为\(O(T\sqrt n)\)
具体实现参见代码。
code
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 50005;
const int n = 50000;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
int mu[N],pri[N],tot,s[N];
bool zhi[N];
void Mobius()
{
zhi[1]=true;mu[1]=1;
for (int i=2;i<=n;i++)
{
if (!zhi[i]) pri[++tot]=i,mu[i]=-1;
for (int j=1;j<=tot&&i*pri[j]<=n;j++)
{
zhi[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];
}
ll calc(int a,int b,int k)
{
a/=k;b/=k;
if (a>b) swap(a,b);
int i=1,j;ll ans=0;
while (i<=a)
{
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()
{
int T=gi();
Mobius();
while (T--)
{
int a=gi(),b=gi(),k=gi();
printf("%lld\n",calc(a,b,k));
}
return 0;
}
[Luogu3455][POI2007]ZAP-Queries的更多相关文章
- BZOJ 1101: [POI2007]Zap
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2262 Solved: 895[Submit][Status] ...
- [BZOJ1101][POI2007]Zap
[BZOJ1101][POI2007]Zap 试题描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd ...
- BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )
求 answer = ∑ [gcd(x, y) = d] (1 <= x <= a, 1 <= y <= b) . 令a' = a / d, b' = b / d, 化简一下得 ...
- BZOJ1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2951 Solved: 1293[Submit][Status ...
- 莫比乌斯反演学习笔记+[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 ...
- [POI2007]Zap
bzoj 1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descriptio ...
- Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...
- BZOJ1101 POI2007 Zap 【莫比乌斯反演】
BZOJ1101 POI2007 Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b, ...
- 1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...
随机推荐
- 调试 smallcorgi/Faster-RCNN_TF 的demo过程遇到的问题
最近在调试faster R-CNN时,遇到了各种各样的问题.使用的算法库为https://github.com/smallcorgi/Faster-RCNN_TF 注:本文使用的是通过virtuale ...
- Docker可视化管理工具Shipyard安装与配置
Shipyard简介 Shipyard是一个集成管理docker容器.镜像.Registries的系统,它具有以下特点: 1.支持多节点的集成管理 2.可动态加载节点 3.可托管node下的容器 镜像 ...
- 关于ruby -gem无法切换淘宝源
ruby官网提供的 淘宝的gem源 不起作用 https://ruby.taobao.org/ taobao Gems 源已停止维护,现由 ruby-china 提供镜像服务 http://gems. ...
- java设计模式-----2、工厂方法模式
再看工厂方法模式之前先看看简单工厂模式 工厂方法模式(FACTORY METHOD)同样属于一种常用的对象创建型设计模式,又称为多态工厂模式,此模式的核心精神是封装类中不变的部分,提取其中个性化善变的 ...
- 几种优化ajax的执行速度的方法
1.尽量使用局部的变量,而不使用全局变量: 2.优化for循环 3.尽量少用eval,每次使用eval都需要消耗大量的时间: 4.将DOM节点放在文档上. 5.尽量减少点好(.)操作符号的使用
- linux 安装 sftp
1,sftp:登陆命令 Xshell:\> sftp root@192.168.159.128 Connecting to 192.168.159.128:22... Connection es ...
- 了解一下Http常见状态码、Http协议的工作特点和原理、Http请求Post与Get的区别
HTTP协议常见状态码状态码的作用负责标记客户端请求服务器的返回结果,标记服务器端的处理是否正常,通知出现的错误等等职责,借助客户端可以知道客户端是否正常请求服务端.五大类:1XX(信息类状态码,接收 ...
- 由select引发的思考
一.前言 网络编程里一个经典的问题,selec,poll和epoll的区别?这个问题刚学习编程时就接触了,当时看了材料很不明白,许多概念和思想没有体会,现在在这个阶段,再重新回头看这个问题,有一种豁然 ...
- nyoj161 取石子 (四) 威佐夫博弈
思路:详细证明见博弈总结 如何判断威佐夫博弈的奇异局势? 对于状态(a, b),c = b - a,如果是奇异局势必定满足 a == c * (1+√5)/ 2. AC代码 #include < ...
- (转载,但不知道谁原创)获取SPRING 代理对象的真实实例,可以反射私有方法,便于测试
/** * 获取 目标对象 * @param proxy 代理对象 * @return * @throws Exception */ public static Object getTarget(Ob ...