2226: [Spoj 5971] LCMSum

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 1949  Solved: 852
[Submit][Status][Discuss]

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

Source

[Submit][Status][Discuss]

一个比较有用的式子:$$f(n)=\sum_{i=1}^{n}[gcd(i,n)=1]i$$$$f(1)=1\quad f(n)=\lfloor \frac{\varphi(n)*n}{2} \rfloor$$

然后按照套路化式子即可:https://blog.sengxian.com/solutions/bzoj-2226

线性筛WA两次,怎么回事啊?

 #include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=l; i<=r; i++)
typedef long long ll;
using namespace std; const int N=;
int n,T,tot,p[N],phi[N];
bool b[N];
ll g[N]; void pre(){
phi[]=;
for (int i=; i<N; i++){
if (!b[i]) p[++tot]=i,phi[i]=i-;
for (int j=; j<=tot && i*p[j]<N; j++){
int t=i*p[j]; b[t]=;
if (i%p[j]) phi[t]=(p[j]-)*phi[i];
else { phi[t]=p[j]*phi[i]; break; }
}
}
for (int i=; i<N; i++) for (int j=i; j<N; j+=i) g[j]+=1ll*phi[i]*i;
} int main(){
freopen("bzoj2226.in","r",stdin);
freopen("bzoj2226.out","w",stdout);
pre();
for (scanf("%d",&T); T--; ) scanf("%d",&n),printf("%lld\n",(n==)?:((n==)?:(g[n]+)*n/));
return ;
}

[BZOJ2226][SPOJ5971]LCMSum(莫比乌斯反演)的更多相关文章

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

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

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

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

  3. BZOJ2226:[SPOJ5971]LCMSum

    Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...

  4. [bzoj2226][Spoj5971]LCMSum_欧拉函数_线性筛

    LCMSum bzoj-2226 Spoj-5971 题目大意:求$\sum\limits_{i=1}^nlcm(i,n)$ 注释:$1\le n\le 10^6$,$1\le cases \le 3 ...

  5. 莫比乌斯反演&各种筛法

    不学莫反,不学狄卷,就不能叫学过数论 事实上大概也不是没学过吧,其实上赛季头一个月我就在学这东西,然鹅当时感觉没学透,连杜教筛复杂度都不会证明,所以现在只好重新来学一遍了(/wq 真·实现了水平的负增 ...

  6. hdu1695 GCD(莫比乌斯反演)

    题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...

  7. BZOJ 2154: Crash的数字表格 [莫比乌斯反演]

    2154: Crash的数字表格 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 2924  Solved: 1091[Submit][Status][ ...

  8. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  9. Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)

    题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...

随机推荐

  1. Vuejs - 花式渲染目标元素

    Vue.js是什么 摘自官方文档: Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库 ...

  2. CSS3禁止网页中文本被选中代码

    通常大家会有js来实现,另一个方案就是,将-webkit-user-select 和-moz-user-select 的值设为none,这针对于移动用户,可能会很有用.请谨慎使用这个属性:因为大部分用 ...

  3. C++学习之路(四):线程安全的单例模式

    (一)简单介绍 单例模式分为两种类型:懒汉模式和饿汉模式. 懒汉模式:在实际类对象被调用时才会产生一个新的类实例,并在之后返回这个实例.多线程环境下,多线程可能会同时调用接口函数创建新的实例,为了防止 ...

  4. nginx源码分析--使用GDB调试(strace、 pstack )

    nginx源码分析--使用GDB调试(strace.  pstack ) http://blog.csdn.net/scdxmoe/article/details/49070577

  5. opengl基础学习专题 (一 )编程环境搭建

    题外话: 第一次在博客园上同大家分享博文.水的的地方,错别字的地方.环境交流.批评.知道了马上改. 以前在百度空间中写技术分享博文,后来百度啥也没说就把整个空间封了.当时感觉 还是有点寒心.只想黑一下 ...

  6. System.getProperty方法中输出路径的方法

    package codegenerator;/** *@author Eilen *@date 2017年9月27日---下午3:15:09 *@描述: *@answer */public class ...

  7. 虚拟机 VMware Workstation12 安装Ubuntu系统

    Ubuntu 系统是一款优秀的.基于GNU/Linux 的平台的桌面系统. 当然,目前为止很多应用程序还完全不能允许运行在 Ubuntu 系统上,而且 Ubuntu 的界面.使用方法会让大部分Wind ...

  8. How ConcurrentHashMap offers higher concurrency without compromising thread safety

    https://www.ibm.com/developerworks/library/j-jtp08223/

  9. W3School WebService教程

    http://www.w3school.com.cn/webservices/index.asp

  10. 查找文件which locate find

    (1)which:查找命令文件路径 which ls //命令的路径查找是根据PATH环境变量 whereis ls echo $PATH //打印PATH环境变量 (2)locate:查找任意文件 ...