[BZOJ2226][SPOJ5971]LCMSum(莫比乌斯反演)
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
5Sample Output
1
4
55HINT
Constraints
1 <= T <= 300000
1 <= n <= 1000000Source
一个比较有用的式子:$$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(莫比乌斯反演)的更多相关文章
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...
- BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常
Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...
- BZOJ2226:[SPOJ5971]LCMSum
Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...
- [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 ...
- 莫比乌斯反演&各种筛法
不学莫反,不学狄卷,就不能叫学过数论 事实上大概也不是没学过吧,其实上赛季头一个月我就在学这东西,然鹅当时感觉没学透,连杜教筛复杂度都不会证明,所以现在只好重新来学一遍了(/wq 真·实现了水平的负增 ...
- hdu1695 GCD(莫比乌斯反演)
题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...
- BZOJ 2154: Crash的数字表格 [莫比乌斯反演]
2154: Crash的数字表格 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 2924 Solved: 1091[Submit][Status][ ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
随机推荐
- windows phone 8.1如何访问应用商店,商店评论的连接
Windows Phone 8.1 中可以使用这个链接跳转到应用评论页面: await Windows.System.Launcher.LaunchUriAsync( new Uri("ms ...
- ORA-02291:parent key not found
Hibernate operation: Could not execute JDBC batch update; SQL [insert into dchnpricecarchancesource ...
- Deep Learning基础--参数优化方法
1. 深度学习流程简介 1)一次性设置(One time setup) -激活函数(Activation functions) - 数据预处理(Data Preprocessing) ...
- 【bzoj3545】peaks
离线一下,动态开点+线段树合并,然后权值线段树上询问kth即可. #include<bits/stdc++.h> ; *; using namespace std; ; inline in ...
- Mysql 数据库学习笔记05 触发器
一.触发器 * 是由时间来出发某个操作,这些事件可以包括 insert.update.delete.等语句.当执行这些操作时,就会触发对应的操作. * 创建一个执行语句的触发器: create tri ...
- 【python】日志系统
来源: http://blog.csdn.net/wykgf/article/details/11576721 http://www.jb51.net/article/42626.htm http:/ ...
- 【python】pip的使用
来源:http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/ pip是用来安装python相关的包的.使用参数如下: # pip - ...
- linux命令(3):rpm命令
查询当前环境是否已安装软件包,如下命令: [root@cloud ~]# rpm -qa | grep httpd httpd-2.4.6-31.el7.centos.1.x86_64 httpd-t ...
- 四:ZooKeeper的集群,伪集群,单机的搭建
一:ZooKeeper服务安装包下载 第一步:打开zooKeeper官网
- Largest Number——STL的深层理解
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...