Code:

#include <bits/stdc++.h>
#include <tr1/unordered_map> #define setIO(s) freopen(s".in","r",stdin)
#define ll long long
#define ull unsigned long long
#define maxn 10000000
#define mod 1000000007
#define inv 500000004 using namespace std;
using namespace tr1;
int vis[maxn],prime[maxn],tot;
ll phi[maxn];
unordered_map<ll,ull>ansphi;
void init(){
phi[1] = 1;
for(int i=2;i<maxn; ++i) {
if(!vis[i]) prime[++tot]=i,phi[i] = i-1;
for(int j=1;j<=tot&&i*prime[j]<maxn;++j) {
vis[i*prime[j]]=1;
if(i%prime[j]!=0) phi[i*prime[j]]=phi[i]*(prime[j]-1);
else {
phi[i*prime[j]]=phi[i]*(prime[j]);
break;
}
}
}
for(int i=1;i<maxn;++i) phi[i]+=phi[i-1],phi[i]%=mod;
}
ll solve(ll n){
if(n < maxn) return phi[n];
if(ansphi[n]) return ansphi[n];
ll ans=(ull)(((n%mod)*((n+1)%mod) %mod)*(inv%mod))%mod;
ll ans2=0;
for(ll l=2,r;l<=n;l=r+1) {
r=n/(n/l);
ans2+=(ll)(r-l+1)*solve(n/l);
ans2%=mod;
}
return ansphi[n]=(ans+mod-ans2)%mod;
}
int main(){
//setIO("input");
init();
ll n,ans=0,ans1,tmp;
scanf("%lld",&n);
for(ll l=1,r;l<=n;l=r+1){
r=(n/(n/l));
ans+=(((n/l)%mod)*((n/l)%mod)%mod*(solve(r)+mod-solve(l-1))%mod)%mod;
ans%=mod;
}
printf("%lld",ans);
return 0;
}

  

51nod 237 最大公约数之和 V3 杜教筛的更多相关文章

  1. 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 ...

  2. 【51nod】1238 最小公倍数之和 V3 杜教筛

    [题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...

  3. 51nod 1244 莫比乌斯函数之和 【杜教筛】

    51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...

  4. 51nod 1244 莫比乌斯函数之和(杜教筛)

    [题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...

  5. [51Nod1238]最小公倍数之和 V3[杜教筛]

    题意 给定 \(n\) ,求 \(\sum_{i=1}^n \sum_{j=1}^n lcm(i,j)\). \(n\leq 10^{10}\) 分析 推式子 \[\begin{aligned} an ...

  6. 51NOD 1238 最小公倍数之和 V3 [杜教筛]

    1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...

  7. 51 Nod 1238 最小公倍数之和 V3 杜教筛

    题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1238 题意:求$\sum_{i=1}^{n}\sum_{j=1}^{n}l ...

  8. 51nod 1237 最大公约数之和 V3(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...

  9. 51Nod.1237.最大公约数之和 V3(莫比乌斯反演 杜教筛 欧拉函数)

    题目链接 \(Description\) \(n\leq 10^{10}\),求 \[\sum_{i=1}^n\sum_{j=1}^ngcd(i,j)\ mod\ (1e9+7)\] \(Soluti ...

随机推荐

  1. [POI2005]AUT-The Bus 树状数组维护最大前缀和

    #include<cstdio> #include<algorithm> using namespace std; const int N=100000+3; int x[N] ...

  2. 算法8-----Different Ways to Add Parentheses(不同括号结果)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

  3. vue router 配置默认页/404页面

    *号通配404默认页面

  4. docker数据卷的使用 -v --volumes--from

    总结一下docker数据管理的三种方法: 1.普通的挂在数据: -v docker run  -v /father/path:/child/path-v 参数会把当前系统的文件目录/father/pa ...

  5. 《你又怎么了我错了行了吧》【Alpha】Scrum meeting 1

    第一天 日期:2019/6/14 前言: 第1次会议在9C-405召开 4个人讨论了整体代码的框架.布局.找出需要改进的地方重点讨论.明确编码的具体分工,每个人搭建好环境. 1.1 今日完成任务情况以 ...

  6. Android学习总结(4)——Andorid Studio熟练使用

    前言 该文以Android Studio2.1.1(Bundle)为例.JDK使用的是1.8版本,也建议大家使用1.8版本. 使用技巧无先后顺序. Android Studio 2.1.1 软件下载 ...

  7. @Autowired 作用范围

    一.@AutoWired 可以作用于:构造器.方法.参数.属性 二.作用在方法上 @Component public class Student{ private Book book; public ...

  8. BigDecimal类(高精度小数)

    位置:java.math.BigDecimal 作用:提供高精度小数数据类型及相关操作 一.基本介绍 BigDecimal为不可变的.任意精度的有符号十进制数,其值为(unscaledValue * ...

  9. 《编程导论(Java)&#183;7.4.4 String对象问题》

    String是Java API中使用频率第一的类,曾经在CSDN论坛上.至少每一个月都有相关的贴子,讨论==和equals(). 本节介绍这一部分的内容,也有一个重要更正. String文字在程序中被 ...

  10. 页面与后台传递中文乱码问题(java乱码)

    1.前台中文传递到后台乱码. 前台不须要处理, 系统一般都会默认把中文转化为ISO-8859-1类型. 仅仅需在后台接受数据是处理 Str为前台传过来的中文字符串: String inputer = ...