[51nod1238] 最小公倍数之和 V3(杜教筛)
题面
题解
懒了……这里写得挺好的……
//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define IT map<ll,int>::iterator
#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;
const int N=6e6+5,P=1e9+7,inv2=500000004,inv6=166666668;
bitset<N>vis;int p[N],phi[N],g[N],m,sqr;ll n;map<ll,int>mp;
inline int calc(R int x){return (1ll*x*(x+1)>>1)%P;}
inline int calc2(R int x){return 1ll*x*(x+1)%P*((x<<1)+1)%P*inv6%P;}
inline int calc3(R int x){x=calc(x);return 1ll*x*x%P;}
void init(int n){
phi[1]=g[1]=1;
fp(i,2,n){
if(!vis[i])p[++m]=i,phi[i]=i-1,g[i]=1ll*phi[i]*i%P*i%P;
for(R int j=1,k;j<=m&&1ll*i*p[j]<=n;++j){
vis[k=i*p[j]]=1;
if(i%p[j]==0){phi[k]=phi[i]*p[j],g[k]=1ll*phi[k]*k%P*k%P;break;}
phi[k]=phi[i]*(p[j]-1),g[k]=1ll*g[i]*g[p[j]]%P;
}
}
fp(i,2,n)(g[i]+=g[i-1])%=P;
}
int G(ll n){
if(n<=sqr)return g[n];
IT it=mp.find(n);
if(it!=mp.end())return it->second;
int res=calc3(n%P),las=1,now;
for(ll i=2,j;i<=n;i=j+1)
j=n/(n/i),now=calc2(j%P),res=(res-1ll*(now-las+P)*G(n/i)%P+P)%P,las=now;
return mp[n]=res;
}
int main(){
// freopen("testdata.in","r",stdin);
scanf("%lld",&n),init(sqr=N-5);
int res=0,las=0,now=0;
for(R ll i=1,j;i<=n;i=j+1)
j=n/(n/i),now=calc(j%P),(res+=1ll*(now-las+P)*G(n/i)%P)%=P,las=now;
printf("%d\n",res);
return 0;
}
[51nod1238] 最小公倍数之和 V3(杜教筛)的更多相关文章
- [51Nod1238]最小公倍数之和 V3[杜教筛]
题意 给定 \(n\) ,求 \(\sum_{i=1}^n \sum_{j=1}^n lcm(i,j)\). \(n\leq 10^{10}\) 分析 推式子 \[\begin{aligned} an ...
- 51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...
- 【51nod】1238 最小公倍数之和 V3 杜教筛
[题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...
- 51 Nod 1238 最小公倍数之和 V3 杜教筛
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1238 题意:求$\sum_{i=1}^{n}\sum_{j=1}^{n}l ...
- 51NOD 1237 最大公约数之和 V3 [杜教筛]
1237 最大公约数之和 V3 题意:求\(\sum_{i=1}^n\sum_{j=1}^n(i,j)\) 令\(A(n)=\sum_{i=1}^n(n,i) = \sum_{d\mid n}d \c ...
- [51Nod 1238] 最小公倍数之和 (恶心杜教筛)
题目描述 求∑i=1N∑j=1Nlcm(i,j)\sum_{i=1}^N\sum_{j=1}^Nlcm(i,j)i=1∑Nj=1∑Nlcm(i,j) 2<=N<=10102<=N ...
- 51nod 237 最大公约数之和 V3 杜教筛
Code: #include <bits/stdc++.h> #include <tr1/unordered_map> #define setIO(s) freopen(s&q ...
- 51nod1238 最小公倍数之和 V3 莫比乌斯函数 杜教筛
题意:求\(\sum_{i = 1}^{n}\sum_{j = 1}^{n}lcm(i, j)\). 题解:虽然网上很多题解说用mu卡不过去,,,不过试了一下貌似时间还挺充足的,..也许有时间用phi ...
- 51nod 1244 莫比乌斯函数之和 【杜教筛】
51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...
- 51nod 1244 莫比乌斯函数之和(杜教筛)
[题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...
随机推荐
- AngularJS:SQL
ylbtech-AngularJS:SQL 1.返回顶部 1. AngularJS SQL 在前面章节中的代码也可以用于读取数据库中的数据. 使用 PHP 从 MySQL 中获取数据 AngularJ ...
- 杂项:C# 方法、属性杂项-01
ylbtech-杂项:C# 方法.属性杂项-01 1. 属性杂项返回顶部 1. public int ReadCnt { get; set; } 2.设置默认值 public int ReadCnt ...
- d-s证据理论
证据理论是Dempster于1967年首先提出,由他的学生Shafer于1976年进一步发展起来的一种不精确推理理论,也称为Dempster/Shafer 证据理论(D-S证据理论),属于人工智能范畴 ...
- Byte和byte[]数组
Byte和byte[]数组,“表示一个 8 位无符号整数, 一般为8位二进制数”. Byte是计算机最基础的存储单位和最基础的通讯单位. 而所有的类型都是支持由byte[]类型转换而来. 为什么说By ...
- dtgrid 手动条件删除表格中的某一行
dtgrid 手动条件删除表格中的某一行 var grid = $.fn.DtGrid.init(dtGridOption); $(function () { grid.load(); }); fun ...
- asp.net mysql 链接类
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...
- DAY10-MYSQL初识
一 数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件 ...
- new与delete,malloc与free
1.new_delete与malloc_free ❶malloc/free: malloc原型: void *malloc(long NumBytes) 该函数分配了NumBytes个字节,并返回了指 ...
- C++实现筛选法
筛选法 介绍: 筛选法又称筛法,是求不超过自然数N(N>1)的所有质数的一种方法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-194年)发明的,又称埃拉托斯特尼筛子. ...
- JDBC03 利用JDBC实现事务提交与回滚【调用Connection中的方法实现事务管理】
目录 1 Connection中的重用方法 2 JDBC事务管理经典案例 1 Connection类中常用的方法回顾 1.1 Statement createStatement() throws SQ ...