Description

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.

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

Constraints
1 <= T <= 300000
1 <= n <= 1000000

Solution

$\sum_{i=1}^{n}lcm(i,n)$
$=\sum_{i=1}^{n}\frac{i\times n}{gcd(i,n)}$
$=\frac{1}{2}(\sum_{i=1}^{n-1}\frac{i\times n}{gcd(i,n)}+\sum_{i=n-1}^{1}\frac{i\times n}{gcd(i,n)})+n$
因为$gcd(a,b)=gcd(a-b,b)$,所以上面的两个$\sum$可以合起来。
$=\frac{1}{2}\sum_{i=1}^{n-1}\frac{n^2}{gcd(i,n)}+n$
设$gcd(i,n)=d$,把式子改为枚举$d$,那么与$n$的$gcd$为$d$的数有$φ(\frac{n}{d})$个。
$=\frac{1}{2}\sum_{d|n}\frac{n^2\times φ(\frac{n}{d})}{d}+n$
设$d'=\frac{n}{d}$,上下约分一下
$=\frac{1}{2}\sum_{d'|n}d'\times φ(d')+n$
预处理出$φ$数组,然后枚举每一个约数去计算它对它所有倍数的贡献,复杂度是调和级数的$O(nlogn)$。

Code

 #include<iostream>
#include<cstring>
#include<cstdio>
#define N (1000009)
#define MAX (1000000)
#define LL long long
using namespace std; inline int read()
{
int x=,w=; char c=getchar();
while (c<'' || c>'') {if (c=='-') w=-; c=getchar();}
while (c>='' && c<='') x=x*+c-'', c=getchar();
return x*w;
} LL T,n,cnt,phi[N],ans[N],vis[N],prime[N]; void Preprocess()
{
phi[]=;
for (int i=; i<=MAX; ++i)
{
if (!vis[i]) prime[++cnt]=i, phi[i]=i-;
for (int j=; j<=cnt && i*prime[j]<=MAX; ++j)
{
vis[i*prime[j]]=;
if (i%prime[j]) phi[i*prime[j]]=phi[i]*(prime[j]-);
else {phi[i*prime[j]]=phi[i]*prime[j]; break;}
}
}
for (int i=; i<=MAX; ++i)
for (int j=i; j<=MAX; j+=i)
ans[j]+=i*phi[i]/;
for (int i=; i<=MAX; ++i) ans[i]=ans[i]*i+i;
} int main()
{
Preprocess();
T=read();
while (T--) n=read(), printf("%lld\n",ans[n]);
}

BZOJ2226:LCMSum(欧拉函数)的更多相关文章

  1. 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数

    题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...

  2. bzoj 2226 LCMSum 欧拉函数

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

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

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

  4. hdu2588 GCD (欧拉函数)

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

  5. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  6. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  7. COGS2531. [HZOI 2016]函数的美 打表+欧拉函数

    题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...

  8. poj2478 Farey Sequence (欧拉函数)

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  9. 51Nod-1136 欧拉函数

    51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...

随机推荐

  1. sqlserver 删除表中数据 id 从1开始

    TRUNCATE  TABLE  TbName   --TbName是表名 但如果TbName中某些字段与其它表有主外键关系,会报错: 无法截断表 'Plants',因为该表正由 FOREIGN KE ...

  2. [日常] Go语言圣经-Goroutines和线程

    Goroutines和线程: 1.动态栈: 1)线程都有一个固定大小的内存块(一般会是2MB)来做栈 2)一个goroutine会以一个很小的栈开始其生命周期,一般只需要2KB,不是固定的:栈的大小会 ...

  3. HDU3534(SummerTrainingDay13-C tree dp)

    Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. Code Signal_练习题_depositProfit

    You have deposited a specific amount of money into your bank account. Each year your balance increas ...

  5. Intellij IDEA 使用GitHub+Git

    1.配置Git路径 打开Settings(File-->Settings) --> 在搜索栏内输入git,回车跳转到Git配置页面 --> 将git的运行路径填入Path to Gi ...

  6. bzoj P5016[Snoi2017]一个简单的询问——solution

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出   get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input ...

  7. python 数据结构中的链表操作

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...

  8. 【查找数字x第k为上的数字】

    #include<stdio.h> #include<math.h> // 求x用10进制表示时的数位长度 int len(int x){ ) ; )+; } // 取x的第k ...

  9. EasyUI 通过 Combobox 实现 AutoComplete 效果

    朋友在做一个web程序,用的EasyUI框架,让我帮忙实现一个自动提示功能.由于之前我也没用过EasyUI框架,就想到了jQueryUI有 AutoComplete 插件,就想直接拿过来用. 但当我将 ...

  10. python学习笔记之——python安装mysqldb后,pycharm导入还是报错问题

    在安装mysqldb过程中遇到,本来已经安装了mysqldb了,但是在pycharm中import   MySQLdb还是报错找不到该模块的问题.解决方法如下:1.file->settings ...