\(\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操作Excel,并结合unittest单元测试框架

    第一步:写Excel操作方法 excel_operate.py文件 from openpyxl import load_workbook #引入模块 class MyExcel: def __init ...

  2. Python模块之: ConfigParser 配置文件读取

    Python模块之: ConfigParser 配置文件读取   ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...

  3. JavaScript中给对象添加方法

    在JavaScript中,我们经常要给已定义的对象添加一些方法,如下:    function circle(w,h){      this.width=w;      this.height=h; ...

  4. 部署和调优 2.1 squid正向代理

    安装squid yum install -y squid Squid 官方网站为 http://www.squid-cache.org 打开注释掉的 cache_dir ufs / 缓存目录的位置,大 ...

  5. Spring装配各种类型bean

    一.单属性值的装配 //setter注入,提供无参构造器,提供setXX方法 <property name="" value=""></pro ...

  6. [Python Study Notes]pynput实现对键盘控制与监控

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  7. 卸载sql2008r2简易版

    Sql Server 2008完全卸载方法(其他版本类似)第1/2页作者: 字体:[增加 减小] 类型:转载 本文介绍如何卸载 Microsoft SQL Server 2008的方法.当您按照本文中 ...

  8. PHP如何将XML转成数组

    如果你使用 curl 获取的 xml data $xml = simplexml_load_string($data); $data['tk'] = json_decode(json_encode($ ...

  9. Solr之缓存篇

    原文出自:http://my.oschina.net/u/1026644/blog/123957 Solr在Lucene之上开发了很多Cache功能,从目前提供的Cache类型有: (1)filter ...

  10. Codeforces #505(div1+div2) C Plasticine zebra

    题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...