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 ...
随机推荐
- C# Windows程序窗口置前台的几种方法
这个是从别的地方看来的,放我这里 第一种:SetForegroundWindow,这个方法时灵时不灵.有人说,在自己的程序里把自己的窗口之前一般就不灵,而置前其它程序的窗口就灵.我觉得这是有原因的:当 ...
- [TJOI2015]弦论
我们先求出该字符串的\(SA\)和\(Ht\) 然后分类讨论 \(T=0\)时,每次去掉\(Ht\)往后扫就行 \(T=1\)时,我们考虑\(lcp\)对答案的影响 既然用到\(lcp\),那就要用\ ...
- PHP 基础总结
PHP(Hypertext Preprocessor)是一种被广泛应用的开源通用脚本语言,尤其适用于Web开发.可用于服务端脚本.命令行脚本.桌面应用程序三大领域. PHP 的 SAPI(服务器应用程 ...
- amazeui+canvas绘制二维码
<link rel="stylesheet" type="text/css" href="css/amazeui.min.css"/& ...
- 高性能JavaScript(字符串和正则表达式)
字符串连接 +/+=操作符连接 str += "one" + "two"; 这是常用的连接字符串的方法,它运行的时候会经历下面四个步骤: 1.在内存中创建一个临 ...
- fastclick select 闪退 bug
这时候needsclick就派上用场了 <select class='needsclick'></select> 附上fastclick github上的链接
- hashCode()与equals()方法的对比
Java对于eqauls方法和hashCode方法是这样规定的: 1.如果两个对象相同,那么它们的hashCode值一定要相同: 2.如果两个对象的hashCode相同,它们并不一定相同(上面 ...
- maven 学习笔记--仓库,聚合和继承,私服搭建
仓库 http://blog.csdn.net/wanghantong/article/details/36427433 聚合和继承 http://www.cnblogs.com/xdp-gacl/p ...
- PHP正则自动验证传入数据
本文出至:新太潮流网络博客 /** * [is_string_regular_type 正则自动验证传入数据] * @E-mial wuliqiang_aa@163.com * @TIME 2017- ...
- SQL Server全文搜索
SQL Server全文搜索 看这篇文章之前请先看一下下面我摘抄的全文搜索的MSDN资料,基本上MSDN上关于全文搜索的资料的我都copy下来了 并且非常认真地阅读和试验了一次,并且补充了一些SQL语 ...