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;
}

最新文章

  1. Leetcode First Missing Positive
  2. rman归档删除
  3. magento添加分类属性
  4. WPF中的Style(风格,样式)(转)
  5. 3Com Network Supervisor与IBM Tivoli NetView两款网管软件操作视频
  6. Coins (poj 1742 &amp;amp;&amp;amp; hdu 2844 DP)
  7. 基于ZooKeeper的分布式Session实现
  8. JavaWeb中jdbcproperties配置文件
  9. 深入解析 SQL Server 高可用镜像实现原理
  10. PHPCMS v9.6.0 wap模块 SQL注入
  11. 翻译 | Improving Distributional Similarity with Lessons Learned from Word Embeddings
  12. Centos7.4 防火墙配置
  13. 复制id_rsa命令
  14. CH 2101 - 可达性统计 - [BFS拓扑排序+bitset状压]
  15. 重写TreeView,多层级节点下批量显示图片,图片支持缩略图和文件名列表切换,支持调用者动态匹配选中,支持外界拖入图片并添加到对应节点下
  16. linux如何查看一个进程的堆栈
  17. js格式化文件大小,单位:Bytes、KB、MB、GB
  18. 【转】doxygen+graphviz生成工程中的类继承树及函数调用图
  19. 在分享到微信里的网页中,打开qq对话框。
  20. DB_FILE_MULTIBLOCK_READ_COUNT对物理读和IO次数的影响

热门文章

  1. 【BZOJ】3262: 陌上花开
  2. 【BZOJ】1023: [SHOI2008]cactus仙人掌图 静态仙人掌(DFS树)
  3. CALayer---iOS-Apple苹果官方文档翻译之CALayer
  4. mysql数据库 批量替换 某字段中的数据
  5. centOS7 vsftp ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) 启动失败问题?
  6. CRF++模板使用(转)
  7. Supply
  8. skb_reserve(skb,2)中的2的意义
  9. 64_p4
  10. puppet practice