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]$$

首先使用骚操作把式子化成

\[\sum_{i=1}^{a/d}\sum_{j=1}^{b/d}[gcd(i,j)==1]
\]

以下令\(a/d=n\),\(b/d=m\),且\(n\le m\)。

接着设两个函数\(f(x)\),\(F(x)\),其中

\[f(x)=\sum_{i=1}^{n}\sum_{j-1}^{m}[gcd(i,j)==x]
\]

\[F(x)=\sum_{x|d}^{n}f(d)
\]

那么我们可以手动脑补出这个关系

\[F(x)=\lfloor\frac nx\rfloor\lfloor\frac mx\rfloor
\]

所以\(F(x)\)是可以\(O(1)\)算的,处理出全部的\(F(x)\),总时间复杂度是\(O(n)\)

又根据莫比乌斯反演可得

\[f(x)=\sum_{x|d}^{n}\mu(\frac dx)F(d)
\]

因为我们需要求的是\(f(1)\)所以把\(x=1\)代入得

\[f(1)=\sum_{d=1}^{n}\mu(d)F(d)
\]

所以我们只要搞出所有的\(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的更多相关文章

  1. BZOJ 1101: [POI2007]Zap

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2262  Solved: 895[Submit][Status] ...

  2. [BZOJ1101][POI2007]Zap

    [BZOJ1101][POI2007]Zap 试题描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd ...

  3. BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )

    求 answer = ∑ [gcd(x, y) = d] (1 <= x <= a, 1 <= y <= b) . 令a' = a / d, b' = b / d, 化简一下得 ...

  4. BZOJ1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2951  Solved: 1293[Submit][Status ...

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

  6. [POI2007]Zap

    bzoj 1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Descriptio ...

  7. Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...

  8. BZOJ1101 POI2007 Zap 【莫比乌斯反演】

    BZOJ1101 POI2007 Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b, ...

  9. 1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...

随机推荐

  1. LNMP搭建01 -- 编译安装MySQL 5.6.14 和 LNMP相关的区别

    [编译安装MySQL 5.6.14] [http://www.cnblogs.com/xiongpq/p/3384681.html ]  [mysql-5.6.14.tar.gz 下载] http:/ ...

  2. centos ELK安装

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn ELK是进行日志收集分析用的,具体工作.原理.作用自行google. ...

  3. Java SocketChannel 读取ByteBuffer字节的处理模型

    在JAVA中的流分为字节流或字符流,一般来说采用字符流处理起来更加方便.字节流处理起来相对麻烦,SocketChannel中将数据读取到ByteBuffer中,如何取出完整的一行数据(使用CRLF分隔 ...

  4. idea 使用debugger技巧

    1,背景 每个开发人员每天都离不开debugger,只要你在编码,就需要调试,作为一个开发快10年的老程序员每天都要写很多代码,当每个人接到任务的时候都会想,这些功能其实很快就能写完,没错,对于写代码 ...

  5. 手把手教你树莓派实现简易室内监控系统(A)

    第一次写博文,有很多疏漏之处,然后受逼乎影响较深,希望大家多多包涵! _______________________________________________分割线是这样画的吧_________ ...

  6. Samba服务器的安装与配置

    Samba服务器主要的功能是实现本地windows系统下方便读写局域网内虚拟机下的文件: Samba与window连接需要使用NetBIOS协议,请确认你的Windows系统已经安装了NetBIOS协 ...

  7. 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;

    //第一题是猜数字的游戏.//题目:随即产生一个3位的正整数,让你进行猜数字,//如果猜小了,输出:"猜小了,请继续".//如果猜大了,输出:"猜大了,请继续" ...

  8. Django 学习笔记

    day 1 : 一.web 框架本质: 1.http 建立在tcp 之上:一次互通后断开,无状态,短链接 请求头: b'GET / HTTP/1.1 Host: 127.0.0.1:8080 Conn ...

  9. UVA - 242 线性DP

    题意:给定多种邮票的组合,邮票最多只能用S张,这些邮票能组成许多不同面额,问最大连续面额的长度是多少,如果有多个组合输出组合中邮票数量最少的,如果仍有长度一致的,输出邮票从大到小排序后字典序最大的那个 ...

  10. Java Enum总结

    枚举类型 枚举类型是Java5新增的特性之一,枚举是一种特殊类型的类,其枚举的每一个值 都是该枚举类的一个实例.枚举类型是作为Java语言的一部分,是完全类型安全的, 编译器会帮助我们检查枚举类型的正 ...