luogu P1891 疯狂LCM
嘟嘟嘟
这题跟上一道题有点像,但是我还是没推出来……菜啊
ans
&= \sum_{i = 1} ^ {n} \frac{i * n}{gcd(i, n)} \\
&= n * \sum_{d | n} \sum_{i = 1} ^ {n} [gcd(i, n) = d] * \frac{i}{d} \\
&= n * \sum_{d | n} \sum_{i = 1} ^ {\frac{n}{d}} [gcd(i, n) = 1] * i \\
\end{align*}\]
令\(f(n)\)表示小于等于\(n\)且与\(n\)互质的数的和,则
ans
&= n * \sum_{d | n} f(\frac{n}{d}) \\
&= n * \sum_{d | n} f(d)
\end{align*}\]
如果\(i\)与\(n\)互质,那么\(n - i\)一定也和\(n\)互质,所以\(\varphi(n)\)个数两两配对等于\(n\),得到\(f(n) = \frac{\varphi(n) * n}{2}\)。
但是这对\(1\)不成立,因此要特别处理\(f(1) = 1\),于是
\]
这个时候可以每一次\(O(\sqrt{n})\)枚举\(n\)的约数,总复杂度\(O(n + T *\sqrt{n} )\),但是还可以再优化:我们像埃氏筛素数一样,\(O(n \log{n})\)预处理\(f(i)\)。然后\(O(1)\)询问。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n;
int prim[maxn], v[maxn], phi[maxn];
ll ans[maxn];
void init()
{
phi[1] = 1;
for(int i = 2; i < maxn; ++i)
{
if(!v[i]) v[i] = i, phi[i] = i - 1, prim[++prim[0]] = i;
for(int j = 1; j <= prim[0] && i * prim[j] < maxn; ++j)
{
v[i * prim[j]] = prim[j];
if(i % prim[j] == 0)
{
phi[i * prim[j]] = phi[i] * prim[j];
break;
}
else phi[i * prim[j]] = phi[i] * (prim[j] - 1);
}
}
for(int i = 1; i < maxn; ++i)
for(int j = 1; i * j < maxn; ++j)
ans[i * j] += (ll)phi[i] * i;
for(int i = 1; i < maxn; ++i) ans[i] = (ans[i] + 1) * i >> 1;
}
int main()
{
init();
int T = read();
while(T--) n = read(), write(ans[n]), enter;
return 0;
}
luogu P1891 疯狂LCM的更多相关文章
- P1891 疯狂LCM
\(\color{#0066ff}{ 题目描述 }\) 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N ...
- 洛谷 - 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 \ ...
- 洛咕 【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)\) \( ...
- luogu P1616 疯狂的采药
题目背景 此题为NOIP2005普及组第三题的疯狂版. 此题为纪念LiYuxiang而生. 题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的 ...
- [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 ...
随机推荐
- JS 格林威治时间格式(GMT)格式化
Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, //month & ...
- jQuery自定义数组操作类(类似于List集合的增删改查)
js外部文件,前提需要引入jquery类库. 封装类代码如下: (function ($) { $.List = function () { var _list = new A ...
- leaks工具查找内存泄露
作为一名iOS开发攻城狮,在苹果没有出ARC(自动内存管理机制)时,我们几乎有一半的开发时间都耗费在这么管理内存上.后来苹果很人性的出了ARC,虽然在很大程度上,帮助我们开发者节省了精力和时间.但是我 ...
- 使用JDBC一次执行多条语句(以MySQL为例)
阅读本文需要的先修知识: 最基本的SQL语句 最基本的JDBC操作(如插入单条记录) 如急需使用请直接看最后一段代码. 在JDBC中,对记录进行修改操作最简单的方法是使用executeUpdate() ...
- Mybatis标签bind用法
Mybatis使用bind元素进行模糊查询,不用在乎数据库是mysql还是oracle从而提高可移植性 使用bind元素传递多个参数 public List<Student> findSt ...
- CPU单核多核区别【转载】
CPU个数.CPU核心数.CPU线程数 我们在选购电脑的时候,CPU是一个需要考虑到核心因素,因为它决定了电脑的性能等级.CPU从早期的单核,发展到现在的双核,多核.CPU除了核心数之外,还有线程数之 ...
- python学习之老男孩python全栈第九期_day007作业
一.关系运算 有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合pythons={'alex','egon','yuanhao','wu ...
- js-ES6学习笔记-for...of循环
1.一个数据结构只要部署了Symbol.iterator属性,就被视为具有iterator接口,就可以用for...of循环遍历它的成员.也就是说,for...of循环内部调用的是数据结构的Symbo ...
- MyEclipse中搭建Struts2开发环境
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53205941 冷血之心的博客) 在MyEclipse中如何搭建St ...
- Linux 释放Linux 系统预留的硬盘空间
释放 Linux 系统预留的硬盘空间 by:授客 QQ:1033553122 大多数文件系统都会保留一部分空间作为紧急情况时用(比如硬盘空间满了),这样能保证有些关键应用(比如数据库)在硬盘满的时 ...