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 ...
随机推荐
- 8月的list
多校的list: 第一周的多校list: k路归并 (思想大概理解了,还没实现 莫比乌斯 树归 第三场的多校list: 斯坦纳树 第四场多校: Pollard_rho算法和Miller_Rabin ...
- git .gitignore未生效
添加进.gitignore的问题未生效. .gitignore只会忽略在.gitignore编写之后的未跟踪(untrack)文件,而在编写.gitignore之前已经add and commit的文 ...
- AOP的异常通知
一.配置异常通知的步骤 (Aspectj方式) 1.只有当切点报异常才能触发异常通知 2.在spring中有Aspectj 方式提供了异常通知方法 2.1 如果希望通过 schema-base 实 ...
- 对SVC和SVR的理解
首先: support vector classify(SVC)支持分类机做二分类的,找出分类面,解决分类问题 support vector regression(SCR)支持回归机做曲线拟合.函数回 ...
- jQuery动态控制下拉列表的被选项[转]
<form id="form" action="/query!query.action"> <select> <option va ...
- FS210(cortex-A8)移植MT7601无线WIFI模块
准备:ubuntu 12.04 板子内核:3.0.2 交叉编译器:arm-cortex_a8-linux-gnueabi-gcc 所需资源下载:https://pan.baidu.com/s/1yWA ...
- mysql 在linux下的完整安装过程
1.下载RPM包 https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar 2.先使用命令删 ...
- 在vue或者react中使用express框架
在react 或者 vue项目中使用express框架 1.创建vue或者 react 项目 2.在项目中创建server文件夹,创建server.js //require()方法引入express模 ...
- 同时安装python2.7和python3.5
同时安装python2.7和python3.5,并配置sublime ctrl+B选择运行python版本 安装python 首先是安装两个版本的python,并配置相应的环境变量 1.在下载安装好P ...
- Mathtype批量修改公式
(1)将模板中的公式直接打开 (2)将需要修改好的公式复制黏贴到模板中 (3)再复制黏贴出,即可 PS: (1)统一设置公式格式 (2)统一设置公式大小