【51Nod 1363】最小公倍数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1363
&\sum_{i=1}^n\frac{in}{(i,j)}\\
=&n\sum_{d|n}\sum_{i=1}^{\frac nd}\left[\left(i,\frac nd\right)=1\right]i\\
=&n\left(1+\sum_{d|n,d\neq n}\sum_{i=1}^{\left\lfloor\frac {n}{2d}\right\rfloor}\left[\left(i,\frac nd\right)=1\right]\right)\\
=&n+\frac n2\sum_{d|n,d\neq 1}d\varphi(d)
\end{aligned}
\]
重点是统计\(\sum\limits_{d|n,d\neq 1}d\varphi(d)\)
&\sum_{d|n,d\neq 1}d\varphi(d)\\
=&\prod\sum_{j=0}^{c_i}p_i^j\times\varphi\left(p_i^j\right)-1\\
=&\prod\left(1+\sum_{j=1}^{c_i}p_i^{2j-1}\left(p_i-1\right)\right)-1\\
=&\prod\left(1+\frac{p_i^{2c_i+1}-p_i}{p_i+1}\right)-1
\end{aligned}
\]
质因子分解统计就可以了,时间复杂度\(O\left(T\sqrt n\right)\)。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 100000;
const int p = 1000000007;
bool notp[N + 3];
int T, n, prime[N + 3], num = 0, ni[N + 3];
void Euler_shai() {
for (int i = 2; i <= N; ++i) {
if (!notp[i]) prime[++num] = i;
for (int j = 1; j <= num && prime[j] * i <= N; ++j) {
notp[prime[j] * i] = true;
if (i % prime[j] == 0) break;
}
}
ni[1] = 1;
for (int i = 2; i <= N; ++i)
ni[i] = 1ll * (p - p / i) * ni[p % i] % p;
}
int ipow(int a, int b) {
int r = 1, w = a;
while (b) {
if (b & 1) r = 1ll * r * w % p;
w = 1ll * w * w % p;
b >>= 1;
}
return r;
}
int cal(int x) {
int ret = 1, ci;
for (int i = 1, pi = 2; i <= num && pi * pi <= x; pi = prime[++i]) {
if (x % pi == 0) {
ci = 1; x /= pi;
while (x % pi == 0) x /= pi, ++ci;
ret = 1ll * ret * (1ll * (ipow(pi, 2 * ci + 1) - pi + p) % p * ni[pi + 1] % p + 1) % p;
}
}
if (x > 1) {
ret = 1ll * ret * ((1ll * x * x % p * x % p - x + p) % p * ipow(x + 1, p - 2) % p + 1) % p;
}
return ret;
}
int main() {
Euler_shai();
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
printf("%lld\n", (1ll * n * ni[2] % p * (cal(n) - 1) % p + n) % p);
}
return 0;
}
【51Nod 1363】最小公倍数之和的更多相关文章
- 51nod 1363 最小公倍数之和 ——欧拉函数
给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30,6,加在一起 = 66. 由于结果很大,输出Mod 1000 ...
- 51nod - 1363 - 最小公倍数之和 - 数论
https://www.51nod.com/Challenge/Problem.html#!#problemId=1363 求\(\sum\limits_{i=1}^{n}lcm(i,n)\) 先换成 ...
- 51nod 1238 最小公倍数之和 V3
51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...
- 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举
1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...
- 51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...
- 51nod 1190 最小公倍数之和 V2
给出2个数a, b,求LCM(a,b) + LCM(a+1,b) + .. + LCM(b,b). 例如:a = 1, b = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30 ...
- 51nod 1238 最小公倍数之和 V3 【欧拉函数+杜教筛】
首先题目中给出的代码打错了,少了个等于号,应该是 G=0; for(i=1;i<=N;i++) for(j=1;j<=N;j++) { G = (G + lcm(i,j)) % 10000 ...
- 51nod 1190 最小公倍数之和 V2【莫比乌斯反演】
参考:http://blog.csdn.net/u014610830/article/details/49493279 这道题做起来感觉非常奇怪啊--头一次见把mu推出来再推没了的-- \[ \sum ...
- [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 1238 最小公倍数之和
这是一道杜教筛的入(du)门(liu)题目... 题目大意 求 \[ \sum_{i=1}^n\sum_{j=1}^nlcm(i,j) \] 一看就是辣鸡反演一类的题目, 那就化式子呗.. \[ \s ...
随机推荐
- [php]http的状态码
1.分类 100~199 表示成功接受请求,要求客户端继续提交下一次请求才能完成整个过程处理. 200~299 表示成功接收请求并已完成整个处理过程,常用200 300~399 为完成请求,客户需进一 ...
- 【BZOJ】3036: 绿豆蛙的归宿
[题意]给定DAG带边权连通图,保证所有点都能到达终点n,每个点等概率沿边走,求起点1到终点n的期望长度.n<=10^5. [算法]期望DP [题解]f[i]表示到终点n的期望长度. f[n]= ...
- 【CodeForces】915 F. Imbalance Value of a Tree 并查集
[题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...
- About configuration center of Apollo
A comparison among different configuration management tools Use of Apollo configuration management p ...
- Docker 配置国内镜像拉取中心,Configure docker to use faster registries in China.
Networking in China is really bad when it comes to using some cloud based tools like docker, it's us ...
- 旅游(CSUST省赛选拔赛2+状压dp+最短路)
题目链接:http://csustacm.com:4803/problem/1016 题目: 思路:状压dp+最短路,比赛的时候有想到状压dp,但是最短路部分写挫了,然后就卡死了,对不起出题人~dis ...
- 47、Python面向对象中的继承有什么特点?
继承的优点: 1.建造系统中的类,避免重复操作. 2.新类经常是基于已经存在的类,这样就可以提升代码的复用程度. 继承的特点: 1.在继承中基类的构造(__init__()方法)不会被自动调用,它需要 ...
- js_网页导出pdf文件
打印当前页面,一开始我认为是需要输出pdf的,后来了解的需求是能够打印就可以了.需求既然都研究了,记录下. 更好的打印方式,window.print();会弹出打印对话框,打印的是window.doc ...
- pip install bs4安装失败
使用管理员方式打开命令提示符框,然后pip install bs4即可安装成功:
- 关于parse_str变量覆盖分析
这个漏洞有两个姿势.一个是不存在的时候一个是存在的时候. 经过测试该漏洞只在php5.2中存在,其余均不存在. 倘若在parse_str函数使用的代码上方未将其定义那么即存在变量覆盖漏洞否则不行. 还 ...