【YY的GCD】
设
\]
我们的答案显然是
\]
设
\]
即有多少个数对的最大公约数是\(n\)的倍数
显然\(F(n)=\left \lfloor \frac{N}{n} \right \rfloor\times\left \lfloor \frac{M}{n} \right \rfloor\)
同时还存在
\]
看起来并不能反演,但是我们大胆猜测会存在这样的性质
\]
看起来很靠谱啊,那就证明一下吧
\]
考虑把\(f(i)\)提前,因为\(n|d,d|i\),所以\(n|i\)
\]
设\(d=kn,i=cd\),则有\(i=ckn\)
则\(\frac{d}{n}=k,\frac{i}{n}=ck\),所以其实是在\(\frac{i}{n}\)的约数
所以可以写成
\]
所以只有在\(i=n\)的时候\(\sum_{d|\frac{i}{n}}\mu(d)=1\),所以这个柿子的值是成立的
所以有一种新的反演形式
\]
就有
\]
之后我们的柿子变成了
\]
于是现在得到了一个复杂度非常玄学的做法,就是枚举\(p\)之后枚举\(p\)的倍数
暴力就写好了
#include<iostream>
#include<cstring>
#include<cstdio>
#define re register
#define maxn 10000005
#define LL long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
inline int read()
{
char c=getchar();
int x=0;
while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-48,c=getchar();
return x;
}
int mu[maxn],p[maxn],f[maxn];
int n,m,T;
int main()
{
scanf("%d",&T);
f[1]=mu[1]=1;
for(re int i=2;i<=10000000;i++)
{
if(!f[i]) p[++p[0]]=i,mu[i]=-1;
for(re int j=1;j<=p[0]&&p[j]*i<=10000000;j++)
{
f[p[j]*i]=1;
if(i%p[j]==0) break;
mu[p[j]*i]=-1*mu[i];
}
}
while(T--)
{
scanf("%d%d",&n,&m);
LL ans=0;
for(re int i=1;i<=p[0]&&p[i]<=min(n,m);i++)
{
for(re int j=1;j*p[i]<=min(n,m);j++)
ans+=mu[j]*(n/(j*p[i]))*(m/(j*p[i]));
}
printf("%lld\n",ans);
}
return 0;
}
把柿子化到这里显然还不够啊
我们需要继续搞一搞
设\(k\times p=n\)
那么
\]
设\(T=kp\)
于是就有
\]
\]
发现好像前面那两个东西可以两个整除分块一起上,后面这个\(\sum_{t|T,t\in prime}\mu(\frac{T}{t})\)看起来好像需要一个前缀和
于是就可以啦
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#define re register
#define maxn 10000005
#define LL long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
inline int read()
{
char c=getchar();
int x=0;
while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-48,c=getchar();
return x;
}
int mu[maxn],f[maxn],p[maxn>>2];
LL pre[maxn];
int T,n,m;
int main()
{
f[1]=mu[1]=1;
for(re int i=2;i<=10000000;i++)
{
if(!f[i]) p[++p[0]]=i,mu[i]=-1;
for(re int j=1;j<=p[0]&&p[j]*i<=10000000;j++)
{
f[p[j]*i]=1;
if(i%p[j]==0) break;
mu[p[j]*i]=-1*mu[i];
}
}
for(re int i=1;i<=p[0];i++)
for(re int j=1;j*p[i]<=10000000;j++) pre[j*p[i]]+=mu[j];
for(re int i=1;i<=10000000;i++) pre[i]+=pre[i-1];
scanf("%d",&T);
while(T--)
{
LL ans=0;
n=read(),m=read();
if(n>m) std::swap(n,m);
for(re int l=1,r;l<=n;l=r+1)
{
r=min(n/(n/l),m/(m/l));
ans+=(LL)(n/l)*(m/l)*(pre[r]-pre[l-1]);
}
printf("%lld\n",ans);
}
return 0;
}
【YY的GCD】的更多相关文章
- BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1624 Solved: 853[Submit][Status][Discu ...
- [BZOJ2820]YY的GCD
[BZOJ2820]YY的GCD 试题描述 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...
- bzoj 2820 YY的GCD 莫比乌斯反演
题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...
- 【BZOJ】【2820】YY的GCD
莫比乌斯反演 PoPoQQQ讲义第二题. 暴力枚举每个质数,然后去更新它的倍数即可,那个g[x]看不懂就算了…… 为什么去掉了一个memset就不T了→_→…… /****************** ...
- 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&&bzoj 2820 YY的GCD&&BZOJ 3529 数表)
首先我们来看一道题 BZOJ 2301 Problem b Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd( ...
- 【BZOJ 2820】 YY的GCD (莫比乌斯+分块)
YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...
- 【BZOJ2820】YY的GCD(莫比乌斯反演)
[BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...
- YY的GCD
YY的GCD 给出T个询问,询问\(\sum_{i=1}^N\sum_{j=1}^M(gcd(i,j)\in prime)\),T = 10000,N, M <= 10000000. 解 显然质 ...
- 洛谷【P2257】YY的GCD
YY的GCD 原题链接 这应该是我做的第一道莫比乌斯反演的题目. 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x ...
- 【BZOJ2820】YY的GCD
[BZOJ2820]YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的( ...
随机推荐
- RabbitMQ入门教程系列
https://blog.csdn.net/column/details/18247.html
- SpringBoot 之Quartz的使用
对于Quartz的使用,还是想说一句,SpringBoot真的很好用啊! 第一步:当然是引入依赖啦 <parent> <groupId>org.springframework. ...
- mongdb启动报错
2018-08-19T12:25:31.707+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- JDBC入门(4)--- 批处理
1.Statement批处理 当你有10条SQL语句要执行时,一次向服务器发送一条SQL语句,这样做的效率上极差,处理的方案是使用批处理,即一次向服务发送多条SQL语句,然后由服务器一次性处理. 批处 ...
- MySql:局域网和权限用户管理
MySql 5.6(XP)/5.7(win7) 添加用户和设置局域访问权限操作.请在 http://sourceforge.net/ 下载MySql Control Center(不是安装版本). ...
- POJ P3667 Hotel——solution
Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...
- 关于 Table 表格那些三两事儿
引言 实现下列表格样式,嵌套与form表单中提交信息,为了让自己的表格可以“ 随心所欲 ” 变换自己的形式,需要两个重要的td 属性colspan 列合并 以及 rowspan 行合并 来实现,表格宽 ...
- Spring Data MongoDB 模糊查询
Pattern pattern = Pattern.compile("^.*" + value + ".*$"); Query query = new Quer ...
- CSS3 教程
CSS3 教程 CSS 用于控制网页的样式和布局. CSS3 是最新的 CSS 标准. 本教程向您讲解 CSS3 中的新特性. 开始学习 CSS3! 更多:http://www.runoob.com ...