bzoj 2818 Gcd(欧拉函数 | 莫比乌斯反演)
【题目链接】
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(欧拉函数 | 莫比乌斯反演)的更多相关文章
- ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- BZOJ 2818 Gcd(欧拉函数+质数筛选)
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 9108 Solved: 4066 [Submit][Status][Discu ...
- BZOJ 2818 GCD 【欧拉函数 || 莫比乌斯反演】
传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 2818: Gcd Time Limit: 10 Sec Memory Limit ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- 51nod 1237 最大公约数之和 V3【欧拉函数||莫比乌斯反演+杜教筛】
用mu写lcm那道卡常卡成狗(然而最后也没卡过去,于是写一下gcd冷静一下 首先推一下式子 \[ \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j) \] \[ \sum_{i= ...
- 中国剩余定理 & 欧拉函数 & 莫比乌斯反演 & 狄利克雷卷积 & 杜教筛
ssplaysecond的博客(请使用VPN访问): 中国剩余定理: https://ssplaysecond.blogspot.jp/2017/04/blog-post_6.html 欧拉函数: h ...
- hdu6390 /// 欧拉函数+莫比乌斯反演 筛inv[] phi[] mu[]
题目大意: 给定m n p 求下式 题解:https://blog.csdn.net/codeswarrior/article/details/81700226 莫比乌斯讲解:https://ww ...
随机推荐
- 不重启使XP环境变量生效
不重启使XP环境变量生效 http://www.pkghost.cn/wz/sort0185/8874.html 在“我的电脑”->“属性”->“高级”->“环境变量”中增加或修改环 ...
- 在VS2012后的版本中做数据报表时,提示尚未指定报表“Report1”的报表定义
有一群的朋友在用VS2012做数据报表时,老是提示 本地报表处理期间出错. 尚未指定报表“Report1”的报表定义 未将对象引用设置到对象的实例. 我看了一下,步骤没错,我用VS2010做了一下,一 ...
- 223. Rectangle Area
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- Linux 查找软件安装路径
root@kali:~# whereis sqlmap sqlmap: /usr/bin/sqlmap /usr/share/sqlmap /usr/share/man/man1/sqlmap..gz ...
- Oracle Gateways透明网关访问SQL Server
自己的本机安装了Oracle 12c,公司的平台需要同时支持Oracle与SQL Server,很多时候都有将数据从Oracle同步到SQL Server的需求.通过SQL Server的link S ...
- Lunix中文乱码解决方案
sudo vi /var/lib/locales/supported.d/local#添加下面的中文字符集zh_CN.GBK GBKzh_CN.GB2312 GB2312zh_CN.GB18030 G ...
- ACM刷题常用链接
武汉科技大学 http://acm.wust.edu.cn/ 华中科技大学 http://acm.hust.edu.cn/vjudge/toIndex.action 杭州电子科技大学 http:/ ...
- Java关键字static、final使用小结
static 1. static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的 ...
- 学军NOI训练13 T3 白黑树
唉,大学军有自己的OJ就是好,无限orz 只有周六的比赛是开放的囧,这场比赛最后因为虚拟机卡住没有及时提交…… 否则就能让大家看到我有多弱了…… 前两题题解写的很详细,可以自己去看,我来随便扯扯T3好 ...
- ajax上传图片 jquery插件 jquery.form.js 的方法 ajaxSubmit; AjaxForm与AjaxSubmit的差异
先引入脚本 这里最好是把jquery的脚本升级到1.7 <script src="js/jquery-1.7.js" type="text/javascript& ...