题目描述

Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.

输入

The first line contains T the number of test cases. Each of the next T lines contain an integer n.

输出

Output T lines, one for each test case, containing the required sum.

样例输入

3
1
2
5

样例输出

1
4
55


题解

欧拉函数

其中需要解释一下最后一个式子的推导过程:

有一个结论:当n>2时,小于n且与n互质的数的和等于$\frac{n·\varphi(n)}2$,因为若k与n互质,则n-k一定也与n互质。

当n=2时这个关系式在数值上成立,当n=1时不成立,需要特殊处理。

所以可以先筛欧拉函数,然后枚举d,将1~n所有能够整除d的数的答案加上$\frac{d·\varphi(d)}2$。最后输出答案时再加一点处理即可。

时间复杂度为调和级数的$O(n\ln n)$

#include <cstdio>
#include <algorithm>
#define N 1000010
using namespace std;
typedef long long ll;
const int m = 1000000;
int phi[N] , prime[N] , tot;
ll f[N];
bool np[N];
int main()
{
int i , j , T , n;
for(i = 2 ; i <= m ; i ++ )
{
if(!np[i]) phi[i] = i - 1 , prime[++tot] = i;
for(j = 1 ; j <= tot && i * prime[j] <= m ; j ++ )
{
np[i * prime[j]] = 1;
if(i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
for(i = 2 ; i <= m ; i ++ )
for(j = i ; j <= m ; j += i)
f[j] += (ll)i * phi[i] / 2;
scanf("%d" , &T);
while(T -- ) scanf("%d" , &n) , printf("%lld\n" , (f[n] + 1) * n);
return 0;
}

【bzoj2226】[Spoj 5971] LCMSum 欧拉函数的更多相关文章

  1. BZOJ2226: [Spoj 5971] LCMSum

    题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...

  2. bzoj 2226 LCMSum 欧拉函数

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1123  Solved: 492[Submit][S ...

  3. BZOJ2226:LCMSum(欧拉函数)

    Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...

  4. 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

  5. SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1

    5152. Brute-force Algorithm EXTREME Problem code: BFALG Please click here to download a PDF version ...

  6. SPOJ:NT Games(欧拉函数)

    Katniss Everdeen after participating in Hunger Games now wants to participate in NT Games (Number Th ...

  7. 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举

    1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...

  8. 【SPOJ-GCDEX】GCD Extreme(欧拉函数)

    题目: SPOJ-GCDEX (洛谷 Remote Judge) 分析: 求: \[\sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j)\] 这道题给同届新生讲过,由于种种原因 ...

  9. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

随机推荐

  1. django自定义过滤器及模板标签

    创建一个模板库 不管是写自定义标签还是过滤器,第一件要做的事是创建模板库(Django能够导入的基本结构). 创建一个模板库分两步走: 第一,决定模板库应该放在哪个Django应用下. 如果你通过 m ...

  2. SWIG 多语言接口变换 【转】

    一.             SWIG 是Simple Wrapper and Interface Generator的缩写,是一个帮助使用C或者C++编写的软件创建其他编语言的API的工具.例如,我 ...

  3. 机器学习算法实践:决策树 (Decision Tree)(转载)

    前言 最近打算系统学习下机器学习的基础算法,避免眼高手低,决定把常用的机器学习基础算法都实现一遍以便加深印象.本文为这系列博客的第一篇,关于决策树(Decision Tree)的算法实现,文中我将对决 ...

  4. Unity3d / 3ds max 模型分享站点

    http://www.cgrealm.org/model/ 王国3D模型库 http://www.cgjoy.com/ 游戏特效论坛

  5. Python连接mongodb提取部分字段内数据并写入txt文件

    #coding=utf-8 import sys reload(sys) sys.setdefaultencoding('utf-8') from pymongo import MongoClient ...

  6. ECSHOP去版权(删除ECSHOP所有标识)

    前台部分: 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop前者”ECSHOP演示站”在后台商店设置 – 商店标题修改后者” Powered by ecshop”打开 ...

  7. [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript

    Naive solution for this problem would be caluclate all the possible combinations: const numbers = [1 ...

  8. ES7新特性

    Array.prototype.includes Array.prototype.includes用法都容易和简单.它是一个替代indexOf,开发人员用来检查数组中是否存在值,indexOf是一种尴 ...

  9. 【VBA】查看窗口当前状态

    Excle的窗口分为:最大化,最小化,一般 使用VB代码如何查看窗口的状态呢?代码如下所示: Sub 获取窗口状态() Dim State As String State = Application. ...

  10. 超高逼格Log日志打印

    代码地址如下:http://www.demodashi.com/demo/12646.html 前言 Log日志的打印一直是一个比较头疼的事,怎样才能让自己的log显示更多信息,怎样才能让自己的log ...