51NOD 1227:平均最小公倍数——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1227
懒得打公式了,看这位的吧:https://blog.csdn.net/fromatp/article/details/74999989
又一次将我的智商下限刷低的一道题,论我根本没注意到[gcd(i,j)==1]*j=phi(i)*i/2这个悲催的事实。
果然我数学活该学不好。
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int p=1e9+;
const int N=5e6;
const int M=2e6+;
const int MOD=;
const int INV2=;
const int INV6=;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int to,nxt,w;
}e[M];
bool he[N+];
int su[N+],tot,cnt,head[MOD+],phi[N+],sum[N+];
inline void add(int v,int w){
int u=v%MOD;
e[++cnt].to=v;e[cnt].w=w;e[cnt].nxt=head[u];head[u]=cnt;
}
inline int query(int v){
int u=v%MOD;
for(int i=head[u];i;i=e[i].nxt)
if(v==e[i].to)return e[i].w;
return -;
}
inline int sub(int a,int b){
a-=b;if(a<)a+=p;if(a>=p)a-=p;return a;
}
inline int inc(int a,int b){
a+=b;if(a<)a+=p;if(a>=p)a-=p;return a;
}
inline int s1(int l,int r){
return (ll)inc(l,r)*sub(r+,l)%p*INV2%p;
}
inline int s2(int n){
return (ll)n*(n+)%p*(*n+)%p*INV6%p;
}
void Euler(int n){
phi[]=;
for(int i=;i<=n;i++){
if(!he[i]){
su[++tot]=i;phi[i]=i-;
}
for(int j=;j<=tot&&i*su[j]<=n;j++){
int pri=su[j];he[i*pri]=;
if(i%pri==){
phi[i*pri]=phi[i]*pri;break;
}else phi[i*pri]=phi[i]*phi[pri];
}
}
for(int i=;i<=n;i++)sum[i]=inc(sum[i-],(ll)phi[i]*i%p);
}
int S(int n){
if(n<=N)return sum[n];
int tmp=query(n);
if(tmp!=-)return tmp;
int ans=;
for(int i=,j;i<=n;i=j+){
j=n/(n/i);
ans=inc(ans,(ll)s1(i,j)*S(n/i)%p);
}
ans=sub(s2(n),ans);
add(n,ans);
return ans;
}
inline int f(int n){
int ans=;
for(int i=,j;i<=n;i=j+){
j=n/(n/i);
ans=inc(ans,(ll)S(n/i)*(j-i+)%p);
}
ans=(ll)(ans+n)*INV2%p;
return ans;
}
int main(){
Euler(N);
int a=read(),b=read();
printf("%d\n",sub(f(b),f(a-)));
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
51NOD 1227:平均最小公倍数——题解的更多相关文章
- 51NOD 1227 平均最小公倍数 [杜教筛]
1227 平均最小公倍数 题意:求\(\frac{1}{n} \sum_{i=1}^n lcm(n,i)\) 和的弱化版? \[ ans = \frac{1}{2}((\sum_{i=1}^n \su ...
- 51nod 1227 平均最小公倍数【欧拉函数+杜教筛】
以后这种题能用phi的就不要用mu-mu往往会带着个ln然后被卡常致死 把题目要求转换为前缀和相减的形式,写出来大概是要求这样一个式子: \[ \sum_{i=1}^{n}\sum_{j=1}^{i} ...
- 【51nod】1227 平均最小公倍数
题解 这个故事告诉们数论函数不要往分式上跑,你推不出来 好久没推式子了这么明显的转化我都忘了= = 首先\(A(n) = \frac{1}{n} \sum_{i = 1}^{n} \frac{i * ...
- 51 nod 1227 平均最小公倍数
原题链接 Lcm(a,b)表示a和b的最小公倍数,A(n)表示Lcm(n,i)的平均数(1 <= i <= n), 例如:A(4) = (Lcm(1,4) + Lcm(2,4) + Lcm ...
- 【51nod】1222 最小公倍数计数 莫比乌斯反演+组合计数
[题意]给定a和b,求满足a<=lcm(x,y)<=b && x<y的数对(x,y)个数.a,b<=10^11. [算法]莫比乌斯反演+组合计数 [题解]★具体 ...
- 51NOD 1353:树——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1353 今天小a在纸上研究树的形态,众所周知的,有芭蕉树,樟树,函树,平衡 ...
- 51Nod 有限背包计数问题 题解报告
首先这道题理论上是可以做到O(nlogn)的,因为OEIS上有一个明显可以用多项式乘法加速的式子 但是由于模数不是很兹磁,所以导致nlogn很难写 在这里说一下O(n*sqrt(n))的做法 首先我们 ...
- 【51Nod 1222】最小公倍数计数
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1222 求\([a,b]\)中的个数转化为求\([1,b]\)中的个数减去 ...
- 【51nod】1238 最小公倍数之和 V3 杜教筛
[题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...
随机推荐
- MVC、MVVM
一.MVC 所谓的 MVC 是指: Model: 数据的拥有者,实现具体的业务逻辑. View: 具体的用户界面,如按钮.列表.图片. Controller: 负责将 View 中用户的动作传达给 M ...
- dva框架之redux相关
dva封装了redux,减少很多重复代码比如action reducers 常量等,本文简单介绍dva redux操作流程. 利用官网的一个加减操作小实例来操作: dva所有的redux操作是放在mo ...
- Selenium基础之--01(将浏览器最大化,设置浏览器固定宽、高,操控浏览器前进、后退)
1,将浏览器最大化 我们知道调用启动的浏览器不是全屏的,这样不会影响脚本的执行,但是有时候会影响我们"观看"脚本的执行. coding=utf-8 from selenium im ...
- 学习用MaxScipt批处理Max文件
学习用MaxScipt批处理Max文件 需求 对几百个.max文件中的指定指定名称的骨骼进行重命名. 解决 考虑到是一次性需求,花了两个钟用maxscript实现了功能,基本逻辑把改名规则做成配置文本 ...
- 【system.folder】使用说明
对象:system.folder 说明:提供一系列针对文件夹的操作 目录: 方法 返回 说明 system.folder.exists(folderPath) [True | False] 检测指定文 ...
- php多进程单例模式下的 MySQL及Redis连接错误修复
前几天写了个php常驻脚本,主要逻辑如下 //跑完数据后休息60秒 $sleepTime = 60; $maxWorker = 10; while (true) { $htmlModel = new ...
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- 拓扑排序(Toposort)
摘自:https://blog.csdn.net/qq_35644234/article/details/60578189 <图论算法> 1.拓扑排序的介绍 对一个有向无环图(Direct ...
- “Hello world!”团队第二周贡献分规则+贡献分数分配结果
一.贡献规则制定: (1)基础分:9 , 9 , 8 , 7 , 7 , 7 , 6(按在本次编程中承担模块的重要度制定,某一模块重要度的认定通过组内开会讨论决定) (2)会议分:每人没出勤一次会议记 ...
- LintCode-12.带最小值操作的栈
带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 注意事项 如果堆栈中 ...