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 ...
随机推荐
- linux下实现ssh无密码登录访问
在192.168.9.51机器上 1)运行:#ssh-keygen -t rsa 2)然后拍两下回车(均选择默认) 3)运行: #ssh-copy-id -i /root/.ssh/id_rsa.pu ...
- pygame安装【在pycharm的IDE project下】
pygame安装[在pycharm的IDE project下] 首先更新电脑的pip.exe[命令行下面] 然后进入IDE project ——>setting 中查找是否安装pygame 我的 ...
- SQL注入篇二------利用burp盲注,post注入,http头注入,利用burpsuit找注入点,宽字节注入
1.布尔盲注burpsuit的使用 先自己构造好注入语句,利用burpsuit抓包,设置变量,查出想要的信息. 比如----查数据库名的ascii码得到数据库构造好语句 http://123.206. ...
- RabbitMQ基础教程之使用进阶篇
RabbitMQ基础教程之使用进阶篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 I. 背景 前一篇基本使用篇 ...
- mysql5.6 无法远程连接问题解决
需要配置mysql5.6版本的my.cnf文件,我的my.cnf文件配置如下: port=3306是我后来自己加上的.加上这个之后重启mysql service mysqld restart 记得给r ...
- TPO-11 C2 Work for the biology committee
committee 委员会 representative 代表 department secretary 系里的秘书 applicant 申请人 TPO-11 C2 Work for the biol ...
- Unity热更新文件的服务器部署(IIS)
1.VS新建一个"ASP.NET空网站" 工程结构如下 最好设置.Net FrameWork版本为 V4.0或者V4.5版本的,因为我们的程序最后是要部署到阿里云的虚拟服务器上的, ...
- 前端开发工程师 - 02.JavaScript程序设计 - 第2章.进阶篇
第2章--进阶篇 类型进阶 类型: Undefined Null Boolean String Number Object 原始类型(值类型):undefined, null, true, " ...
- 了解Python控制流语句——continue 语句
continue 语句用以告诉 Python 跳过当前循环块中的剩余语句,并继续该循环的下一次迭代. 案例(保存为 continue.py): while True: s = input('Enter ...
- DeepLearning - Forard & Backward Propogation
In the previous post I go through basic 1-layer Neural Network with sigmoid activation function, inc ...