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 ...
随机推荐
- andriod 学习三 使用android资源
3.1 android框架中有许多资源,包括布局,字符串,位图,图片....,使用资源之前需要在相应的资源文件中定义资源,然后编译程序时ADT将定义的资源转换成java类并给予唯一的id,而代码中需要 ...
- hdu1052Tian Ji -- The Horse Racing(贪心,细节多)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 怎样安装JMeter
JMeter有图形界面, 而且支持中文! JMeter官网地址: http://jmeter.apache.org/ 点击左上角的下载: 点击下面的.zip后缀的压缩包: 解压到本地: JMeter目 ...
- TW实习日记:第24-25天
项目的交付期是真的赶...一直在不断地修改一些小bug,然后消息推送功能出了一个问题,就是不知道为什么PC端会发送两次消息到移动端后台.其中第一条正常第二条会有乱码不正常,可以说是很奇怪了,一开始都认 ...
- [JSON].connectionValue()
语法: [JSON].connectionValue() 说明: 将对象的所有键值接连成新的字符串值 返回: [String] 示例: Set a = toJson() c = Array(1,2,3 ...
- python中的迭代器与生成器
迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...
- Font Awesome 完美的图标字体
好久没来,虽说鄙人的人气不咋地,但还是很想念自己这一亩二分田地. 近期用在平台开发中,看着设计师摆开阵势,准备大画图标,想着自己将会很KUBI拼凑css-sprite图片,接着写一大堆 class^= ...
- python 打包
一.下载 pip install Pyinstaller 二.使用Pyinstaller 1.使用下载安装的方式安装的Pyinstaller打包方式 将需要打包的文件放在解压得到的Pyinstalle ...
- vmware centOS上网配置笔记
⦁ 修改/etc/sysconfig/network-scripts/ifcfg-eth0文件 (首先查看本机vmware 虚拟网络编辑器中的网关) 重启网络 使用命令:service net ...
- Python3 标准库:calendar,time
1.calendar import calendar print(calendar.month(2008,8)) #某个月 print(calendar.calendar(2008)) #某年 pri ...