【题目链接】

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. Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)

    1. 2. 3. 4. 5. 6. 7.

  2. Java类的加载、链接和初始化

    一.Java的类加载机制回顾与总结: 我们知道一个Java类要想运行,必须由jvm将其装载到内存中才能运行,装载的目的就是把Java字节代码转换成JVM中的java.lang.Class类的对象.这样 ...

  3. 255. Verify Preorder Sequence in Binary Search Tree

    题目: Given an array of numbers, verify whether it is the correct preorder traversal sequence of a bin ...

  4. Javascript如何判断一个变量是数字类型?

    isNaN()不能判断一个变量是否为数字类型,isNaN(123)值为false,isNaN('123')值也为false.isNaN() 的实际作用跟它的名字isNaN并不一致,isNaN(NaN) ...

  5. navigationbar的一些设置记录

    1.设置navigationbar背景颜色 [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]]; PS:如果颜色不对 ...

  6. uva 111 - History Grading (dp, LCS)

    题目链接 题意:给N,第二行是答案,n个数c1---cn, 代表第一个的顺序是c1,第二个数顺序是c2; 下面每一行是学生的答案,格式同上. 注意:这个给的顺序需要处理一下,不能直接用. 思路:LCS ...

  7. ASCII码常用值

    大写字母 A~Z 65~90 小写字母a~z 97~122 数字0~9 48~ 57

  8. [反汇编练习] 160个CrackMe之005

    [反汇编练习] 160个CrackMe之005. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  9. 04day1

    无穷的数列 找规律 [问题描述] 有一个无穷序列如下: 110100100010000100000- 请你找出这个无穷序列中指定位置上的数字. [输入] 第一行一个正整数 N,表示询问次数:接下来的 ...

  10. 【C#学习笔记】LinkedList容器使用

    using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { stati ...