2226: [Spoj 5971] LCMSum

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 578  Solved: 259
[Submit][Status]

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

  这道题将lcm转化为gcd并按照相同gcd分为一组的思路进行,巧妙地将题目转化为求小于等于n且与n互质数的和,而这个值时n*phi(n)/2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1000010
typedef long long qword;
//segma(i*n/gcd(i,n))
//=n*segma(h(n/k)/k)
//h(a)表示与a互质数的和
bool pflag[MAXN];
int prime[MAXN],topp=-;
int phi[MAXN];
void init()
{
int i,j;
int x,y;
for (i=;i<MAXN;i++)
{
if (!pflag[i])
{
prime[++topp]=i;
phi[i]=i-;
}
for (j=;j<=topp && i*prime[j]<MAXN ;j++)
{
pflag[i*prime[j]]=true;
phi[i*prime[j]]=phi[i]*phi[prime[j]];
if (i%prime[j]==)
{
x=i;y=prime[j];
while (x%prime[j]==)
{
x/=prime[j];
y*=prime[j];
}
if (x==)
{
phi[i*prime[j]]=i*(prime[j]-);
}else
{
phi[i*prime[j]]=phi[x]*phi[y];
}
continue;
}
}
}
}
qword h(int x)
{
if (x==)return ;
return (qword)x*phi[x]/;
}
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int nn;
int n,i;
scanf("%d",&nn);
init();
while (nn--)
{
scanf("%d",&n);
qword ans=;
for (i=;i*i<n;i++)
{
if (n%i!=)continue;
ans+=(qword)n*h(n/i);
ans+=(qword)n*h(i);
}
if (i*i==n)
ans+=(qword)n*h(i);
printf("%lld\n",ans);
}
}

bzoj 2226: [Spoj 5971] LCMSum 数论的更多相关文章

  1. 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 ...

  2. BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论

    BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...

  3. BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常

    Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...

  4. BZOJ 2226 [Spoj 5971] LCMSum

    题解:枚举gcd,算每个gcd对答案的贡献,贡献用到欧拉函数的一个结论 最后用nlogn预处理一下,O(1)出答案 把long long 打成int 竟然没看出来QWQ #include<ios ...

  5. BZOJ2226: [Spoj 5971] LCMSum

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

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

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

  7. 【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 ...

  8. bzoj 2226 LCMSum 欧拉函数

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

  9. 三种做法:BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster

    目录 题意 思路 AC_Code1 AC_Code2 AC_Code3 参考 @(bzoj 2780: [Spoj]8093 Sevenk Love Oimaster) 题意 链接:here 有\(n ...

随机推荐

  1. Ⅳ.AngularJS的点点滴滴-- 服务

    服务(Angularjs很多方法都是服务组成的) 1.使用service方法创建的单例服务 <html> <script src="http://ajax.googleap ...

  2. Oracle 设置日志模式

    在NOARCHIVELOG模式下启动和运行一个数据库. 确定闪回恢复区的位置和归档日志目标目录的位置. 步骤一 为归档的重做日志配置FRA和单独的归档日志目标. 首先,设置FRA参数DB_RECOVE ...

  3. linux下安装apache2.2.27

    1.首先下载httpd-2.2.27.tar.gz用linux命令下载 wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.2.27.tar.gz 2 ...

  4. Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC

    Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC 缺少com/sun/tools/inte ...

  5. spring使用aop

    基于spring-framework-4.1.7使用aop >>>>>>>>>>>>>>>>>&g ...

  6. HDFS的Java客户端操作代码(HDFS删除文件或目录)

    1.HDFS删除文件或目录 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.hadoo ...

  7. java实现的可以无限级别添加子节点的菜单树

    网上大部分菜单树,都是单独用js代码来实现的,这样做的缺点是:用户无法动态的设置菜单项,比如,超级管理员可能需要根据每个用户的权限,赋予他们不同的系统功能,不同的功能对应着不同数量的菜单项. 对于此问 ...

  8. JDK和JVM

    JDK: JDK包含的基本组件包括: javac – 编译器,将源程序转成字节码 jar – 打包工具,将相关的类文件打包成一个文件 javadoc – 文档生成器,从源码注释中提取文档 jdb – ...

  9. 滚动效果,marquee便签

    语法: <marquee></marquee> 例子: <marquee><font size=+3 color=red>Hello, World< ...

  10. 在C#中使用正则表达式自动匹配并获取所需要的数据

    转自:http://my.oschina.net/bv10000/blog/111736 正则表达式能根据设置匹配各种数据(比如:e-mail地址,电话号码,身份中号码等等).正则表达式功能强大,使用 ...