题目类型:莫比乌斯反演

传送门:>Here<

题意:求有多少对正整数对\((a,b)\),满足\(0<a<A\),\(0<b<B\),\(gcd(a,b)=d\)

解题思路

学了莫比乌斯反演,就以这道题来介绍一下莫比乌斯反演的题的应用(下文中,对数表示在规定范围内满足特定条件的数对数量,不是\(log\)的那个对数)

一般碰到有关\(gcd\)的题,一般地,设\(f(n)\)表示\(gcd=n\)的对数,\(F(n)\)表示\(n|gcd\)的对数

根据定义,满足$$F(n)=\sum\limits_{n|d}f(d)$$因此利用公式二进行反演$$f(n)=\sum\limits_{n|d}μ(\dfrac{d}{n})F(d)$$我们之所以要反演,是因为\(F\)比\(f\)更好求。我们根据\(gcd\)的性质,发现其实\(F\)就是要求两数都为\(n\)的倍数的对数。因此根据乘法原理,也就是$$\left \lfloor \dfrac{A}{n} \right \rfloor * \left \lfloor \dfrac{B}{n} \right \rfloor$$。可以\(O(1)\)求得

因此只需要筛完\(μ\)之后直接求\(f(d)\)作为答案即可。复杂度\(O(n)\)

然而\(O(n)\)的复杂度不足以满足题目要求,因此需要数论分块(整除分块)来优化到\(O(\sqrt{n})\)即可。关于整除分块的用法见上一篇。

Code

/*By DennyQi 2018*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
using namespace std;
typedef long long ll;
const int MAXN = 50010;
const int INF = 1061109567;
const int LIM = 50010;
inline int Max(const int a, const int b){ return (a > b) ? a : b; }
inline int Min(const int a, const int b){ return (a < b) ? a : b; }
inline int read(){
int x = 0; int w = 1; register char c = getchar();
for(; c ^ '-' && (c < '0' || c > '9'); c = getchar());
if(c == '-') w = -1, c = getchar();
for(; c >= '0' && c <= '9'; c = getchar()) x = (x<<3) + (x<<1) + c - '0'; return x * w;
}
int T,A,B,d,ans,last,R;
int mu[MAXN],b[MAXN],prime[MAXN],sumu[MAXN],tot;
inline void getMobius(){
mu[1] = 1;
for(int i = 2; i <= LIM; ++i){
if(!b[i]){
prime[++tot] = i;
mu[i] = -1;
}
for(int j = 1; j <= tot; ++j){
if(i * prime[j] > LIM) break;
b[i * prime[j]] = 1;
if(i % prime[j] == 0){
mu[i * prime[j]] = 0;
break;
}
else{
mu[i * prime[j]] = -mu[i];
}
}
}
for(int i = 1; i <= LIM; ++i){
sumu[i] = sumu[i-1] + mu[i];
}
}
int main(){
getMobius();
T = r;
while(T--){
A = r, B = r, d = r;
ans = 0;
A /= d, B /= d;
for(int i = 1, j; i <= Min(A,B); i = j+1){
j = Min(A/(A/i), B/(B/i));
ans += (A/i) * (B/i) * (sumu[j] - sumu[i-1]);
}
printf("%d\n", ans);
}
return 0;
}

☆ [POI2007] ZAP-Queries 「莫比乌斯反演」的更多相关文章

  1. 「BZOJ 3529」「SDOI 2014」数表「莫比乌斯反演」

    题意 有一张 \(n\times m\) 的数表,其第\(i\)行第\(j\)列的数值为能同时整除\(i\)和\(j\)的所有自然数之和. \(T\)组数据,询问对于给定的 \(n,m,a\) , 计 ...

  2. 「BZOJ 3994」「SDOI 2015」约数个数和「莫比乌斯反演」

    题意 设\(d(x)\)为\(x\)的约数个数,求\(\sum_{i=1}^{n}\sum_{j=1}^{m}d(ij)\). 题解 首先证个公式: \[d(ij) = \sum_{x|i}\sum_ ...

  3. 「CF235E」Number Challenge「莫比乌斯反演」

    一个结论:(从二维扩展来的,三维也是对的,证明可以考虑质因数分解) \[ d(ijk)=\sum_{i'|i}\sum_{j'|j}\sum_{k'|k}[\gcd(i',j')=1][\gcd(i' ...

  4. 【Luogu3455】【POI2007】ZAP-Queries(莫比乌斯反演)

    [Luogu3455][POI2007]ZAP-Queries(莫比乌斯反演) 题面 题目描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x ...

  5. Note -「单位根反演」学习笔记

    \(\mathcal{Preface}\)   单位根反演,顾名思义就是用单位根变换一类式子的形式.有关单位根的基本概念可见我的这篇博客. \(\mathcal{Formula}\)   单位根反演的 ...

  6. BZOJ 1101 [POI2007]Zap | 第一道莫比乌斯反(繁)演(衍)

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1101 题解: http://www.cnblogs.com/mrha/p/8203612.h ...

  7. luogu3455 [POI2007]ZAP-Queries 简单的莫比乌斯反演

    link ms是莫比乌斯反演里最水的题... 题意:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 多组询问, T<=50000, ...

  8. Note -「Mobius 反演」光速入门

    目录 Preface 数论函数 积性函数 Dirichlet 卷积 Dirichlet 卷积中的特殊函数 Mobius 函数 & Mobius 反演 Mobius 函数 Mobius 反演 基 ...

  9. 【BZOJ2301】【HAOI2011】Problem B(莫比乌斯反演)

    [BZOJ2301][HAOI2011]Problem B(莫比乌斯反演) 题面 Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y ...

随机推荐

  1. grid++报表使用时注意事项

    #开始使用:Grid++Report 可以在 Visual C#.Net 与 Visual Basic.Net 下的 WinForm 项目中使用.在项目中使用 Grid++Report 之前,首先必须 ...

  2. 一幅图,看懂中国CMMI

    以下数据由Fancier凡奉信息根据历年CMMI Institute公布的CMMI评估结果的汇总整理.数据跨度2008-2017年,包含对中国CMMI与全球CMMI的不同等级.版本的统计.

  3. Spark SQL,如何将 DataFrame 转为 json 格式

    今天主要介绍一下如何将 Spark dataframe 的数据转成 json 数据.用到的是 scala 提供的 json 处理的 api. 用过 Spark SQL 应该知道,Spark dataf ...

  4. C#学习笔记---C#操作SQL数据库

    C#操作SQL数据库 Connection(连接)对象 连接字符串: 形式1.”server=;uid=;pwd=;database=” 形式2.”server=;Intergrated Securi ...

  5. AngularJS学习之旅—AngularJS SQL(十二)

    一.使用 PHP 从 MySQL 中获取数据 <div ng-app="myApp" ng-controller="customersCtrl"> ...

  6. Enterprise architect 类图加时序图

    原文地址:https://segmentfault.com/a/1190000005639047#articleHeader2 新建一个Project 没什么好说的,“文件-新建项目”,然后选择保存位 ...

  7. github使用个人总结

    1.获取github上面的源码时候,不能获取最新的,因为你的开发工作不一定是最新的要下载历史版本. 2.要使用里面的文件的时候,可以在目录后面url后面添加downloads 这样可以找到封装好的版本 ...

  8. 阿里巴巴开源的Asynchronous I/O Design and Implementation

    Motivation I/O access, for the most case, is a time-consuming process, making the TPS for single ope ...

  9. echarts之legend-改变图例的图标为自定义图片

    legend:{ show:true, orient:'horizontal', borderColor:'#df3434', borderWidth:2, data:[ { name:'蒸发量', ...

  10. linux下 启动node 和关闭node

    1.用forever  进行管理 npm install -g forever forever start app.js //启动 forever stop app.js //关闭 2.用自带的服务n ...