Luogu P2257 YY的GCD 莫比乌斯反演
第一道莫比乌斯反演。。。$qwq$
设$f(d)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)==d]$
$F(n)=\sum_{n|d}f(d)=\lfloor \frac{N}{n} \rfloor \lfloor \frac{M}{n} \rfloor$
$f(n)=\sum_{n|d}\mu(\frac{d}{n})F(d)$
$ans=\sum_{p\in pri}f(p)$
$=\sum_{p\in pri}\sum_{p|d}\mu(\frac{d}{p})F(d)$
$=\sum_{d=1}^{min(N,M)}\sum_{p\in pri且p|d}\space\mu(\frac{d}{p})F(d)$
$=\sum_{d=1}^{min(N,M)}F(d)\sum_{p\in pri且p|d}\space\mu(\frac{d}{p})$
$=\sum_{d=1}^{min(N,M)}\lfloor \frac{N}{d} \rfloor \lfloor \frac{M}{d} \rfloor \sum_{p\in pri且p|d}\space\mu(\frac{d}{p})$
对于$\lfloor \frac{N}{d} \rfloor \lfloor \frac{M}{d} \rfloor$用整除分块,对于$a(d)=\sum_{p\in pri且p|d}\space\mu(\frac{d}{p})$用一个类似埃筛的思路把$a(d)$筛出来然后做一个前缀和。。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define R register int
using namespace std;
namespace Fread {
static char B[<<],*S=B,*D=B;
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
}using Fread::g;
int t,n,m,cnt,pri[],a[],mu[];
bool v[]; long long sum[];
inline void MU(int n) { mu[]=;
for(R i=;i<=n;++i) {
if(!v[i]) pri[++cnt]=i,mu[i]=-;
for(R j=;j<=cnt&&i*pri[j]<=n;++j) {
v[i*pri[j]]=true;
if(i%pri[j]==) break;
else mu[i*pri[j]]=-mu[i];
}
} for(R j=;j<=cnt;++j) for(R i=;i*pri[j]<=n;++i) a[i*pri[j]]+=mu[i];
for(R i=;i<=n;++i) sum[i]=sum[i-]+(ll)a[i];
}
signed main() {
#ifdef JACK
freopen("NOIPAK++.in","r",stdin);
#endif
MU(); t=g(); while(t--) { register long long ans=;
n=g(),m=g(); n>m?(void)(swap(n,m)):(void);
for(R l=,r;l<=n;l=r+) {
r=min(n/(n/l),m/(m/l));
ans+=(ll)(n/l)*(m/l)*(sum[r]-sum[l-]);
}printf("%lld\n",ans);
}
}
2019.06.09
Luogu P2257 YY的GCD 莫比乌斯反演的更多相关文章
- [Luogu P2257] YY的GCD (莫比乌斯函数)
题面 传送门:洛咕 Solution 推到自闭,我好菜啊 显然,这题让我们求: \(\large \sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)\in prime]\) 根 ...
- 洛谷P2257 YY的GCD 莫比乌斯反演
原题链接 差不多算自己推出来的第一道题QwQ 题目大意 \(T\)组询问,每次问你\(1\leqslant x\leqslant N\),\(1\leqslant y\leqslant M\)中有多少 ...
- BZOJ 2820 luogu 2257 yy的gcd (莫比乌斯反演)
题目大意:求$gcd(i,j)==k,i\in[1,n],j\in[1,m] ,k\in prime,n,m<=10^{7}$的有序数对个数,不超过10^{4}次询问 莫比乌斯反演入门题 为方便 ...
- 洛谷 - P2257 - YY的GCD - 莫比乌斯反演 - 整除分块
https://www.luogu.org/problemnew/show/P2257 求 \(n,m\) 中 \(gcd(i,j)==p\) 的数对的个数 求 $\sum\limits_p \sum ...
- P2257 YY的GCD (莫比乌斯反演)
题意:求\[\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j) = prim]\] 题解:那就开始化式子吧!! \[f(d) = \sum_{i=1}^{n}\sum_{j=1 ...
- [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)
[BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...
- BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1624 Solved: 853[Submit][Status][Discu ...
- Luogu P2257 YY的GCD
莫比乌斯反演第一题.莫比乌斯反演入门 数论题不多BB,直接推导吧. 首先,发现题目所求\(ans=\sum_{i=1}^n\sum_{j=1}^m [\gcd(i,j)=prime]\) 考虑反演,我 ...
- 【题解】Luogu P2257 YY的GCD
原题传送门 这题需要运用莫比乌斯反演(懵逼钨丝繁衍) 显然题目的答案就是\[ Ans=\sum_{i=1}^N\sum_{j=1}^M[gcd(i,j)=prime]\] 我们先设设F(n)表示满足\ ...
随机推荐
- BZOJ 3251 树上三角形:LCA【构成三角形的结论】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3251 题意: 给你一棵树,n个节点,每个点的权值为w[i]. 接下来有m个形如(p,a,b ...
- babel-runtime 和 babel-polyfill
Babel 默认只转换新的 JavaScript 语法 https://excaliburhan.com/post/babel-preset-and-plugins.html babel-plugin ...
- ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- python--面向对象(最全讲解)
http://www.cnblogs.com/Eva-J/articles/7293890.html 阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 ...
- DataGrid 显示选中的item
Datagrid或者listview 中想要把相应的项 滚动到当前可见的位置, 必须满足2个条件: 1) 必须去掉虚拟化 VirtualizingStackPanel.IsVirtualiz ...
- SpringBoot之导入导出Excel
1.添加springBoot支持 <dependency> <groupId>org.apache.poi</groupId> <artifactId> ...
- Excel中导入到oracle使用merge into 差异性更新数据库
merge into temp1 ausing (select ID from (Select ID from temp1 ...
- Hibernate注解详细介绍
引自http://blog.csdn.net/lin_yongrui/article/details/6855394 声明实体Bean @Entity public class Flig ...
- Invalidate()这个函数有什么用?
c++中的这个函数,一会是刷新窗口的作用,一会是使区域无效.我搞不懂这个函数究竟是有什么作用?谢谢赐教. void Invalidate( BOOL bErase = TRUE ); 该函数的作用是使 ...
- Deep Learning - Install the Development Environment
WLS(Windows Subsystem for Linux) Base WLS Installation Guide Initializing a newly installed distro W ...