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. [Javascript] MetaProgramming: function name

    Each function should have a 'name' property. It can be anonymous, empty, the same as function name, ...

  2. 系统性能优化分析—CPU消耗

    http://blog.csdn.net/longdel/article/details/7296446

  3. PHP多维数组的魅力

    让我们来体验一下多位数组的魅力吧! 多维数组就是一个数组里面包含一个或多个数组,这就是多维数组.下面用多维数组来编写一个小案例(代码在图片下面): <meta http-equiv=" ...

  4. c++ 中关于int,unsigned int , short的关系与应用

    转载:http://www.cppblog.com/xyjzsh/archive/2010/10/20/130554.aspx?opt=admin   int类型比较特殊,具体的字节数同机器字长和编译 ...

  5. Android Studio美化之优雅的logcat

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 先来个图,图样吐sexy: 很简单,跟我走吧,两步: 1. 引入Logger库 首先,这个sexy ...

  6. SET NOCOUNT 的意义.

    SET NOCOUNT 使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息. 语法 SET NOCOUNT { ON | OFF } 当 SET NOCOUNT 为 ON 时, ...

  7. Quartz.net 定时任务在IIS中未按时执行

    IIS 垃圾回收机制下解决Quartz.net 的不执行问题 IIS中涉及了垃圾回收机制,quartz.net 在ASP.NET 项目中可以实现线程监控定时执行任务,但是在IIS7.5机一下版本中涉及 ...

  8. 类库探源——System.Exception

    一.MSDN描述 Exception 类: 表示在应用程序执行期间发生的错误 命名空间 : System 程序集:   mscorlib.dll 继承关系: 常用属性(含字段)和方法: 1. 属性Me ...

  9. C++学习笔记-1-自增和自减运算符

    1. post-increment and pre-increment 的区别 来源:http://www.c4learn.com/c-programming/c-increment-operator ...

  10. ThinkPHP调试模式与日志记录

    1.可以在config.php中进行设置,默认为关闭状态. 'APP_DEBUG'   =>  true 打开\ThinkPHP\Common\debug.php文件可以查看debug的默认设置 ...