[51nod1190]最小公倍数之和V2(莫比乌斯反演)
题解
题解
我是真的不明白这玩意儿是怎么跟反演扯上关系的……
首先
ans
&=b\sum_{d|b}{1\over d}\sum_{i=a}^{b}i[\gcd(i,b)=d]\\
&=b\sum_{d|b}\sum_{i=\lceil{a\over d}\rceil}^{b\over d}i[gcd(i,{b\over d})=1]\\
\end{align}
\]
然后有一个非常神仙的操作……就是强行反演一波,把\([n=1]\)化成\(\sum_{i|n}\mu(i)\)
ans
&=b\sum_{d|b}\sum_{i=\lceil{a\over d}\rceil}^{b\over d}i\sum_{j|\gcd(i,{b\over d})}\mu(j)\\
&=b\sum_{d|b}\sum_{j|{b\over d}}\mu(j)\sum_{i=\lceil{a\over d}\rceil}^{b\over d}[i \mod j=0]\\
&=b\sum_{d|b}\sum_{j|{b\over d}}\mu(j)\sum_{i=\lceil{a\over d}\rceil}^{b\over d}[i \mod j=0]\\
&={b\over 2}\sum_{d|b}\sum_{j|{b\over d}}\mu(j)j(\lfloor{b\over {dj}}\rfloor+\lceil{a\over {dj}}\rceil)(\lfloor{b\over {dj}}\rfloor-\lceil{a\over {dj}}\rceil+1)\\
&={b\over 2}\sum_{T|b}(\lfloor{b\over T}\rfloor+\lceil{a\over T}\rceil)(\lfloor{b\over T}\rfloor-\lceil{a\over T}\rceil+1)\sum_{d|T}\mu(d)d\\
\end{align}
\]
那么我们只要把\(b\)分解一下质因数,然后\(dfs\)找出\(b\)的所有因子就可以了
然而这里还有一个问题,就是\(f(T)=\sum_{d|T}\mu(d)d\)该怎么快速计算
首先我们可以发现\(f\)也是个积性函数,有\(f(p)=1-p,f(p^c)=1-p\)
因为只有次数小于等于\(1\)的质因子会有贡献,所以我们在爆搜枚举因数的时候,如果\(p\nmid i\),那么\(f(i\times p)=f(i)\times(1-p)\)
然后就没有然后了
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+5,P=1e9+7,inv2=500000004;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
R int res=1;
for(;y;y>>=1,x=mul(x,x))if(y&1)res=mul(res,x);
return res;
}
bitset<N>vis;int p[N],v[N],c[N],top,m,sqr,n,tot,res,g;
void init(int n){
fp(i,2,n){
if(!vis[i])p[++tot]=i;
for(R int j=1;j<=tot&&1ll*i*p[j]<=n;++j){
vis[i*p[j]]=1;
if(i%p[j]==0)break;
}
}
}
inline int calc(R int x,R int y){
R int a=n/x,b=(m+x-1)/x;
return 1ll*(a+b)*(a-b+1)%P*y%P;
}
void dfs(int pos,int val,int mu){
if(pos==top+1)return res=add(res,calc(val,mu)),void();
dfs(pos+1,val,mu),mu=mul(mu,dec(1,v[pos]));
fp(i,1,c[pos])val*=v[pos],dfs(pos+1,val,mu);
}
void solve(){
top=0,res=0,g=n;
for(R int i=1;i<=tot&&1ll*p[i]*p[i]<=g;++i)if(g%p[i]==0){
v[++top]=p[i],c[top]=0;
while(g%p[i]==0)g/=p[i],++c[top];
}
if(g!=1)v[++top]=g,c[top]=1;
dfs(1,1,1);
res=1ll*res*n%P*inv2%P;
print(res);
}
int main(){
// freopen("testdata.in","r",stdin);
// freopen("testdata.out","w",stdout);
int T=read();init(sqr=N-5);
while(T--)m=read(),n=read(),solve();
return Ot(),0;
}
[51nod1190]最小公倍数之和V2(莫比乌斯反演)的更多相关文章
- 51nod1238 最小公倍数之和 V3(莫比乌斯反演)
题意 题目链接 Sol 不想打公式了,最后就是求一个 \(\sum_{i=1}^n ig(\frac{N}{i})\) \(g(i) = \sum_{i=1}^n \phi(i) i^2\) 拉个\( ...
- 【CJOJ2512】gcd之和(莫比乌斯反演)
[CJOJ2512]gcd之和(莫比乌斯反演) 题面 给定\(n,m(n,m<=10^7)\) 求 \[\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)\] 题解 首先把公因数直 ...
- 51nod 1190 最小公倍数之和 V2
给出2个数a, b,求LCM(a,b) + LCM(a+1,b) + .. + LCM(b,b). 例如:a = 1, b = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30 ...
- 51nod 1190 最小公倍数之和 V2【莫比乌斯反演】
参考:http://blog.csdn.net/u014610830/article/details/49493279 这道题做起来感觉非常奇怪啊--头一次见把mu推出来再推没了的-- \[ \sum ...
- 51nod1238 最小公倍数之和 V3 莫比乌斯函数 杜教筛
题意:求\(\sum_{i = 1}^{n}\sum_{j = 1}^{n}lcm(i, j)\). 题解:虽然网上很多题解说用mu卡不过去,,,不过试了一下貌似时间还挺充足的,..也许有时间用phi ...
- [51nod1222] 最小公倍数计数(莫比乌斯反演)
题面 传送门 题解 我此生可能注定要和反演过不去了--死都看不出来为啥它会突然繁衍反演起来啊-- 设\(f(n)=\sum_{i=1}^n\sum_{j=1}^n[{ij\over\gcd(i,j)} ...
- 51nod 1222 最小公倍数计数【莫比乌斯反演】
参考:https://www.cnblogs.com/SilverNebula/p/7045199.html 所是反演其实反演作用不大,又是一道做起来感觉诡异的题 转成前缀和相减的形式 \[ \sum ...
- 51Nod.1237.最大公约数之和 V3(莫比乌斯反演 杜教筛 欧拉函数)
题目链接 \(Description\) \(n\leq 10^{10}\),求 \[\sum_{i=1}^n\sum_{j=1}^ngcd(i,j)\ mod\ (1e9+7)\] \(Soluti ...
- 【51Nod 1190】最小公倍数之和 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1190 \[ \begin{aligned} &\sum_{i=a ...
随机推荐
- MySQL 5.6 date 与 string 的转换和比较
我们有张表,表中有一个字段 dpt_date ,SQL 类型为 date,表示离开日期. 我们将 dpt_date 与字符串 ‘2016-03-09’ 进行比较,发现效率低于 dpt_date 转换为 ...
- SPRING中的线程池ThreadPoolTaskExecutor
一.初始化 1,直接调用 ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor(); //线程池所使用的缓冲队列 p ...
- maven中maven dependencies中依赖出现了项目
maven 中maven dependencies中依赖出现了项目,把依赖的项目关掉,项目消失,但是还是无法打包 ,出现的错误如图.说明:依赖的项目为project-dao 打包的项目为projec ...
- 2016.2.28 DataTable用法汇总
将控件的DataSource转换为DataTable,但是,此控件的DataSource绑定时必须是DataTable,不能是List DataTable dt = (bgvRoutePortion. ...
- CreateMutex实现只能打开一个客户端
#include "stdafx.h" #include <Windows.h> #include <iostream> using namespace s ...
- python 函数相关定义
1.为什么要使用函数? 减少代码的冗余 2.函数先定义后使用(相当于变量一样先定义后使用) 3.函数的分类: 内置函数:python解释器自带的,直接拿来用就行了 自定义函数:根据自己的需求自己定义的 ...
- junit4新框架hamcrest的assertThat
assertThat JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活 /**equalTo匹配符断言被测的tes ...
- java线程游戏之背景图片的移动
package com.plane; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; im ...
- [Python Study Notes]计算器
# ------------------------------------------------------------------------------------- # @文件: 计算器.p ...
- Linux 下安装redis
记录一下linux下的安装步骤,还是比较复杂的 1. 下载redis-2.8.19.tar.gz: ftp传到linux01上: 解压: tar –zxvf redis-2.8.19.tar.gz 2 ...