嘟嘟嘟




这题跟上一道题有点像,但是我还是没推出来……菜啊

\[\begin{align*}
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\)互质的数的和,则

\[\begin{align*}
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\),于是

\[ans = n * (\sum_{d | n, d > 1} \frac{\varphi(d) * d}{2} + 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的更多相关文章

  1. P1891 疯狂LCM

    \(\color{#0066ff}{ 题目描述 }\) 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N ...

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

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

  3. 题解:洛谷P1891 疯狂LCM

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

  4. 洛谷 P1891 疯狂LCM 题解

    原题链接 享受推式子的乐趣吧 数论真有趣! 庆祝:数论紫题第 \(3\) 道. \[\sum_{i=1}^n \operatorname{lcm}(i,n) \] \[= \sum_{i=1}^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. luogu P1616 疯狂的采药

    题目背景 此题为NOIP2005普及组第三题的疯狂版. 此题为纪念LiYuxiang而生. 题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的 ...

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

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

  9. 疯狂LCM

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

随机推荐

  1. eclipse连接VisualSVN Server

    1.下载安装VisualSVN Server 2.修改资源库的网络连接.去掉默认的选中,修改端口,点击ok. 3.新建资源库Test,显示连接的地址http://svnybb/svn/Test/ .之 ...

  2. 【公众号转载】MyBatis拦截器原理探究

    MyBatis拦截器介绍 MyBatis提供了一种插件(plugin)的功能,虽然叫做插件,但其实这是拦截器功能.那么拦截器拦截MyBatis中的哪些内容呢? 我们进入官网看一看: MyBatis 允 ...

  3. A simple problem(hdu2522)

      A simple problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. 前端SEO

    一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...

  5. structs2.8创建拦截器

    控制层 public class PrintUsername { private String username; public String getUsername() { return usern ...

  6. SD从零开始45-46

    [原创] SD从零开始45 运输流程的控制 运输业务场景的例子Examples 一个公司可使用不同的运输业务场景,通过不同的处理类型或者运输方式来刻画: 要模型化这些不同的装运,你可以在配置中定义装运 ...

  7. 【webpack】webpack.base.conf.js基础配置

    var path = require('path') // node路径模块 var utils = require('./utils') // 对vue-loader对于css预编译一些提取的工具模 ...

  8. ActiveReports 报表应用教程 (12)---交互式报表之贯穿钻取

    在葡萄城ActiveReports报表中提供强大的数据分析能力,您可以通过图表.表格.图片.列表.波形图等控件来实现数据的贯穿钻取,在一级报表中可以通过鼠标点击来钻取更为详细的数据. 本文展示的是20 ...

  9. ONLYOFFICE连接数20个限制的由来

    搜onlyoffice document server的github上的issue,会得到这2个地址https://github.com/ONLYOFFICE/DocumentServer/issue ...

  10. 与HttpSession相关的监听器

    概述 与HttpSession相关的监听器有四个:分别是HttpSessionListener.HttpSessionAttributeListener.HttpSessionBindingListe ...