[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正在破解一段密码,他需要回答很多类似的问题:对于给定 ...
随机推荐
- 【技术干货】git常用命令
2.1 git init语法: git init在当前目录初始化git仓库,适用于尚未使用git管理的项目2.2 git clone语法: git clone <url>例如: git c ...
- abp允许跨域代码,时间转换为固定格式,本地时间
在Global的 Application_BeginRequest方法中: Thread.CurrentThread.CurrentCulture = new CultureInfo("z ...
- [翻译]编写高性能 .NET 代码 第一章:性能测试与工具 -- 平均值 vs 百分比
<<返回目录 平均值 vs 百分比 在考虑要性能测试的目标值时,我们需要考虑用什么统计口径.大多数人都会首选平均值,但在大多数情况下,这个正确的,但你也应该适当的考虑百分数.但你有可用性的 ...
- 手把手教你树莓派实现简易室内监控系统(C)之BOA服务器的搭建
本篇主要讲利用BOA服务器做室内监控系统的服务器端. 古人云:万事开头靠百度,实在不行就Google.小编也是一步一步的,亲自搭建成功,不能说是万全之策,仅仅是给大家一个参考就满足了. 第一步: 1. ...
- java常用类————Date类
Date类在Java.util包中. 一.功能介绍:创建Date对象,获取时间,格式化输出的时间. 二.对象创建:1.使用Date类无参数的构造方法创建的对象可以获取本地时间.例如: Date now ...
- Egret学习笔记 (Egret打飞机-1.大致思路)
大致看了一遍Egret的官方文档,就开始打算使用Egret来开发一个打飞机游戏. 首先来捋一捋思路,先来看一看一个打飞机游戏的图片 基本上一个打飞机游戏分为 开始游戏 ----------进入游戏 ...
- Office 365 共享链接直接进入编辑
首先在Word online共享文档(不多赘述) 但这个链接打开的是预览视图,要点击右上角的"在浏览器中编辑"才能真正编辑. 但是很多情况都是没必要进入这个预览界面再编辑的.这多点 ...
- SpringBoot CGLIB AOP解决Spring事务,对象调用自己方法事务失效.
对于像我这种喜欢滥用AOP的程序员,遇到坑也是习惯了,不仅仅是事务,其实只要脱离了Spring容器管理的所有对象,对于SpringAOP的注解都会失效,因为他们不是Spring容器的代理类,Sprin ...
- 关于xlrd处理合并单元格
先埋个雷, 最近在做通过excel读取接口测试用例~ 流程等都是自己制定的,打算做完了之后放到GitHub上去哈哈哈. 正式进入正题~ 在写这个框架的时候,遇到了一个问题,就是同一个接口,需要为他准备 ...
- 20170109| javascript记录
1.时间戳的使用: 在php中的时间戳是以秒为单位的,在js中转换过来的时间戳是以毫秒为单位的.当我们使用js和php同时开发的时候,就需要保证它们都是处于同一时间单位下才好进行相关的计算. 首先说一 ...