http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1363

\[\begin{aligned}
&\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)\)

\[\begin{aligned}
&\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】最小公倍数之和的更多相关文章

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

  2. 51nod - 1363 - 最小公倍数之和 - 数论

    https://www.51nod.com/Challenge/Problem.html#!#problemId=1363 求\(\sum\limits_{i=1}^{n}lcm(i,n)\) 先换成 ...

  3. 51nod 1238 最小公倍数之和 V3

    51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...

  4. 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举

    1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...

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

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

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

  7. 51nod 1238 最小公倍数之和 V3 【欧拉函数+杜教筛】

    首先题目中给出的代码打错了,少了个等于号,应该是 G=0; for(i=1;i<=N;i++) for(j=1;j<=N;j++) { G = (G + lcm(i,j)) % 10000 ...

  8. 51nod 1190 最小公倍数之和 V2【莫比乌斯反演】

    参考:http://blog.csdn.net/u014610830/article/details/49493279 这道题做起来感觉非常奇怪啊--头一次见把mu推出来再推没了的-- \[ \sum ...

  9. [51Nod 1238] 最小公倍数之和 (恶心杜教筛)

    题目描述 求∑i=1N∑j=1Nlcm(i,j)\sum_{i=1}^N\sum_{j=1}^Nlcm(i,j)i=1∑N​j=1∑N​lcm(i,j) 2<=N<=10102<=N ...

  10. 【学术篇】51nod 1238 最小公倍数之和

    这是一道杜教筛的入(du)门(liu)题目... 题目大意 求 \[ \sum_{i=1}^n\sum_{j=1}^nlcm(i,j) \] 一看就是辣鸡反演一类的题目, 那就化式子呗.. \[ \s ...

随机推荐

  1. [转] Linux下程序的加载、运行和终止流程

    TAG: linux, main, _start DATE: 2013-08-08 原文地址: http://blog.csdn.net/tigerscorpio/article/details/62 ...

  2. 【CodeForces】915 F. Imbalance Value of a Tree 并查集

    [题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...

  3. 【洛谷 P3203】 [HNOI2010]弹飞绵羊(LCT)

    题目链接 把每个点和能跳到的点连边,于是就构成了一个森林. 查询操作就是该点到根的路径长度,修改操作就相当于删边再重新连边. 显然是\(LCT\)的强项. 查询时\(access(x),splay(x ...

  4. 旋转3D立方体

    <!DOCTYPE html><html><head> <title>css-3d-盒子</title> <meta charset= ...

  5. F - Debate CodeForces - 1070F 思维

    题目链接:https://vjudge.net/problem/CodeForces-1070F 具体思路:首先把所有的00放进去,然后对于10 和01 的取两个数目最小值t,排完序后将前t个加起来, ...

  6. 面试中关于Redis的问题看这篇就够了

    昨天写了一篇自己搭建redis集群并在自己项目中使用的文章,今天早上看别人写的面经发现redis在面试中还是比较常问的(笔主主Java方向).所以查阅官方文档以及他人造好的轮子,总结了一些redis面 ...

  7. MAC和PHY的区别

    一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数据传送与接收所需要的电与光信号.线路状态.时钟基准.数据编码和电路等,并向数据链路层设备提供标准接口.数据链路层 ...

  8. 宝塔Linux面板新手安装教程【转】

    一.使用远程连接软件 (如 Putty.XShell) 连接你的Linux服务器,本教程以 Putty 为例. 1.动 Putty.exe 程序,进入 Putty 主界面. 2.在 Host Name ...

  9. caffe Python API 之Solver定义

    from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...

  10. java基础11 继承(super、extends关键字和重写,这三个要素出现的前提:必须存在继承关系)

    面向对象的三大特征: 1.封装   (将一类属性封装起来,并提供set()和get()方法给其他对象设置和获取值.或者是将一个运算方法封装起来,其他对象需要此种做运算时,给此对象调用) 2.继承   ...