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——题解的更多相关文章

  1. 51nod 1594 Gcd and Phi 反演

    OTZ 又被吊打了...我当初学的都去哪了??? 思路:反演套路? 提交:\(1\)次 题解: 求\(\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(gcd(\varphi(i ...

  2. 51nod 1594 Gcd and Phi(莫比乌斯反演)

    题目链接 传送门 思路 如果这题是这样的: \[ F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\phi(gcd(i,j)) \] 那么我们可能会想到下 ...

  3. 【51nod】1594 Gcd and Phi

    题解 跟随小迪学姐的步伐,学习一下数论 小迪学姐太巨了! 这道题的式子很好推嘛 \(\sum_{i = 1}^{n} \sum_{j = 1}^{n} \sum_{d|\phi(i),\phi(j)} ...

  4. 51NOD 2026:Gcd and Lcm——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026 参考及推导:https://www.cnblogs.com/ivo ...

  5. 51NOD 1227:平均最小公倍数——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1227 懒得打公式了,看这位的吧:https://blog.csdn.ne ...

  6. 【莫比乌斯反演】51nod1594 Gcd and Phi

    题解 显然可以O(nlogn)计算 代码 //by 减维 #include<set> #include<map> #include<queue> #include& ...

  7. 51nod 最近刷题 简要题解

    51nod 1564 由于数据是随机的,可以证明,对于每一个数,向左或右找比它小的数,长度是logn级别的 考虑枚举最大值 注意,对于每一个最大值,如果直接用2个循环枚举左右端点的话,理论是lognl ...

  8. 51nod 1575 Gcd and Lcm

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1575 万年巨坑终于填掉了…… 首先是煞笔西瓜的做题历程O_O. ...

  9. 【反演复习计划】【51nod1594】Gcd and Phi

    现在感觉反演好多都是套路QAQ…… #include<bits/stdc++.h> using namespace std; ; typedef long long ll; int n,c ...

随机推荐

  1. SpringBoot入门(一)——开箱即用

    本文来自网易云社区 Spring Boot是什么 从根本上来讲Spring Boot就是一些库的集合,是一个基于"约定优于配置"的原则,快速搭建应用的框架.本质上依然Spring, ...

  2. Ubuntu Server 下将HTML页面转换为PNG图片

    零.前言 最近做一个网站,需要将网页转换为图片.由于服务器是Ubuntu Server,没有图形界面,所以实现的过程中遇到了很多问题.记录下来备用. 一.安装CutyCapt CutyCapt是一个可 ...

  3. TPO-10 C2 Return a literature book

    TPO-10 C2 Return a literature book 第 1 段 1.Listen to a conversation between a student and an employe ...

  4. JS实现对数组的去重

    JS实现对数组的去重 $scope.validateContect = function(text) { var arr = text; // 若传入的数据为string类型,用逗号分隔 if((ty ...

  5. spring boot 报错 Error creating bean with name

    Application 启动类 要和父目录平级

  6. 【halcon】算子

    算子 rgb1_to_gray  灰度化 threshold:英文是阈的意思    二值化算子 Connection Compute connected components of a region. ...

  7. 文件操作---基于python

    # coding:utf-8from time import sleepimport sysreload(sys)sys.setdefaultencoding("utf8")f=o ...

  8. Sum of Consecutive Prime Numbers(素数打表+尺取)

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  9. c#非界面线程控制控件

    private delegate void FlushCilent(); Invoke(new FlushCilent(databaseConnect));

  10. Calculator PartⅢ

    GitHub/object-oriented The title of the work 这次敲代码耗时相对较短,但是始终无法完成debug步骤,目前上传的代码可以通过编译,但运行即报停,问题调试为内 ...