[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 ...
随机推荐
- java流。基础
总结:read()方法返回类型是int型??不知道该怎么用好循环> package com.da; import java.io.*; public class fgbv { public st ...
- explain之三:MYSQL EXPLAIN语句的extended 选项学习体会,分析诊断工具之二
MySQL 的explain命令有一个extended选项,我想可以很多人都没有注意,因为它对命令的输出结果没有任何改变,只是增加了一个warning.这个 warning中显示了MySQL对SQL的 ...
- 四川第七届 I Travel(bfs)
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...
- java中实现多线程的方法有几种以及如何实现多线程
多线程是java语言区别于其它语言的一大特点.其它语言向C++的话如果要实现多线程的话,必须调用操作系统的多线程机制(我不确定自己记得对不对 了感兴趣的自己上网查查看). 在java5以前实现多线程有 ...
- 带坑的几道PHP面试题
第二题 代码如下: $i='11'; printf("%d\n",printf("%d",printf("%d",$i))); 输出结果是多 ...
- Cookies和Session的定义与区别
Cookies和Session二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当下次你再光临同一个网 ...
- JAVA基础知识总结7(抽象类 | 接口)
抽象类: abstract 1.抽象:不具体,看不明白.抽象类表象体现. 2.在不断抽取过程中,将共性内容中的方法声明抽取,但是方法不一样,没有抽取,这时抽取到的方法,并不具体,需要被指定关键字abs ...
- bzoj 2653 middle(主席树)
题面:https://vjudge.net/problem/HYSBZ-2653 博客:https://blog.csdn.net/litble/article/details/78984846 这个 ...
- JSON数据格式简介
---------------siwuxie095 JSON 简介 JSON:JavaScript 对象表示法(JavaScript Objec ...
- JS 中的数组遍历方式效率比较
JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代码如下: ; j < ...