P1891 疯狂LCM
\(\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}{ 题解 }\)
题目要求
\]
转为gcd形式
\]
枚举gcd
\]
把d弄前面去
\]
额,后面的的东西就是与一个数互质的数的和
但是我们只能求个数
考虑若\(gcd(i,n)=1\),则\(gcd(n-i,n)=1\)
显然i一定成对出现
要特判一下1
所以,原式可以变为
\]
这样最后的复杂度是\(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的更多相关文章
- 洛谷 - P1891 - 疯狂LCM - 线性筛
另一道数据范围不一样的题:https://www.cnblogs.com/Yinku/p/10987912.html $F(n)=\sum\limits_{i=1}^{n} lcm(i,n) $ $\ ...
- 题解:洛谷P1891 疯狂LCM
原题链接 题目描述 描述: 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N) ...
- 洛谷 P1891 疯狂LCM 题解
原题链接 享受推式子的乐趣吧 数论真有趣! 庆祝:数论紫题第 \(3\) 道. \[\sum_{i=1}^n \operatorname{lcm}(i,n) \] \[= \sum_{i=1}^n \ ...
- luogu P1891 疯狂LCM
嘟嘟嘟 这题跟上一道题有点像,但是我还是没推出来--菜啊 \[\begin{align*} ans &= \sum_{i = 1} ^ {n} \frac{i * n}{gcd(i, n)} ...
- 洛咕 【P1891】疯狂LCM & 三倍经验
经验给掉先: 经验*1 经验*2 经验*3 这里给个跑得比较慢的 \(n \sqrt n\) 预处理然后 \(O(1)\) 回答询问的做法 式子 首先我们推柿子: \[\begin{aligned}A ...
- 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)\) \( ...
- [Luogu1891]疯狂LCM[辗转相减法]
题意 多组询问,每次给定 \(n\) ,求:\(\sum_{i=1}^nlcm(i,n)\) . \(\rm T \leq 3\times 10^4\ ,n \leq 10^6\). 分析 推式子: ...
- 疯狂LCM
传送门 题目要求求: \[\sum_{i=1}^nlcm(i,n)\] 先转化成gcd处理: \[n\sum_{i=1}^n\frac{i}{gcd(i,j)}\] 之后老套路 枚举gcd,并且先把d ...
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
随机推荐
- CreateThread demo
#include "stdafx.h"#include<windows.h>#include<strsafe.h>//win2003SDK必须安装 要不无此 ...
- pyodbc连接MySQL数据库
1:Python安装pyodbc:pip install pyodbc 2:安装unixODBC-2.3.4.tar.gz ./configure make make install 3:下载基于py ...
- RandomForestClassifier(随机森林检测每个特征的重要性及每个样例属于哪个类的概率)
#In the next recipe, we'll look at how to tune the random forest classifier. #Let's start by importi ...
- oracle 基础 执行sql文件
Oracle执行外部文件: c:>sqlplus user/pwd@db sql>@new.sql 执行多个sql文件: 1.把所有的文件都放在同一个目录下,然后在命令行里执行命令: ...
- MVC5网站部署到IIS7
server 2008R2+IIS7.5下配置不会出现什么问题,这里记录下在server2008+IIS7下的配置 参考了一下:http://www.cnblogs.com/fcu3dx/p/3773 ...
- 记录下Linux难记实用的命令
看文件大小:du -sm * | sort -n 合并多个文件,可以跨文件夹合并:cat *_.txt >> news.txt 给文件改编码:iconv -f GBK -t UTF-8 原 ...
- oracle 常用set命令
SQL> set timing on; //设置显示“已用时间:XXXX”SQL> set autotrace on; //设置允许对执行的sql进行分析 ...
- ES01 数据类型、正则表达式、身份证校验
1 基本数据类型 参见W3C的教程即可 2 正则表达式 参考博文:点击前往 3 身份证校验 参考博文:点击前往 <div class="panel panel-primary" ...
- 【转】nginx禁止访问某个文件和目录(文件夹)
nginx禁止访问所有.开头的隐藏文件设置 location ~* /.* {deny all;} nginx禁止访问目录, 例如:禁止访问path目录 location ^~ /path {deny ...
- JavaWeb 没用
Servlet的生命周期 初始化:Web容器加载servlet,调用innit(),只执行一次 处理业务: 请求到达时,运行service方法 并调用相应的doget或者dopost方法. 可执行多 ...