【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. ELK学习笔记

    一.elk框架和java1.8环境搭建 1.1: 环境说明 约定: centos6 iptables关闭 如果不关闭的话,需要开放对应的端口访问 selinux关闭 1.2: ELK简介 els:El ...

  2. webpack入门学习总结

    <h5概述</h5> webpack是当下最热门的前端资源模块化管理和打包工具.它可以将许多松散的模块按照依赖和规则打包成符合生产环境部署的前端资源.还可以将按需加载的模块进行代码分 ...

  3. Timus Online Judge 1057. Amount of Degrees(数位dp)

    1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...

  4. Hybird App(一)----第一次接触

    App你知道多少 一 什么是Native App 长处 缺点 二 什么是Web App 长处 缺点 三 什么是Hybrid App 长处 缺点 四 Web AppHybrid AppNative Ap ...

  5. Android中Scrollview、ViewPager冲突问题汇总(已解决)

    显示问题: 1.设置ScrollView的fillViewPort为true 2.设置ViewPager的layout_height为一个固定高度,比如:100dip 冲突问题: 不过ViewPage ...

  6. 关于POI 中单元格背景色设置(转)

    关于POI 中单元格背景色设置(转)   csdn 中找到了用Apache POI 实现单元格背景色的小例子 我用了JDK6 + POI 3.17 ,调试中报错 最终将 CellStyle.SOLID ...

  7. vue 过滤与全文索引

    过滤 与 全文索引 <template> <div> <input type="text" v-model="query"> ...

  8. DropFileName = "svchost.exe" 问题解决方案

    1.至以下链接处下载ATTK扫描工具: http://support.trendmicro.com.cn ... stomizedpackage.exe (32位) http://support.tr ...

  9. 时间序列 R 读书笔记 04 Forecasting: principles and practice

    本章開始学习<Forecasting: principles and practice> 1 getting started 1.1 事件的可预言性 一个时间能不能被预言主要取决于以下三点 ...

  10. 使用maven结合requirejs管理前端脚本

    已有的web项目,一直使用Maven做工程管理,现阶段前端调整为使用requirejs来负责模块加载依赖,同时使用jasmine来完成前端的UT. 便与在maven下统一管理,简单整理了下合在一起的使 ...