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. 机器学习PAI快速入门

    什么是机器学习? 机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以 ...

  2. Nginx+php-fpm 502 504问题

    常出现502,504错误 思路: 增大nginx与php-fpm通信时的缓存文件大小与个数 静态绑定5个php-fpm进程,减少频率启动进程的开销 利用sock文件代替tcp端口通信 解决办法: 调整 ...

  3. C语言基础 (10) 变量作用域,生命周期 内存结构

    01 课程回顾 1.指针数组 注意: 对于数组来说,在使用sizeof的时候a和&a[0]是不一样的, 虽然以%x打印出来他们都是地址 2.值传递 int a; fun(a); int *** ...

  4. call和apply的使用

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 以checked选中作为判断条件的各种写法

    <input type="radio" name="choice" id="ipt1"> <label for=" ...

  6. nyoj252-01串

    01串 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有"11"子串的这种长度的0 ...

  7. [luogu4053 JSOI2007] 建筑抢修 (贪心 优先队列)

    传送门 题目描述 小刚在玩JSOI提供的一个称之为"建筑抢修"的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的损伤 ...

  8. ubuntu上的arm-elf-tools -20040427.sh 下载及安装问题的解决

    要完成uclinux在ARM上的移植,必须有这个工具. 下载地址:http://opensrc.sec.samsung.com/download.html 这个网站上还有许多其它资源可以下载.我选择 ...

  9. oracle常见sql语句优化

    1.* 号引起的运行效率 尽量降低使用select * 来进行查询,当你查询使用*, 数据库会进行解析并将*转换为所有列. select count(si.student_id)  from Stud ...

  10. Linux--对文件夹下的配置文件批量改动IP

    sed -i 's/10.1.1.1/10.1.1.2/g' `grep -ir 10.1.1.1 * |grep -E '.xml:|.cfg:|.ini:|.wsdl:|.properties:' ...