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 ...
随机推荐
- wireshark源码分析二
一.源代码结构 在wireshark源代码根目录下,可以看到以下子目录: 1)物理结构 其中,epan文件夹负责所有网络协议识别工作,plugins里面存放了wireshark所有插件,gtk ...
- Windows 8风格应用-触控输入
参考:演练:创建您的第一个触控应用程序 http://msdn.microsoft.com/zh-cn/library/ee649090(v=vs.110).aspx win8支持多点触摸技术,而我们 ...
- eclipse安装提要
svn 插件安装http://subclipse.tigris.org/update_1.12.x教程地址http://jingyan.baidu.com/article/f71d60376b4c57 ...
- Le Chapitre VIII
J'appris bien vite à mieux connaître cette fleur. Il y avait toujours eu, sur la planète du petit pr ...
- 证明2x2正交矩阵专置后还是正交矩阵
[ x1 x2 y1 y2] x1^2+y1^2=1 x2^2 + y2^2=1 x1*x2 + y1*y2=0 如果专置后还是 x1^2 + x2^2=1 y1^2 +y2^2=1 x1* ...
- java Concurrent包学习笔记(七):ConcurrentHashMap
(注意:以下讲解的ConcurrentHashMap是jdk 1.8的) 一.ConcurrentHashMap的数据结构 ConcurrentHashMap在1.8中的实现,相比于1.7的版本基本上 ...
- 如何使用阿里巴巴iconfont矢量图片
①打开此网站http://www.iconfont.cn/ , 选择需要的几个图形 → 加入购物车 → 添加至项目 → 给项目随便命名 → 点击电线连接并点击代码 → 复制代码到css ②在body里 ...
- Redis模块开发示例
实现一个Redis module,支持两个扩展命令: 1) 可同时对hash的多个field进行incr操作: 2) incrby同时设置一个key的过期时间 在没有module之前,需要借助eval ...
- UML(一)下载与安装
三步 Step1 下载绿色版: http://www.pc6.com/softview/SoftView_64080.html Step2 下载汉化包,(提供一个地址): http://downloa ...
- POJ1468 Sorting Slides
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4442 Accepted: 1757 De ...