【题目链接】

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. 【动态规划】流水作业调度问题与Johnson法则

    1.问题描述:     n个作业{1,2,…,n}要在由2台机器M1和M2组成的流水线上完成加工.每个作业加工的顺序都是先在M1上加工,然后在M2上加工.M1和M2加工作业i所需的时间分别为ai和bi ...

  2. Java 反编译工具几枚(class转java)

    1.Java Decompiler Yet another fast Java decompiler. 下载地址:http://jd.benow.ca/#jd-gui-download 一款非常简洁的 ...

  3. HTML5 编辑 API 之 Range 对象(二)

    1.Range.cloneContents()The Range.cloneContents() returns a DocumentFragment copying the objects of t ...

  4. 枚举桌面应用程序lnk路径并得到对应程序的绝对路径(使用SHGetDesktopFolder函数枚举,然后使用COM查询IShellFolder,IShellLink和IPersistFile接口)

    // 枚举桌面上应用程序的lnk路径 void EnumDesktopLnkPath() { WCHAR szPath[MAX_PATH] = {0}; SHGetSpecialFolderPathW ...

  5. epmap服务

    epmap是 tcp 135和udp135端口135端口的“epmap - DCE endpoint resolution”记录可以通过连接到135端口和做适当的查询列举出来. 135端口大多数情况下 ...

  6. Android The content of the adapter has changed but ListView did not receive a notification

    The content of the adapter has changed but ListView did not receive a notification. Make sure the co ...

  7. MyEclipse 2013 开发WebService

    1.在Package Explorer窗口右键File新建WebService Project项目,我的名称为:TestWebService 2.WebService Framework选择JAX-W ...

  8. 04-语言入门-04-Fibonacci数

    地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=13    描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibona ...

  9. Oracle过程包加密

    Oracle加绕功能可以将PL/SQL代码实现部分隐藏,如存储过程.函数.包体等均可使用加绕功能,下面以一个存储过程实现部分加绕来展示Oracle加绕功能的使用.  加绕方法一: 1.编写如下存储过程 ...

  10. Nlog Layout

    Nlog.config <targets>     <target type="Console" name="trace" layout=&q ...