\(\color{#0066ff}{ 题目描述 }\)

众所周知,czmppppp是数学大神犇。一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了。。。

给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N)。

\(\color{#0066ff}{输入格式}\)

第一行一个数T,表示有T组数据。

对于每组数据,一行,一个正整数N。

\(\color{#0066ff}{输出格式}\)

T行,每行为对应答案。

\(\color{#0066ff}{输入样例}\)

3
1
2
5

\(\color{#0066ff}{输出样例}\)

1
4
55

\(\color{#0066ff}{数据范围与提示}\)

对于30%的数据,1≤T≤5,1≤N≤100000

对于100%的数据,1≤T≤300000,1≤N≤1000000

\(\color{#0066ff}{ 题解 }\)

题目要求

\[\sum_{i=1}^n lcm(i,n)
\]

转为gcd形式

\[n*\sum_{i=1}^n \frac{i}{gcd(i,n)}
\]

枚举gcd

\[\sum_{d=1}^n n\sum_{i=1}^n [gcd(i,n)==d] \frac i d
\]

把d弄前面去

\[n\sum_{d|n}\sum_{i=1}^{\lfloor\frac n d \rfloor} [gcd(i,\frac n d)==1] i
\]

额,后面的的东西就是与一个数互质的数的和

但是我们只能求个数

考虑若\(gcd(i,n)=1\),则\(gcd(n-i,n)=1\)

显然i一定成对出现

要特判一下1

所以,原式可以变为

\[n\sum_{d|n} \frac {\varphi(\lfloor\frac n d \rfloor)*\lfloor\frac n d \rfloor + 1} {2}
\]

这样最后的复杂度是\(O(T\sqrt n)\)的

不太好卡进去

因为时间浪费在了枚举因子

看到题目n的范围,显然可以开一个数组记录n的答案

这样是\(O(T+n\sqrt n)\)的

考虑枚举倍数,减少无用枚举

\(O(T+nlogn)\)可过

#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e6 + 10;
LL ans[maxn], phi[maxn], pri[maxn], tot;
bool vis[maxn];
LL getans(LL n) {
return (phi[n] * n + 1) >> 1;
}
void predoit() {
phi[1] = 1;
for(int i = 2; i < maxn; i++) {
if(!vis[i]) pri[++tot] = i, phi[i] = i - 1;
for(int j = 1; j <= tot && (LL)i * pri[j] < maxn; j++) {
vis[i * pri[j]] = true;
if(i % pri[j] == 0) {
phi[i * pri[j]] = phi[i] * pri[j];
break;
}
else phi[i * pri[j]] = phi[i] * (pri[j] - 1);
}
}
for(int i = 1; i < maxn; i++)
for(int j = i; j < maxn; j += i)
ans[j] += getans(j / i);
}
int main() {
predoit();
for(int T = in(); T --> 0;) {
LL n = in();
printf("%lld\n", n * ans[n]);
}
return 0;
}

P1891 疯狂LCM的更多相关文章

  1. 洛谷 - P1891 - 疯狂LCM - 线性筛

    另一道数据范围不一样的题:https://www.cnblogs.com/Yinku/p/10987912.html $F(n)=\sum\limits_{i=1}^{n} lcm(i,n) $ $\ ...

  2. 题解:洛谷P1891 疯狂LCM

    原题链接 题目描述 描述: 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N) ...

  3. 洛谷 P1891 疯狂LCM 题解

    原题链接 享受推式子的乐趣吧 数论真有趣! 庆祝:数论紫题第 \(3\) 道. \[\sum_{i=1}^n \operatorname{lcm}(i,n) \] \[= \sum_{i=1}^n \ ...

  4. luogu P1891 疯狂LCM

    嘟嘟嘟 这题跟上一道题有点像,但是我还是没推出来--菜啊 \[\begin{align*} ans &= \sum_{i = 1} ^ {n} \frac{i * n}{gcd(i, n)} ...

  5. 洛咕 【P1891】疯狂LCM & 三倍经验

    经验给掉先: 经验*1 经验*2 经验*3 这里给个跑得比较慢的 \(n \sqrt n\) 预处理然后 \(O(1)\) 回答询问的做法 式子 首先我们推柿子: \[\begin{aligned}A ...

  6. luogu1891 疯狂lcm ??欧拉反演?

    link 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N). 多组询问,1≤T≤300000,1≤N≤1000000 \(\sum_{i=1}^nlcm(i,n)\) \( ...

  7. [Luogu1891]疯狂LCM[辗转相减法]

    题意 多组询问,每次给定 \(n\) ,求:\(\sum_{i=1}^nlcm(i,n)\) . \(\rm T \leq 3\times 10^4\ ,n \leq 10^6\). 分析 推式子: ...

  8. 疯狂LCM

    传送门 题目要求求: \[\sum_{i=1}^nlcm(i,n)\] 先转化成gcd处理: \[n\sum_{i=1}^n\frac{i}{gcd(i,j)}\] 之后老套路 枚举gcd,并且先把d ...

  9. 2021record

    2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...

随机推荐

  1. python第三十二天-----算法

    算法(Algorithm):一个计算过程,解决问题的方法时间复杂度:用来评估算法运行效率的一个东西ps:在日常使用中,请使用sort(),because no zuo no die! 1.冒泡排序:指 ...

  2. mysql 异常宕机 ..InnoDB: Database page corruption on disk or a failed,,InnoDB: file read of page 8.

    mysql 测试环境异常宕机 系统:\nKylin 3.3 mysql版本:5.6.15--yum安装,麒麟提供的yum源数据库版本 error日志 181218 09:38:52 mysqld_sa ...

  3. rails登录后跳转到登录前的路径

    # 重定向到存储的地址或默认地址 def redirect_back_or(default) redirect_to(session[:forwarding_url] || default) sess ...

  4. 五颜六色的记事本 Notepad2.cn

    这是一款五颜六色的记事本,支持同时五种颜色的标签录入,可随意切换. 考虑到使用者的用眼舒适度,特意采用颜色对比明显并且色调柔和的配色方案,选择通用的微软雅黑字体作为编辑字体,字体工整便于识别. 针对使 ...

  5. linux 权限 homework

    作业一: 1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” useradd natasha -u 1000 -g 555 -c "master&qu ...

  6. .Net 使用 GregorianCalendar 获取任意一天为一年的第几周

    private static int GetWeekOfYear(DateTime dt) { GregorianCalendar gc = new GregorianCalendar(); int ...

  7. IDEA中Git实战

    工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...

  8. Vim 配置文件===/etc/vimrc

    1.替换方法 替换对应的vimrc文件,定制自己的vimrc /etc/vimrc              替换此文件: /home/lmy/.vimrc     只对当前用户有效: Ubuntu9 ...

  9. volatile关键字在多线程中的作用

  10. springmvc 路径问题

    web项目中的相对路径可以分为二类: 1.以斜杠开头:以斜杠开头的又分为二类(分类依据是斜杠出现的位置):如果出现在java代码或者配置文件(xml,properties等),这个路径叫做后台路径. ...