【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;
}

【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)的更多相关文章

  1. $BZOJ$2818 $gcd$ 莫比乌斯反演/欧拉函数

    正解:莫比乌斯反演/欧拉函数 解题报告: 传送门$QwQ$ 一步非常显然的变形,原式=$\sum_{d=1,d\in prim}^{n}\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd ...

  2. [luogu P2586] GCD 解题报告 (莫比乌斯反演|欧拉函数)

    题目链接:https://www.luogu.org/problemnew/show/P2568#sub 题目大意: 计算​$\sum_{x=1}^n\sum_{y=1}^n [gcd(x,y)==p ...

  3. luogu2658 GCD(莫比乌斯反演/欧拉函数)

    link 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 (1)莫比乌斯反演法 发现就是YY的GCD,左转YY的GCD ...

  4. 洛谷 - P1390 - 公约数的和 - 莫比乌斯反演 - 欧拉函数

    https://www.luogu.org/problemnew/show/P1390 求 $\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m} gcd(i,j) $ ...

  5. BZOJ2005:[NOI2010]能量采集(莫比乌斯反演,欧拉函数)

    Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...

  6. HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解

    题意: 给定\(n,m,p\),求 \[\sum_{a=1}^n\sum_{b=1}^m\frac{\varphi(ab)}{\varphi(a)\varphi(b)}\mod p \] 思路: 由欧 ...

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

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

  8. BZOJ.2705.[SDOI2012]Longge的问题(莫比乌斯反演 欧拉函数)

    题目链接 \(Description\) 求\[\sum_{i=1}^n\gcd(i,n)\] \(Solution\) \[ \begin{aligned} \sum_{i=1}^n\gcd(i,n ...

  9. BZOJ4804 欧拉心算(莫比乌斯反演+欧拉函数+线性筛)

    一通套路后得Σφ(d)μ(D/d)⌊n/D⌋2.显然整除分块,问题在于怎么快速计算φ和μ的狄利克雷卷积.积性函数的卷积还是积性函数,那么线性筛即可.因为μ(pc)=0 (c>=2),所以f(pc ...

随机推荐

  1. git删除历史

    Git如何永久删除文件(包括历史记录)   有些时候不小心上传了一些敏感文件(例如密码), 或者不想上传的文件(没及时或忘了加到.gitignore里的), 而且上传的文件又特别大的时候, 这将导致别 ...

  2. Spring 注解Autowired自动注入bean异常解决

      错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...

  3. Win7与虚拟机Linux互通ping的网络设置

    转载请标明出处:http://www.linuxidc.com/Linux/2014-04/100450.htm 虽然从WinXP到Win7一直都可以使用VMWARE虚拟机安装Linux系统,记得每次 ...

  4. JavaScript-2.4 改进的Hello程序,使用div,---ShinePans

    <html> <head> <meta http-equiv="content-type" content="text/html;chars ...

  5. POJ-3134-Power Calculus(迭代加深DFS)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  6. Linux LVM逻辑卷配置过程详解(创建、扩展、缩减、删除、卸载、快照创建)(未完)

    转:http://blog.csdn.net/xuanfeng407/article/details/51465472

  7. 深入理解get和post的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二.最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 正常GET和 ...

  8. 【HDU-5246】超级赛亚ACMer(贪心)

    之前用了个nlogn的算法超时了.仅仅能改成n的算法了 大题贪心思路就是 对每一个人的能力值从小到大进行排序,当前能力值为now,那么我们找到一个人的能力使得这个能力值 <= now.now + ...

  9. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  10. MYSQL分区表功能测试简析

    1.查看Mysql版本是否支持分区  SHOW VARIABLES LIKE '%partition%';   +-------------------+-------+ | Variable_nam ...