YY的GCD 莫比乌斯反演
题解:
$ans = \sum_{x = 1}^{n}\sum_{y = 1}^{m}\sum_{i = 1}^{k}[gcd(x, y) == p_{i}]$其中k为质数个数
$$ans = \sum_{i = 1}^{k}\sum_{x = 1}^{n}\sum_{y = 1}^{m}[gcd(x, y) == p_{i}]$$
设$f(d)$表示$x$从$1$到$n$,$y$从$1$到$m$,$gcd == d$的个数,$g(d)$表示相同条件下$d | gcd$(即$gcd$为$d$的倍数)的个数
那么$$f(d) = \sum_{x = 1}^{n}\sum_{y = 1}^{m}[gcd(x, y) == d]$$,$$g(d) = \lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor$$
因为$$g(x) = \sum_{x|d}^{min(n, m)}f(d)$$
所以反演一下。
$$f(x) = \sum_{x | d}^{min(n, m)}\mu(\frac{d}{x})g(d)$$
那么$ans = \sum_{i = 1}^{k}f(p_{i})$
$$= \sum_{i = 1}^{k}\sum_{x | d}^{min(n, m)}\mu(\frac{d}{x})g(d)$$
改成直接枚举系数
$$= \sum_{i = 1}^{k}\sum_{d = 1}^{\lfloor{\frac{min(n, m)}{p_{i}}}\rfloor}\mu(d)g(dp_{i})$$
$$= \sum_{i = 1}^{k}\sum_{d = 1}^{\lfloor{\frac{min(n, m)}{p_{i}}}\rfloor}\mu(d)\lfloor{\frac{n}{dp_{i}}\rfloor \lfloor{\frac{m}{dp_{i}}\rfloor}}$$<---枚举每个$\mu(d)分别被每个质数统计了几次$
$$= \sum_{T = 1}^{min(n, m)} \lfloor{\frac{n}{T}}\rfloor \lfloor{\frac{m}{T}}\rfloor\sum_{k|T}{\mu(\frac{T}{k})}$$<---枚举每个$\lfloor{\frac{n}{T}}\rfloor \lfloor{\frac{m}{T}}\rfloor$会给哪些$\mu$做贡献(哪些$\mu$会在某次被统计$\lfloor{\frac{n}{T}}\rfloor \lfloor{\frac{m}{T}}\rfloor$次)
然后暴力枚举质数和系数,给对应的$\mu$做贡献(质数$p_{i}$给它的倍数做贡献),统计前缀和,对前面的$\lfloor{\frac{n}{T}}\rfloor \lfloor{\frac{m}{T}}\rfloor$进行整数分块处理
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 10000100
#define LL long long
int n, m, tot, t;
int prime[AC], mu[AC];
LL s[AC], ans;
bool z[AC]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void pre()
{
int now;
mu[] = ;
for(R i = ; i <= ; i++)
{
if(!z[i]) prime[++tot] = i, mu[i] = -;
for(R j = ; j <= tot; j++)
{
now = prime[j];
if(i * now > ) break;
z[i * now] = true;
if(!(i % now)) break;
mu[now * i] = -mu[i];
}
}
int p;
for(R i = ; i <= tot; i++)//枚举质数
{
p = prime[i];//卡常
for(R j = p; j <= ; j += p) //枚举倍数
s[j] += mu[j / p];//or j = 系数, s[j * prime[i]] += mu[j];
}
for(R i = ; i <= ; i++) s[i] += s[i - ];
} void work()
{
t = read();
while(t--)
{
int pos = ;
ans = ;
n = read(), m = read();
int b = min(n, m);//这里要取min!!!
for(R i = ; i <= b; i = pos + )
{
pos = min(n / (n / i), m / (m / i));
ans += (LL) (n / i) * (LL) (m / i) * (LL) (s[pos] - s[i - ]);//error 只有ans是LL是不够的
}
printf("%lld\n", ans);
}
} int main()
{
// freopen("in.in", "r", stdin);
//freopen("YYnoGCD.in", "r", stdin);
//freopen("YYnoGCD.out", "w", stdout);
pre();
work();
//fclose(stdin);
//fclose(stdout);
return ;
}
YY的GCD 莫比乌斯反演的更多相关文章
- [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 ...
- 洛谷P2257 YY的GCD 莫比乌斯反演
原题链接 差不多算自己推出来的第一道题QwQ 题目大意 \(T\)组询问,每次问你\(1\leqslant x\leqslant N\),\(1\leqslant y\leqslant M\)中有多少 ...
- 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 ...
- 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}次询问 莫比乌斯反演入门题 为方便 ...
- Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...
- 【BZOJ2820】YY的GCD(莫比乌斯反演 数论分块)
题目链接 大意 给定多组\(N\),\(M\),求\(1\le x\le N,1\le y\le M\)并且\(Gcd(x, y)\)为质数的\((x, y)\)有多少对. 思路 我们设\(f(i)\ ...
- bzoj 2820 YY的GCD 莫比乌斯反演
题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...
- 【BZOJ2820】YY的GCD [莫比乌斯反演]
YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 求1<=x<=N, ...
- bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
随机推荐
- mavn打外部配置jar包依赖
https://blog.csdn.net/pei19890521/article/details/80984707
- OpenCV 3.2 Tracking 物体跟踪
跟踪就是在连续视频帧中定位物体,通常的跟踪算法包括以下几类: 1. Dense Optical Flow 稠密光流 2. Sparse Optical Flow 稀疏光流 最典型的如KLT算法(Kan ...
- InnoDB锁冲突案例演示
Preface As we know,InnoDB is index organized table.InnoDB engine supports row-level lock bas ...
- appium -- 页面出现弹窗,关闭后,无法识别页面元素
1. 问题:如图所示:在修改手势密码的过程中,点击了返回按钮后,弹出该弹窗:点击继续设置后,就发现 driver.getPageSource()获取不到页面元素.在找了一圈无用的资料后,没有什么好的处 ...
- .NET MVC和.NET WEB api混用时注意事项
1.同时配置了mvc路由和api路由时,mvc路由无法访问(调用所有mvc路由全部404错误) 在Global.asax中,需注意路由注册的顺序,将api路由注册放在最后: 即将 void Appli ...
- ant-design学习准备_1
在学习ant-desin过程中,发现很多知识都不清楚,从现在开始,每天将自己学习到的知识进行一个总结记录,前端大佬勿扰勿喷.先介绍几个基础概念和一些常用命令: 1.什么是脚手架 我们经常在各个博客论坛 ...
- Linux命令应用大词典-第23章 进程和服务管理
23.1 ps:报告当前进程的快照 23.2 top:显示当前正在运行的进程 23.3 pgrep:按名称和其他属性查找进程 23.4 pidof:查找正在运行的进程的进程号 23.5 pstree: ...
- Deep Residual Learning for Image Recognition论文笔记
Abstract We present a residual learning framework to ease the training of networks that are substant ...
- Fluent Python: Mutable Types as Parameter Defaults: Bad Idea
在Fluent Python一书第八章有一个示例,未看书以先很难理解这个示例运行的结果,我们先看结果,然后再分析问题原因: 定义了如下Bus类: class Bus: def __init__(sel ...
- HDU 1512 Monkey King(左偏树)
Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...