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. Java生产者消费者

    简单的生产者.消费者,一个数据缓冲区,一个或者多个生产者把数据放入缓冲区.一个或者多个消费者将数据从缓冲区取走.该缓冲区是一个数据共享,必须进行同步处理,如果缓冲区是满的,生产者将不能放数据,同理如果 ...

  2. Go解析写死的json

    func TestAliAfpAdapter_AskAd_Banner(t *testing.T) { apiData := getApiData() apiData.ApiInfo.ApiPosit ...

  3. PHPUnit 手册

    PHPUnit 手册 Sebastian Bergmann 版权 © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 ...

  4. MySQL数据库基础(MySQL5.7安装、配置)

      写在前面: MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQ ...

  5. springmvc 对 jsonp 的支持

    在与前端开发人员合作过程中,经常遇到跨域名访问的问题,通常我们是通过jsonp调用方式来解决.jsop百科:http://baike.baidu.com/link?url=JKlwoETqx2uuKe ...

  6. SharedPreferences封装类

    最近一直在读马伟奇老师的简书,给人以不一样的感觉,接下来的时间会做做笔记,毕竟好东西变成自己的才有用 原文地址SharedPreferencesUtils 依赖 dependencies { comp ...

  7. 地牢逃脱 (BFS)

    题意:给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指 ...

  8. 微信企业号JS-SDK选择图片、上传图片

    因公司项目需要,要修改一个手机端上传图片的一个功能,原本的项目用的是input 的file控件上传的,虽然标注了可以多选,但是在实际运用当中只有iOS手机可以实现多选,Android手机并不支持多选, ...

  9. MSQL的基准测试

    Mysql基准测试 基准测试 直接.简单.易于比较,用于评估服务器的处理能力 压力测试 对真实的月数据进行测试,获得真是系统所能承受的压力 基准测试的目的 1.建立MySQL服务器的性能基准线 2.模 ...

  10. linux memcached Session共享

    memcached memcached是高性能的分布式缓存服务器用来集中缓存数据库查询结果,减少数据库访问次数提高动态web应用的响应速度 传统web架构的问题许多web应用都将数据保存在RDBMS中 ...