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

【BZOJ2226】[Spoj 5971] LCMSum

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

题解:好吧我naive了,别人都用欧拉函数就我用莫比乌斯反演,还是写一发吧~

然后线性筛∑μ(d)d,然后O(nlogn)枚举n的约数就行了

>欧拉函数做法

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int m=1000000;
typedef long long ll;
int n,T,num,tot;
int pri[m/10],to[m*14],next[m*14],head[m+10];
bool np[m+10];
vector<int> v[m+10];
ll sm[m+10],ans;
int main()
{
	int i,j;
	for(i=1;i<=m;i++)	for(j=i;j<=m;j+=i)	to[++tot]=i,next[tot]=head[j],head[j]=tot;
	sm[1]=1;
	for(i=2;i<=m;i++)
	{
		if(!np[i])	pri[++num]=i,sm[i]=1-i;
		for(j=1;j<=num&&i*pri[j]<=m;j++)
		{
			np[i*pri[j]]=1;
			if(i%pri[j]==0)
			{
				sm[i*pri[j]]=sm[i];
				break;
			}
			sm[i*pri[j]]=sm[i]*(1ll-pri[j]);
		}
	}
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n),ans=0;
		for(i=head[n];i;i=next[i])	ans+=sm[n/to[i]]*to[i]*(to[i]+1)>>1;
		printf("%lld\n",ans*n);
	}
	return 0;
}

转载于:https://www.cnblogs.com/CQzhangyu/p/7008252.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值