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 ...
随机推荐
- jsp传递参数的四种方法
1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...
- unity3d 计时功能舒爽解决方案
上次也写了一篇计时功能的博客 今天这篇文章和上次的文章实现思路不一样,结果一样 上篇文章地址:http://www.cnblogs.com/shenggege/p/4251123.html 思路决定一 ...
- 用Anko和Kotlin实现Android上的对话框和警告提示(KAD 24)
作者:Antonio Leiva 时间:Mar 9, 2017 原文链接:https://antonioleiva.com/dialogs-android-anko-kotlin/ 借助Builder ...
- 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
此篇文章是对上一篇文章(http://www.ifiero.com/index.php/archives/611)的进一步补充,主要说明如何适配Apple的最新三款手机iPhoneXs.iPhoneX ...
- 【WXS全局对象】Number
属性: 名称 说明 Number.MAX_VALUE 返回JS中可表示的最大的数.它的近似值为 1.7976931348623157 x 10308. Number.MIN_VALUE 返回JS中可表 ...
- 垃圾收集器与内存分配策略(深入理解Java虚拟机)
3.1 概述 垃圾收集器要解决哪些问题? 哪些内存需要回收 什么时候回收 如何回收 引用计数算法:当有一个地方引用,+1,引用失效,-1. 缺点:对象之间相互循环引用的问题. 可达性分析算法: ...
- python中的迭代器与生成器
迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...
- JSP页面无法使用EL导致"java.sql.SQLException: No suitable driver found for ${snapshot}"的问题
使用JSTL来连接mysql,这个问题折腾了半天,老以为是Mysql驱动的问题,还好最后偶然发现了是EL表达式识别不了,报错: javax.servlet.ServletException: java ...
- Java进阶知识点:服务端高并发的基石 - NIO与Reactor AIO与Proactor
一.背景 要提升服务器的并发处理能力,通常有两大方向的思路. 1.系统架构层面.比如负载均衡.多级缓存.单元化部署等等. 2.单节点优化层面.比如修复代码级别的性能Bug.JVM参数调优.IO优化等等 ...
- 【第五章】MySQL数据库的安全机制
MySQL权限表MySQL用户管理MySQL权限管理SSL加密连接