首页 技术 正文
技术 2022年11月16日
0 收藏 900 点赞 5,079 浏览 1421 个字

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);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899