51NOD 1594:Gcd and Phi——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1594
参考及详细推导:http://www.cnblogs.com/rir1715/p/8584083.html
设\(cnt_i=\sum_{j=1}^n[\phi(j)==i]\),这个可以在\(O(n)\)处理出来。
我们用它把\(\phi(i)\phi(j)\)换元得:
\(\sum_{i=1}^n\sum_{j=1}^n\phi(gcd(i,j))\times cnt_i\times cnt_j\)
\(=\sum_{d=1}^n\phi(d)\sum_{i=1}^n\sum_{j=1}^n[gcd(i,j)==d]\times cnt_i\times cnt_j\)
好的最难的部分已经过去了,随着一阵套路,这个式子索然无味。
\(=\sum_{d=1}^n\phi(d)\sum_{d'=1}^n\mu(d')(\sum_{i=1}^{\frac{n}{dd^`}}cnt_{idd'})^2\)
令\(sum(t)=\sum_{i=1}^{\frac{n}{t}}cnt_{it}\),这个可以在\(O(nlogn)\)处理出来(调和级数)。
式子
\(=\sum_{d=1}^n\phi(d)\sum_{d'=1}^n\mu(d')sum(dd')^2\)
然后我们发现当\(dd'>n\)时\(sum(dd')=0\),所以我们\(d'\)不需要枚举那么多,所以最终的式子为:
\(\sum_{d=1}^n\phi(d)\sum_{d'=1}^{\frac{n}{d}}\mu(d')sum(dd')^2\)
这个式子可以在\(O(nlogn)\)运算(调和级数)。
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2e6+5;
inline int read(){
int X=0,w=0;char ch=0;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
bool he[N];
int su[N],tot;
ll phi[N],mu[N],cnt[N],sum[N];
void Euler(int n){
phi[1]=mu[1]=1;
for(int i=2;i<=n;i++){
if(!he[i]){
su[++tot]=i;
phi[i]=i-1;mu[i]=-1;
}
for(int j=1;j<=tot&&i*su[j]<=n;j++){
int p=su[j];he[i*p]=1;
if(i%p==0){
mu[i*p]=0;phi[i*p]=phi[i]*p;
break;
}else{
mu[i*p]=mu[i]*mu[p];phi[i*p]=phi[i]*phi[p];
}
}
}
}
int main(){
Euler(N-5);
int t=read();
while(t--){
memset(cnt,0,sizeof(cnt));
memset(sum,0,sizeof(sum));
int n=read();
for(int i=1;i<=n;i++)cnt[phi[i]]++;
for(int i=1;i<=n;i++)
for(int j=1;j<=n/i;j++)
sum[i]+=cnt[i*j];
for(int i=1;i<=n;i++)sum[i]*=sum[i];
ll ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n/i;j++)
ans+=phi[i]*mu[j]*sum[i*j];
printf("%lld\n",ans);
}
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +
+++++++++++++++++++++++++++++++++++++++++++
51NOD 1594:Gcd and Phi——题解的更多相关文章
- 51nod 1594 Gcd and Phi 反演
OTZ 又被吊打了...我当初学的都去哪了??? 思路:反演套路? 提交:\(1\)次 题解: 求\(\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(gcd(\varphi(i ...
- 51nod 1594 Gcd and Phi(莫比乌斯反演)
题目链接 传送门 思路 如果这题是这样的: \[ F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\phi(gcd(i,j)) \] 那么我们可能会想到下 ...
- 【51nod】1594 Gcd and Phi
题解 跟随小迪学姐的步伐,学习一下数论 小迪学姐太巨了! 这道题的式子很好推嘛 \(\sum_{i = 1}^{n} \sum_{j = 1}^{n} \sum_{d|\phi(i),\phi(j)} ...
- 51NOD 2026:Gcd and Lcm——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026 参考及推导:https://www.cnblogs.com/ivo ...
- 51NOD 1227:平均最小公倍数——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1227 懒得打公式了,看这位的吧:https://blog.csdn.ne ...
- 【莫比乌斯反演】51nod1594 Gcd and Phi
题解 显然可以O(nlogn)计算 代码 //by 减维 #include<set> #include<map> #include<queue> #include& ...
- 51nod 最近刷题 简要题解
51nod 1564 由于数据是随机的,可以证明,对于每一个数,向左或右找比它小的数,长度是logn级别的 考虑枚举最大值 注意,对于每一个最大值,如果直接用2个循环枚举左右端点的话,理论是lognl ...
- 51nod 1575 Gcd and Lcm
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1575 万年巨坑终于填掉了…… 首先是煞笔西瓜的做题历程O_O. ...
- 【反演复习计划】【51nod1594】Gcd and Phi
现在感觉反演好多都是套路QAQ…… #include<bits/stdc++.h> using namespace std; ; typedef long long ll; int n,c ...
随机推荐
- KubeCon深度洞察 | KubeEdge开源首秀
以下内容根据华为云DJ在KubeCon Shanghai Demo Session演讲实录整理而成. KubeEdge Demo Show 11月15日上午Huawei宣布了KubeEdge项目开源, ...
- WEB页面常用基本控件测试用例
一.树控件的测试外观操作 1)项目中的所有树是否风格一致 2)树结构的默认状态是怎样的.比如默认树是否是展开,是展开几级? 是否有默认的焦点? 默认值是什么?展开的节点图标和颜色? 2.执行操作 1 ...
- Objective-C NSString基本使用 类方法 self关键字
NSString基本使用 #import <Foundation/Foundation.h> int main() { //最简单的创建字符串的方式 NSString *str = @&q ...
- javac 编译过程
javac 编译过程 一.解析与填充符号表: 1. 语法.此法分析: a) 语法分析:将源代码字符流转换为标记(Token:编译过程最小元素)集合. b) ...
- [JSON].value( keyPath )
语法:[JSON].value( keyPath ) 返回:[String | Null] 说明:获取指定键名路径值的字符串格式 示例: Set jsonObj = toJson("{bod ...
- [JSON].exists( keyPath )
语法:[JSON].exists( keyPath ) 返回:[True | False] 说明:检测指定键名路径是否存在 示例: Set jsonObj = toJson("{div:{' ...
- centos端口管理
centos 6.5 ###############配置filter表防火墙############### #清除预设表filter中的所有规则链的规则iptables -F #清除预设表filter ...
- 从零开始的Python学习Episode 6——字符串操作
字符串操作 一.输出重复字符串 print('smile'*6) #输出6个smile 二.通过引索输出部分字符串 print('smile'[1:]) print('smile'[1:3]) #输出 ...
- Matlab 图象操作函数讲解
h = imrect;pos = getPosition(h); 这个函数用来获取图象上特定区域的坐标,其中pos的返回值中有四个参数[xmin,ymin,width,height],特定区域的左上角 ...
- POJ 1655 Balancing Act(求树的重心)
Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...