【bzoj2226】[Spoj 5971] LCMSum 欧拉函数
题目描述
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 欧拉函数的更多相关文章
- BZOJ2226: [Spoj 5971] LCMSum
题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...
- bzoj 2226 LCMSum 欧拉函数
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1123 Solved: 492[Submit][S ...
- BZOJ2226:LCMSum(欧拉函数)
Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...
- 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 ...
- SPOJ:NT Games(欧拉函数)
Katniss Everdeen after participating in Hunger Games now wants to participate in NT Games (Number Th ...
- 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举
1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...
- 【SPOJ-GCDEX】GCD Extreme(欧拉函数)
题目: SPOJ-GCDEX (洛谷 Remote Judge) 分析: 求: \[\sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j)\] 这道题给同届新生讲过,由于种种原因 ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
随机推荐
- 全国省会城市路网以及POI 数据
- Linux alias理解及设置
1.alias简介 Linux alias 是命令的一种别称,输入 alias 可以看到像下面这样的结果: alias l.='ls -d .* --color=auto' alias ll='ls ...
- Angular 学习笔记——$rounte
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- linux命令之------vmstat使用
在linux命令中,vmstat是个经常用到的分析系统性能的命令之一,主要有两个参数:一个是采样频率,一个是采样的次数.如:vmstat 1 3,意思就是每隔1秒采样1次,总共采样3次. 统计次 ...
- react-native 中使用 mobx
1. 介绍 1.1. 原理 React的render是 状态 转化为树状结构的渲染组件的方法而MobX提供了一种存储,更新 状态 的方法React 和 MobX都在优化着软件开发中相同的问题.Reac ...
- 机房收费系统合作版(二)——初识Git
研究了一天半的Git.查阅了不少资料,这里将Git的运用分为两条线做个简单梳理:本地控制库.远程控制库. **************************************本地控制库**** ...
- 【BIEE】页面跳转以及跳转后返回
报表开发过程中,我们经常会遇到这种问题:知道统计结果,然后根据统计结果去看明细数据 很多人可能首先想到的就是钻探,钻探是一种方法,但是不是唯一的办法,可以使用页面跳转完成. 下面举个例子 页面A 现在 ...
- dispatch_after中时间的计算
dispatch_after中用的时间是纳秒,所以需要进行转换:desDelayInSeconds(目标时间,比如2s)* NSEC_PER_SEC double delayInSeconds = 0 ...
- CSS3:选择器
CSS选择器的作用是找出某类元素,以便我们使用style元素或者外部样式表对这类元素设置样式. 基本选择器 选择器 含义 演示样例 描写叙述 * 选择全部元素 * { border: thin bla ...
- Selection Problem (选择问题)
在一个由n个元素组成的集合中,第i个“顺序统计量(order statistic)”是该集合中第i小的元素.例如,在一个由n个元素组成的集合中,最小值是第1个顺序统计量,最大值是第n个顺序统计量.而“ ...