[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) 正解不会...总之最后化出来的 ...
随机推荐
- 【BZOJ】1455 罗马游戏
[算法]可并堆(左偏树) #include<cstdio> #include<algorithm> using namespace std; ; int l[maxn],r[m ...
- 2009 Round2 A Crazy Rows (模拟)
Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...
- hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...
- 模型验证与模型集成(Ensemble)
作者:吴晓军 原文:https://zhuanlan.zhihu.com/p/27424282 模型验证(Validation) 在Test Data的标签未知的情况下,我们需要自己构造测试数据来验证 ...
- RemoteBox 1.6 发布,VirtualBox 管理工具
RemoteBox 1.6 发布,VirtualBox 管理工具 http://www.lupaworld.com/article-230113-1.html 安装VirtualBox Extensi ...
- WA时查错点
这篇文章旨在总结可能出错的原因,想到时随时会补充. 查看调试输出语句是否删除 查看数组是否清零 查看是否使用long long 查看是否有的常量应开LL(如1LL << (32) ) 查看 ...
- 微信小程序实现图片上传,预览,删除
wxml: <view class='imgBox'> <image class='imgList' wx:for="{{imgs}}" wx:for-item= ...
- echo常用操作
echo -n 不换行输出 [root@C ~]# echo -n "peter" ; echo "linux" peterlinux echo -e 输出转义 ...
- 用JavaScript校验日期的合法性
校验表单时可能会遇到校验日期是否正确.可以利用JS的内置对象Date帮助我们完成日期校验. 思路是首先用被校验日期(假设为A,可能为字符串或数字)创建一个Date对象(假设为B). 然后判断A和B的年 ...
- SQL 列 转换成 查询出来的 行
查询 每个学生 的 (姓名,语文,数学,英语,成绩)为列 表结构如下: student: 学生表 grade 成绩表 : 查询出如下效果: SQL如下: select s.name,a.* fro ...