疯狂LCM
题目要求求:
\]
先转化成gcd处理:
\]
之后老套路 枚举gcd,并且先把d除进去之后用\(i\)代替\(\frac{i}{d}\)
\]
这时候发现 后面那一项其实是要求求在\(\frac{n}{d}\)以内所有与其互质的数的和。因为当\(gcd(d,i) = 1\)时,\(gcd(d-i,i) = 1\),所以这样的数一定是成对出现,有\(\frac{1}{2}\varphi(n)\)对,所以就可以计算这个值。注意当n=1的时候,这个值是1.所以要在后面加上1.
我们现在要求的就是
\]
这玩意咋求呢……?我们首先线性把欧拉函数筛出来,之后虽然他有枚举因子的循环,但实际上我们也这么操作,他每次做的操作次数之和其实是一个调和级数(很像埃氏筛法),是\(O(nlogn)\)的。所以直接这样先预处理出来,之后询问的时候\(O(1)\)出结果即可。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')
#define fr friend inline
#define y1 poj
#define mp make_pair
#define pr pair<int,int>
#define fi first
#define sc second
#define pb push_back
#define I puts("bug")
using namespace std;
typedef long long ll;
const int M = 1000005;
const int INF = 1000000009;
const double eps = 1e-7;
const double pi = acos(-1);
const ll mod = 1e9+7;
ll read()
{
ll ans = 0,op = 1;char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
return ans * op;
}
int T,n,p[M],phi[M],tot;
bool np[M];
ll ans[M];
void euler()
{
np[1] = 1,phi[1] = 1;
rep(i,2,M-2)
{
if(!np[i]) p[++tot] = i,phi[i] = i-1;
for(int j = 1;i * p[j] <= M-2;j++)
{
np[i * p[j]] = 1;
if(i % p[j] == 0) {phi[i * p[j]] = phi[i] * p[j];break;}
phi[i * p[j]] = phi[i] * (p[j] - 1);
}
}
rep(i,1,M-2)
for(int j = 1;j * i <= M-2;j++) ans[i * j] += (ll)i * phi[i] / 2;
rep(i,1,M-2) ans[i] = (ll)ans[i] * i + i;
}
int main()
{
euler();
T = read();
while(T--) n = read(),printf("%lld\n",ans[n]);
return 0;
}
疯狂LCM的更多相关文章
- P1891 疯狂LCM
\(\color{#0066ff}{ 题目描述 }\) 众所周知,czmppppp是数学大神犇.一天,他给众蒟蒻们出了一道数论题,蒟蒻们都惊呆了... 给定正整数N,求LCM(1,N)+LCM(2,N ...
- 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)\) \( ...
- 洛谷 - 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 & 三倍经验
经验给掉先: 经验*1 经验*2 经验*3 这里给个跑得比较慢的 \(n \sqrt n\) 预处理然后 \(O(1)\) 回答询问的做法 式子 首先我们推柿子: \[\begin{aligned}A ...
- 洛谷 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)} ...
- [Luogu1891]疯狂LCM[辗转相减法]
题意 多组询问,每次给定 \(n\) ,求:\(\sum_{i=1}^nlcm(i,n)\) . \(\rm T \leq 3\times 10^4\ ,n \leq 10^6\). 分析 推式子: ...
- 洛谷 - P3768 - 简单的数学题 - 欧拉函数 - 莫比乌斯反演
https://www.luogu.org/problemnew/show/P3768 \(F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}ijgcd(i ...
随机推荐
- DLL注入新姿势:反射式DLL注入研究
在分析koadic渗透利器时,发现它有一个注入模块,其DLL注入实现方式和一般的注入方式不一样.搜索了一下发现是由HarmanySecurity的Stephen Fewer提出的ReflectiveD ...
- php如何读取ini文件
很多时候,我们使用配置文件来读取配置,那么php如何使用ini文件呢? 代码如下: 例如将:数据库信息存到ini文件中,进行读取. <?php header('content-type:text ...
- binary-tree-level-order-traversal I、II——输出二叉树的数字序列
I Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...
- eclipse从svn检出项目
在eclipse的project explorer 右键->import->svn->从svn检出项目,然后填写资源库的位置,完成,然后一直next. 直到项目检出完成后,选择项目, ...
- windows下taskkill命令简介
1.简介 使用该工具可以按照进程 ID (PID) 或映像名称终止任务. 2.语法 TASKKILL [/S system [/U username [/P [password]]]] ...
- 编码知识 (Unicode、UTF-8、ANSI)
1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...
- caffe2 安装与介绍
http://blog.csdn.net/yan_joy/article/details/70241319 标签: 深度学习 2017-04-19 15:31 5970人阅读 评论(0) 收藏 举报 ...
- git 安装及命令
一.window下的git安装 1.安装教程 网上教程一堆.我參考的是这个:Git_Windows 系统下Git安装图解 还有这个也不错 2.环境搭建: 在配置完毕后,自己主动载入到系统环境变量中.如 ...
- 【转】安卓逆向实践5——IDA动态调试so源码
之前的安卓逆向都是在Java层上面的,但是当前大多数App,为了安全或者效率问题,会把一些重要功能放到native层,所以这里通过例子记录一下使用IDA对so文件进行调试的过程并对要点进行总结. 一. ...
- Spark SQL includes a cost-based optimizer, columnar storage and code generation to make queries fast.
https://spark.apache.org/sql/ Performance & Scalability Spark SQL includes a cost-based optimize ...