[bzoj2154]Crash的数字表格(mobius反演)
题意:$\sum\limits_{i = 1}^n {\sum\limits_{j = 1}^m {lcm(i,j)} } $
解题关键:
$\sum\limits_{i = 1}^n {\sum\limits_{j = 1}^m {lcm(i,j)} } = \sum\limits_{i = 1}^n {\sum\limits_{j = 1}^m {\frac{{i*j}}{{\gcd (i,j)}}} } $
枚举gcd,上式化为:
$\sum\limits_{d = 1}^{\min (n,m)} {d\sum\limits_{\begin{array}{*{20}{c}}
{\gcd (i,j) = = 1}\\
{1 < = i < = n/d}\\
{1 < = j < = m/d}
\end{array}} {i*j} } $
令
$f(n,m,k) = \sum\limits_{\begin{array}{*{20}{c}}
{\gcd (i,j) = = k}\\
{1 < = i < = n}\\
{1 < = j < = m}
\end{array}} {i*j} $
由于
$sum(n,m) = \sum\limits_{\begin{array}{*{20}{c}}
{1 < = i < = n}\\
{1 < = j < = m}
\end{array}} {i*j} = \frac{{n(n + 1)}}{2}\frac{{m(m + 1)}}{2}$
$\begin{array}{l}
F(n,m,k) = \sum\limits_{k|d} {f(n,m,d) = \sum\limits_{\begin{array}{*{20}{c}}
{1 < = i < = n}\\
{1 < = j < = m}\\
{k|\gcd (i,j)}
\end{array}} {i*j} = {k^2}sum(\left\lfloor {\frac{n}{k}} \right\rfloor ,\left\lfloor {\frac{m}{k}} \right\rfloor )} \\
f(n,m,k) = \sum\limits_{k|d} {u(\frac{d}{k})F(n,m,d)}
\end{array}$
而此题中,$k==1$,
则,
$\begin{array}{l}
f(n,m,1) = \sum\limits_{d = 1}^{\min (n,m)} {u(d)F(n,m,d)} \\
= \sum\limits_{d = 1}^{\min (n,m)} {u(d){d^2}sum(\left\lfloor {\frac{n}{d}} \right\rfloor ,\left\lfloor {\frac{m}{d}} \right\rfloor )} \\
ans = \sum\limits_{d = 1}^{\min (n,m)} {d*f(\left\lfloor {\frac{n}{d}} \right\rfloor ,\left\lfloor {\frac{m}{d}} \right\rfloor ,1)}
\end{array}$
求解ans和$f$函数复杂度都是$O(\sqrt n )$,所以最终复杂度为$O(n)$。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<iostream>
#define Sum(x,y) (x*(x+1)/2%mod*(y*(y+1)/2%mod)%mod)
using namespace std;
typedef long long ll;
const ll mod=;
const ll maxn=+;
ll n,m;
bool vis[maxn];
ll prime[maxn],mu[maxn],sum1[maxn];
void init_mu(ll n){
ll cnt=;
mu[]=;
for(ll i=;i<n;i++){
if(!vis[i]){
prime[cnt++]=i;
mu[i]=-;
}
for(ll j=;j<cnt&&i*prime[j]<n;j++){
vis[i*prime[j]]=;
if(i%prime[j]==){mu[i*prime[j]]=;break;}
else { mu[i*prime[j]]=-mu[i];}
}
}
sum1[]=;
for(ll i=;i<n;i++) sum1[i]=(sum1[i-]+1ll*mu[i]*i*i)%mod;
}
ll calf(ll n,ll m){
ll ans=,pos,len=min(n,m);
for(ll i=;i<=len;i=pos+){
pos=min(n/(n/i),m/(m/i));
//ans+=(sum1[pos]-sum1[i-1])%mod*((n/i)*((n/i)+1)/2%mod*(((m/i)+1)*(m/i)/2%mod)%mod);
ans+=(sum1[pos]-sum1[i-])%mod*Sum(n/i,m/i)%mod;//最好用函数写出
ans%=mod; }
return ans;
}
ll calres(ll n,ll m){
ll ans=,pos,len=min(n,m);
for(ll i=;i<=len;i=pos+){
pos=min(n/(n/i),m/(m/i));
ans+=(i+pos)*(pos-i+)/%mod*calf(n/i,m/i)%mod;
ans%=mod;
}
return (ans%mod+mod)%mod;
} int main(){
scanf("%lld%lld",&n,&m);
init_mu(min(n,m)+);
printf("%lld\n",calres(n,m));
return ;
}
[bzoj2154]Crash的数字表格(mobius反演)的更多相关文章
- bzoj2154: Crash的数字表格 莫比乌斯反演
题意:求\(\sum_{i=1}^n \sum_{j=1}^m\frac{i*j}{gcd(i,j)}\) 题解:\(ans=\sum_{i=1}^n\sum_{j=1}^m \frac{i*j}{g ...
- BZOJ2154 Crash的数字表格 【莫比乌斯反演】
BZOJ2154 Crash的数字表格 Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b) ...
- [BZOJ 2154]Crash的数字表格(莫比乌斯反演+数论分块)
[BZOJ 2154]Crash的数字表格(莫比乌斯反演+数论分块) 题面 求 \[\sum_{i=1}^{n} \sum_{j=1}^{m} \mathrm{lcm}(i,j)\] 分析 \[\su ...
- BZOJ 2154: Crash的数字表格 [莫比乌斯反演]
2154: Crash的数字表格 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 2924 Solved: 1091[Submit][Status][ ...
- [bzoj 2693] jzptab & [bzoj 2154] Crash的数字表格 (莫比乌斯反演)
题目描述 TTT组数据,给出NNN,MMM,求∑x=1N∑y=1Mlim(x,y)\sum_{x=1}^N\sum_{y=1}^M lim(x,y)\newlinex=1∑Ny=1∑Mlim(x, ...
- 【bzoj2154】Crash的数字表格 莫比乌斯反演
题目描述 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b)表示能同时被a和b整除的最小正整数.例如,LCM(6, ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
- 【莫比乌斯反演】BZOJ2154 Crash的数字表格
Description 求sigma lcm(x,y),x<=n,y<=m.n,m<=1e7. Solution lcm没有什么直接做的好方法,用lcm=x*y/gcd转成gcd来做 ...
- 莫比乌斯反演套路三、四--BZOJ2154: Crash的数字表格 && BZOJ2693: jzptab
t<=1e4个询问每次问n,m<=1e7,$\sum_{1\leqslant x \leqslant n,1 \leqslant y\leqslant m}lcm(x,y)$. 首先题目要 ...
随机推荐
- 复习Java虚拟机:JVM中的Stack和Heap
在JVM中,内存分为两个部分,Stack(栈)和Heap(堆).这里,我们从JVM的内存管理原理的角度来认识Stack和Heap,并通过这些原理认清Java中静态方法和静态属性的问题. 一般,JVM的 ...
- @Bean 和@ Component的区别
@Component auto detects and configures the beans using classpath scanning whereas @Bean explicitly d ...
- EasyNVR NVR网页无插件直播在兼容宇视NVR RTSP流媒体时PLAY过程对Scale的兼容
前一段在维护EasyNVR客户的过程中遇到一个问题,在接入宇视NVR的时候,就是明明在vlc中能非常正常播放的视频流,却用EasyRTSPClient RTSP客户端拉流的协议交互过程中,PLAY命令 ...
- django使用自己的setting的方法
创建一个自己的setting xxx.setting export DJANGO_SETTINGS_MODULE="xxx.setting" 然后在项目中import原生的sett ...
- iOS 跳转到Appstore的链接及二维码
1.应用内部跳转到Appstore 1.跳转到应用详情 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"it ...
- json (js对象标记)
基础 JSON: JavaScript Object Notation (JavaScript对象表示法) 网络媒体类型是 application/json,文件名扩展是 .json JSON 独立于 ...
- CentOS、乌班图设置固定静态IP
CentOS.乌班图设置固定静态IP 一.centOS 1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2,在文件 ...
- install_driver(mysql) failed
安装好了mysql监控神器innotop,正得意,innotoop不可用,其错误提示为install_driver(mysql) failed: Can't load '/usr/lib64/ ...
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- 顽石系列:Java技术面试
顽石系列:Java技术面试 JDBC相关 1.Statement与PreparedStatement的区 别,什什么是SQL注⼊入,如何防⽌止SQL注⼊? PreparedStatement支持动态设 ...