DZY Loves Math(莫比乌斯反演)
\(x=p_1^{\alpha_1}p_2^{\alpha_2}...p_c^{\alpha_c}\)
\(f(x)=\max(\alpha_1,\alpha_2,...,\alpha_c)\)
\(assume\ n\leq m\)
\(\sum_{i=1}^{n}\sum_{j=1}^{m}f(\gcd(i,j))\)
\(\sum_{x=1}^{n}f(x)\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)=x]\)
\(\sum_{x=1}^{n}f(x)\sum_{i=1}^{\frac nx}\sum_{j=1}^{\frac mx}[\gcd(i,j)=1]\)
\(\sum_{x=1}^{n}f(x)\sum_{d=1}^{\frac nx}\mu(d)\sum_{i=1}^{\frac nx}\sum_{j=1}^{\frac mx}[d|i,d|j]\)
\(\sum_{x=1}^{n}f(x)\sum_{d=1}^{\frac nx}\mu(d)\lfloor \frac {n}{dx}\rfloor\lfloor \frac {m}{dx}\rfloor\)
\(\sum_{x=1}^{n}f(x)\sum_{x|d}\mu(\frac dx)\lfloor \frac {n}{d}\rfloor\lfloor \frac {m}{d}\rfloor\)
\(\sum_{d=1}^{n}\lfloor \frac {n}{d}\rfloor\lfloor \frac {m}{d}\rfloor\sum_{x|d}f(x)\mu(\frac dx)\)
根据套路,我们到了这个式子。直接暴力调和级数算 \(\sum_{x|d}f(x)\mu(\frac dx)\) 的前缀和,时间复杂度 \(O(n\log n)\)
怎么 \(O(n)\) 筛的锅待填
\(O(n\log n):\)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=10000000+10;
int n,m,f[maxn],mu[maxn],prim[maxn],vis[maxn],cnt;
ll g[maxn];
inline int read(){
register int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return (f==1)?x:-x;
}
void pre(int n){
mu[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){prim[++cnt]=i;mu[i]=-1;}
for(int j=1;i*prim[j]<=n&&j<=cnt;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0) break;
mu[i*prim[j]]=-mu[i];
}
}
int num,ans;
for(int i=1;i<=cnt;i++){
for(int j=prim[i];j<=n;j+=prim[i]){
num=j;ans=0;
while(num%prim[i]==0) num/=prim[i],ans++;
f[j]=max(f[j],ans);
}
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j+=i) g[j]+=f[i]*mu[j/i];
for(int i=1;i<=n;i++) g[i]+=g[i-1];
}
int main()
{
pre(10000000);
int T=read();
while(T--){
n=read(),m=read();
if(n>m) swap(n,m);
ll ans=0;
for(int l=1,r;l<=n;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=(ll)(n/l)*(m/l)*(g[r]-g[l-1]);
}
printf("%lld\n",ans);
}
return 0;
}
\(O(n):\)
#include <bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;
const int maxn=10000000+10;
int n,m,f[maxn],low[maxn],prim[maxn],vis[maxn],cnt;
ll g[maxn];
inline int read(){
register int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return (f==1)?x:-x;
}
void pre(int n){
for(int i=2;i<=n;i++){
if(!vis[i]){low[i]=prim[++cnt]=i;f[i]=g[i]=1;}
for(int j=1;i*prim[j]<=n&&j<=cnt;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0){
f[i*prim[j]]=f[i]+1;low[i*prim[j]]=low[i]*prim[j];
if(i==low[i]) g[i*prim[j]]=1;
else g[i*prim[j]]=(f[i/low[i]]==f[i*prim[j]])?-g[i/low[i]]:0;
break;
}
f[i*prim[j]]=1;low[i*prim[j]]=prim[j];
g[i*prim[j]]=(f[i]==1)?-g[i]:0;
}
}
for(int i=1;i<=n;i++) g[i]+=g[i-1];
}
signed main()
{
pre(10000000);
int T=read();
while(T--){
n=read(),m=read();
if(n>m) swap(n,m);
ll ans=0;
for(int l=1,r;l<=n;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=(ll)(n/l)*(m/l)*(g[r]-g[l-1]);
}
printf("%lld\n",ans);
}
return 0;
}
DZY Loves Math(莫比乌斯反演)的更多相关文章
- 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...
- bzoj 3309 DZY Loves Math 莫比乌斯反演
DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1303 Solved: 819[Submit][Status][Dis ...
- 【BZOJ3309】DZY Loves Math 莫比乌斯反演+线性筛(好题)
[BZOJ3309]DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10 ...
- 【BZOJ3309】DZY Loves Math - 莫比乌斯反演
题意: 对于正整数n,定义$f(n)$为$n$所含质因子的最大幂指数.例如$f(1960)=f(2^3 * 5^1 * 7^2)=3$,$f(10007)=1$,$f(1)=0$. 给定正整数$a,b ...
- 【bzoj3309】DZY Loves Math 莫比乌斯反演+线性筛
Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...
- BZOJ 3309 DZY Loves Math ——莫比乌斯反演
枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...
- BZOJ 3309: DZY Loves Math [莫比乌斯反演 线性筛]
题意:\(f(n)\)为n的质因子分解中的最大幂指数,求\(\sum_{i=1}^n \sum_{j=1}^m f(gcd(i,j))\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d| ...
- [BZOJ3309]DZY Loves Math(莫比乌斯反演+线性筛)
$\sum\limits_{T=1}^{n}\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum\limits_{d|T}f(d)\mu(\fr ...
- bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 凭着上课所讲和与 Narh 讨论推出式子来: 竟然是第一次写数论分块!所以迷惑了半天: ...
- BZOJ 3309: DZY Loves Math 莫比乌斯反演+打表
有一个神奇的技巧——打表 code: #include <bits/stdc++.h> #define N 10000007 #define ll long long #define se ...
随机推荐
- 实现WireCard支付
实现WireCard支付,暂未完成 WireCardController.cs using System; using System.Collections.Generic; using System ...
- python学习 day3 (3月4日)---字符串
字符串: 下标(索引) 切片[起始:终止] 步长[起始:终止:1] 或者-1 从后往前 -1 -2 -3 15个专属方法: 1-6 : 格式:大小写 , 居中(6) s.capitalize() s ...
- linux_关闭防火墙
centos6版本 永久关闭 chkconfig iptables off 查看状态 chkconfig iptables --list 此时关闭开机重新启动 service iptables sto ...
- rails gem更换ruby-china源
查看gem源 gem sources -l 换添加源 gem sources --add https://gems.ruby-china.com/ 删除原来的rubygems源 gem sources ...
- 2019.01.14 bzoj4530: [Bjoi2014]大融合(线段树合并)
传送门 线段树合并菜题. 题意简述:nnn个点,支持连边以及查询一个点所在连通块中经过这个点的路径条数,保证这张图时刻为森林. 思路: 先建出所有操作完之后的树统计出dfsdfsdfs序 注意有可能是 ...
- react优化--pureComponent
shouldComponentUpdate的默认渲染 在React Component的生命周期中,shouldComponentUpdate方法,默认返回true,也就意味着就算没有改变props或 ...
- Perl语言入门
Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言". Perl语法基础: (1)Perl程序由声 ...
- html的那些小小细节
1.get post方式提交的不同 get:数据放在url的后面,用?连接 会在客户端保留缓存信息,不安全 ...
- C#控件之:进度条(ProgressBar)
一.重绘进度条 public class CustomProgressBar:ProgressBar { public CustomProgressBar() { this.SetStyle(Cont ...
- Android自定义视图三:给自定义视图添加“流畅”的动画
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...