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. Java JDBC批处理插入数据操作(转)

    在此笔记里,我们将看到我们如何可以使用像Statement和PreparedStatement JDBC API来批量在任何数据库中插入数据.此外,我们将努力探索一些场景,如在内存不足时正常运行,以及 ...

  2. extjs_02_grid(显示本地数据,显示跨域数据)

    1.显示表格 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  3. Java基础知识强化之网络编程笔记04:UDP之发送端的数据来自于键盘录入案例

    1. 数据来自于键盘录入 键盘录入数据要自己控制录入结束. 2. 代码实现: (1)发送端: package com.himi.updDemo1; import java.io.IOException ...

  4. 指针做MAP的KEY的TEST

    用struct做map的key会需要"operator <"等等,还会出现奇怪的问题可能. 试了下用指针做key,看看效果: #include <iostream> ...

  5. spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二)

    spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二) >>>>>>>>>>>>>>&g ...

  6. 程序员带你十天快速入门Python,玩转电脑软件开发(三)

    声明:本次教程主要适用于已经习得一门编程语言的程序员.想要学习第二门语言.有梦想,立志做全栈攻城狮的你 . 如果是小白,也可以学习本教程.不过可能有些困难.如有问题在文章下方进行讨论.或者添加QQ群5 ...

  7. .net+easyui系列--datagrid

    加载CSS <link href="../../Public/easyui/SiteEasy.css" rel="stylesheet" type=&qu ...

  8. php5魔术函数、魔术常量

    魔术函数 1.__construct()  实例化对象时被调用,  当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__destruct ...

  9. Java 之文件IO编程 之写入

    package com.sun; /* * 操作对文件IO的写 * 2014-08-10 */ import java.io.*; public class File_Write { public s ...

  10. (转)asp.net基础-HttpModule

    HttpModule是向实现类提供模块初始化和处置事件.当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于H ...