【题目链接】

http://www.lydsy.com/JudgeOnline/problem.php?id=2818

【题意】

  问(x,y)为质数的有序点对的数目。

【思路一】

  定义f[i]表示i之前(x,y)=1的有序点对的数目,则有递推式:

  f[1]=1

  f[i]=f[i-1]+phi[i]*2

  我们依次枚举小于n的所有素数,对于素数t,(x,y)=t的数目等于(x/t,y/t),即f[n/t]。

【代码一】

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; typedef long long ll;
const int N = 1e7+; int su[N],tot,phi[N];
ll f[N]; void get_pre(int n)
{
phi[]=;
for(int i=;i<=n;i++) if(!phi[i]) {
su[++tot]=i;
for(int j=i;j<=n;j+=i) {
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
f[]=;
for(int i=;i<=n;i++) f[i]=f[i-]+*phi[i];
} int n; int main()
{
scanf("%d",&n);
get_pre(n);
ll ans=;
for(int i=;i<=tot;i++)
ans+=f[n/su[i]];
printf("%lld\n",ans);
return ;
}

【思路二】

  其它思路一样,不同的是使用莫比乌斯反演计算(x,y)=1的数目,累计答案的时间复杂度为O(n sqrt(n))

  推倒过程:

【代码二】

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; typedef long long ll;
const int N = 1e7+; int su[N],mu[N],tot,vis[N]; void get_mu(int n)
{
mu[]=;
for(int i=;i<=n;i++) {
if(!vis[i]) {
su[++tot]=i;
mu[i]=-;
}
for(int j=;j<=tot&&su[j]*i<=n;j++) {
vis[i*su[j]]=;
if(i%su[j]==) mu[i*su[j]]=;
else mu[i*su[j]]=-mu[i];
}
}
for(int i=;i<=n;i++) mu[i]+=mu[i-];
} int n; ll calc(int n)
{
int i,last; ll ans=;
for(int i=;i<=n;i=last+) {
last=n/(n/i);
ans+=(ll)(n/i)*(n/i)*(mu[last]-mu[i-]);
}
return ans;
} int main()
{
// freopen("in.in","r",stdin);
// freopen("out.out","w",stdout);
scanf("%d",&n);
get_mu(n);
ll ans=;
for(int i=;i<=tot;i++)
ans+=calc(n/su[i]);
printf("%lld\n",ans);
return ;
}

UPD.16/4/8

  另外莫比乌斯反演还有一种O(n)预处理O(sqrt(n))查询的做法 click Here

bzoj 2818 Gcd(欧拉函数 | 莫比乌斯反演)的更多相关文章

  1. ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)

    Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...

  2. 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)

    P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...

  3. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  4. BZOJ 2818 Gcd(欧拉函数+质数筛选)

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 9108  Solved: 4066 [Submit][Status][Discu ...

  5. BZOJ 2818 GCD 【欧拉函数 || 莫比乌斯反演】

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 2818: Gcd Time Limit: 10 Sec  Memory Limit ...

  6. UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)

    UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...

  7. 51nod 1237 最大公约数之和 V3【欧拉函数||莫比乌斯反演+杜教筛】

    用mu写lcm那道卡常卡成狗(然而最后也没卡过去,于是写一下gcd冷静一下 首先推一下式子 \[ \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j) \] \[ \sum_{i= ...

  8. 中国剩余定理 & 欧拉函数 & 莫比乌斯反演 & 狄利克雷卷积 & 杜教筛

    ssplaysecond的博客(请使用VPN访问): 中国剩余定理: https://ssplaysecond.blogspot.jp/2017/04/blog-post_6.html 欧拉函数: h ...

  9. hdu6390 /// 欧拉函数+莫比乌斯反演 筛inv[] phi[] mu[]

    题目大意: 给定m n p 求下式   题解:https://blog.csdn.net/codeswarrior/article/details/81700226 莫比乌斯讲解:https://ww ...

随机推荐

  1. go语言入门

    Go语言最主要的特性: 自动垃圾回收 更丰富的内置类型 函数多返回值 错误处理 匿名函数和闭包 类型和接口 并发编程 反射 语言交互性 1.2.4 错误处理Go语言引入了3个关键字用 ...

  2. ipconfig

    当使用ipconfig时不带任何参数选项,那么它为每个已经配置了的接口显示IP地址.子网掩码和缺省网关值. 如果你安装了虚拟机和无线网卡的话,它们的相关信息也会出现在这里.  

  3. go的优缺点

    1.1 不允许左花括号另起一行1.2 编译器莫名其妙地给行尾加上分号1.3 极度强调编译速度,不惜放弃本应提供的功能1.4 错误处理机制太原始1.5 垃圾回收器(GC)不完善.有重大缺陷1.6 禁止未 ...

  4. HDU5086——Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...

  5. 在CentOS 6.X 上面安装 Python 2.7.X

    在CentOS 6.X 上面安装 Python 2.7.X CentOS 6.X 自带的python版本是 2.6 , 由于工作需要,很多时候需要2.7版本.所以需要进行版本升级.由于一些系统工具和服 ...

  6. Android Handler 避免内存泄漏的用法总结

    Android开发经常会用到handler,但是我们发现每次使用Handler都会出现:This Handler class should be static or leaks might occur ...

  7. Android开发之定义app在手机的安装位置

    定义app在手机的安装位置,可以通过在清单文件中添加属性 android:installLocation="" 该属性有三个值:auto(自动),preferExternal(外部 ...

  8. BootStrap基本样式

    文本对齐风格:.text-left:左对齐.text-center:居中对齐.text-right:右对齐.text-justify:两端对齐 取消列表符号:.list-unstyled内联列表:.l ...

  9. iOS开发:mac使用svn管理项目

    记录mac下常用的svn命令: 1.检出项目: svn checkout .../svn/projectName --username=xxx --password=xxx //将ip换成svn服务器 ...

  10. 配置IIS服务器,APK文件下载

    解决方法:在IIS的MIME类型中添加扩展名.apk,MIME类型application/vnd.android即可