BZOJ 2226 LCMSum
Description
Given \(n\), calculate the sum \(LCM(1,n) + LCM(2,n) + \cdots + LCM(n,n)\), where \(LCM(i,n)\) denotes the Least Common Multiple of the integers \(i\) and \(n\).
Input
The first line contains \(T\) the number of test cases. Each of the next \(T\) lines contain an integer \(n\).
Output
Output \(T\) lines, one for each test case, containing the required sum.
Sample Input
3
1
2
5
Sample Output
1
4
55
HINT
\(1 \le T \le 300000\)
\(1 \le n \le 1000000\)
题目求$$\sum_{i=1}^{n}LCM(i,n)$$
根据\(LCM\)的公式,即$$\sum_{i=1}^{n}\frac{i \times n}{GCD(i,n)}$$
我们枚举\(GCD\)——\(g\),即$$\sum_{g=1}^{n}[g \mid n]n \sum_{i=1}^{n}i[GCD(i,n)=g]$$
化简一下,转而求$$\sum_{g=1}^{n}[g \mid n]n \sum_{i=1}^{n}i[GCD(\frac{i}{g},\frac{n}{g})=1]$$
变化一下\(i\)的范围:$$\sum_{g=1}^{n}[g \mid n]n \sum_{i=1}^{\frac{n}{g}}i[GCD(i,\frac{n}{g})=1]$$
\(\sum_{i=1}^{\frac{n}{g}}i[GCD(i,\frac{n}{g})=1]\)即\(\frac{n}{g}\)内与之互质的数的和,这个有个公式:$$\sum_{i=1}^{n}i[GCD(n,i)=1]= \frac{\phi(n) \times n}{2}$$
如何证明,假设某个数\(a\)与\(n\)互质,那么\(n-a\)一定也与\(n\)互质,这样的数一共有\(\phi(n)\)个,于是得证,但在\(n=1\)是要特判,于是这个式子就出来了。$$\sum_{i=1}{n}LCM(i,n)=\sum_{g=1}{n}[g \mid n]n \frac{\phi(\frac{n}{g}) \times \frac{n}{g} }{2}$$
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
#define maxn (1000010)
bool exist[maxn]; int n,phi[maxn],prime[maxn],tot;
inline void ready()
{
phi[1] = 1;
for (int i = 2;i < maxn;++i)
{
if (!exist[i]) phi[i] = i-1,prime[++tot] = i;
for (int j = 1;j <= tot;++j)
{
if (i*prime[j] >= maxn) break;
exist[i*prime[j]] = true;
if (i % prime[j] == 0) { phi[i*prime[j]] = phi[i]*prime[j]; break; }
else phi[i*prime[j]] = phi[i]*phi[prime[j]];
}
}
}
inline ll calc(int g)
{
if (g == 1) return 1;
return ((ll)phi[g]*(ll)g>>1);
}
int main()
{
freopen("2226.in","r",stdin);
freopen("2226.out","w",stdout);
ready();
int T; scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
ll ans = 0;
for (int g = 1;g * g <= n;++g)
if (n % g == 0)
{
ans += (ll)n*calc(n / g);
if (g * g != n) ans += (ll)n*calc(g);
}
printf("%lld\n",ans);
}
fclose(stdin); fclose(stdout);
return 0;
}
BZOJ 2226 LCMSum的更多相关文章
- bzoj 2226 LCMSum 欧拉函数
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1123 Solved: 492[Submit][S ...
- BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...
- bzoj 2226: [Spoj 5971] LCMSum 数论
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 578 Solved: 259[Submit][St ...
- BZOJ 2226 【SPOJ 5971】 LCMSum
题目链接:LCMSum 这个题显然就是要我们推式子了……那么就来推一波: \begin{aligned}&\sum_{i=1}^n lcm(i,n) \\=&\sum_{i=1}^n\ ...
- BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...
- BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常
Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...
- BZOJ 2226 [Spoj 5971] LCMSum
题解:枚举gcd,算每个gcd对答案的贡献,贡献用到欧拉函数的一个结论 最后用nlogn预处理一下,O(1)出答案 把long long 打成int 竟然没看出来QWQ #include<ios ...
- 莫比乌斯反演&各种筛法
不学莫反,不学狄卷,就不能叫学过数论 事实上大概也不是没学过吧,其实上赛季头一个月我就在学这东西,然鹅当时感觉没学透,连杜教筛复杂度都不会证明,所以现在只好重新来学一遍了(/wq 真·实现了水平的负增 ...
- BZOJ2226: [Spoj 5971] LCMSum
题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...
随机推荐
- 征服 Redis + Jedis + Spring (三)—— 列表操作【转】
一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 相关链接: 征服 Redis 征服 Re ...
- linux 系统调优2
换作Linux: 1.杀使用内存大,非必要的进程 2.增加连接数 3.磁盘分区的碎片整理 4.服务优化,把不要的服务关闭 5.更换性能更好的硬件,纵向升级 常见优化手段: 1.更换性能更好的硬件,纵 ...
- mysql的limit、order by和group by的用法
程序执行会重复 用mysql很长时间,limit是分页的一个好工具, select * from table_a where num = 4 limit 1,10, select * from tab ...
- 安装指南:Win10下安装CentOs7
系统安装 安装准备 系统:CentOS 7.Win 10 硬件:U盘一枚.PC一台 软件:UltraISO 安装步骤 使用UltraISO将镜像写入U盘 window10使用磁盘管理,空出一个未分配的 ...
- js获取元素transform参数得出的个人理解
之前写页面的时候有试过想用js获取某些元素的translate的数值什么的,但是translate又是transform的子样式(勉强说说),理所当然就是先获取transform样式,再读里面的值. ...
- css hack 大全
各个浏览器的css hack区别属性: IE6: _zoom:1; IE6/7: *zoom:1; IE6/7/8/9 :\9 各个浏览器的css hack区别规则 IE6: *html{} IE7: ...
- 【转】JavaScript中的constructor与prototype
最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...
- .Net程序员学习Linux(一)
本次知识点:Linux系统的多终端切换,linux下的用户,linux远程访问工具使用,linux下重要的目录,命令的组成,通配符,linux的路径问题,文件操作的综合运用 为什么学习linux? 1 ...
- BUG Error:Execution failed for task ':app:dexDebug'.
Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessExceptio ...
- 使用graphics2D给图片上画字符
//读取图片BufferedImage frontImage = ImageIO.read(new File(eCardXMLConfigManager.geteCardXMLConfigManage ...