题目链接:https://www.nowcoder.com/acm/contest/141/H

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Eddy has solved lots of problem involving calculating the number of coprime pairs within some range. This problem can be solved with inclusion-exclusion method. Eddy has implemented it lots of times. Someday, when he encounters another coprime pairs problem, he comes up with diff-prime pairs problem. diff-prime pairs problem is that given N, you need to find the number of pairs (i, j), where and are both prime and i ,j ≤ N. gcd(i, j) is the greatest common divisor of i and j. Prime is an integer greater than 1 and has only 2 positive divisors.

Eddy tried to solve it with inclusion-exclusion method but failed. Please help Eddy to solve this problem.

Note that pair (i1, j1) and pair (i2, j2) are considered different if i1 ≠ i2 or j1 ≠ j2.

输入描述:

Input has only one line containing a positive integer N.

1 ≤ N ≤ 10^7

输出描述:

Output one line containing a non-negative integer indicating the number of diff-prime pairs (i,j) where i, j ≤ N

输入例子:
3
输出例子:
2

-->

示例1

输入

3

输出

2
示例2

输入

5

输出

6

题意:

给出一个数字 n (1 ≤ n ≤ 1e7),求多少 数对(i, j) 满足 $\frac{i}{{\gcd \left( {i,j} \right)}}$ 和 $\frac{j}{{\gcd \left( {i,j} \right)}}$ 均为质数,且1 ≤ i, j ≤ n。

题解:

筛出[1,n]之间所有的素数,

不难知道,每次取到其中两个素数组成一个素数对(x, y),不妨设 x < y,那么相应的就增加了 $2 \times \left\lfloor {n/y} \right\rfloor $ 个数对;

例如,n=7,取到素数对(2,3),那么 $\left\lfloor {n/3} \right\rfloor = \left\lfloor {7/3} \right\rfloor = 2$,就有 $2 \times 2 = 4$ 个数对:(1*2,1*3) = (2,3)、(3,2)、(2*2,2*3) = (4,6)、(6,4);

对欧拉筛法稍加改造,添加一行代码即可。时间复杂度O(n)。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e7+; int n;
ll ans; bool isPrime[maxn];
int prime[maxn/],cnt;
void screen()//欧拉筛法求素数
{
cnt=;
memset(isPrime,,sizeof(isPrime));
isPrime[]=isPrime[]=;
for(int i=;i<=n;i++)
{
if(isPrime[i])
{
prime[cnt++]=i; ans+=*(n/i)*(cnt-);
//每找到一个素数i,其就可以与前面所有出现过的cnt-1个素数组成cnt-1个素数对,相应的就有2*(n/i)*(cnt-1)个数对 }
for(int j=;j<cnt;j++)
{
if(i*prime[j]>n) break;
isPrime[(i*prime[j])]=;
if(i%prime[j]==) break;
}
}
} int main()
{
scanf("%d",&n);
ans=;
screen();
cout<<ans<<endl;
}

2018牛客网暑期ACM多校训练营(第三场) H - Diff-prime Pairs - [欧拉筛法求素数]的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  3. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  4. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  5. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  6. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  7. 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]

    题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...

  8. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  9. 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述  Given a sequence of integers a1, a2, ..., an a ...

  10. 2018牛客网暑期ACM多校训练营(第九场)A -Circulant Matrix(FWT)

    分析 大佬说看样例就像和卷积有关. 把题目化简成a*x=b,这是个xor的FWT. FWT的讲解请看:https://www.cnblogs.com/cjyyb/p/9065615.html 那么要求 ...

随机推荐

  1. .NET Framework 4.0源代码

    原文出处:http://blogs.microsoft.co.il/blogs/arik/archive/2010/07/12/step-into-net-framework-4-0-source-c ...

  2. 安装centos6.5

    我是在虚拟机上面安装的centos6.5,本机的系统是windows 2008 datacenter. 到这个地方下载镜像文件:http://mirrors.sohu.com/centos/6.5/i ...

  3. zabbix添加对tomcat线程池的监控

    在zabbix模板中添加以下监控项: 可以参考文档:http://www.fblinux.com/?p=616

  4. 【AI】Ubuntu NVIDIA CUDA CUDNN安装配置

    https://blog.csdn.net/qq_33200967/article/details/80689543 https://blog.csdn.net/sinat_29963957/arti ...

  5. [Command] lrzsz - 文件传输工具包

    lrzsz 是一个支持 XMODEM.YMODEM.ZMODEM 文件传输协议的 Unix 程序包.它是 Omen Technologies 公司所有的 rzsz 程序包的公开发行增强版,遵守 GNU ...

  6. gradle 两种更新方法

    第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper.properties 替换 distributionUrl= ...

  7. javascript的实现事件的一些实例

    嘿嘿,今天学习到了事件,其实在C#中事件只需要我们触发即可实现,但是在javascript并不是这样的,在这里,事件是javascript与html的交互,就是文档或者浏览器窗口发生的一件特定的交互瞬 ...

  8. Python学习(20):Python函数(4):关于函数式编程的内建函数

    转自http://www.cnblogs.com/BeginMan/p/3178103.html 一.关于函数式编程的内建函数 apply()逐渐被舍弃,这里不讨论 1.filter() #filte ...

  9. 【十大算法实现之naive bayes】朴素贝叶斯算法之文本分类算法的理解与实现

    关于bayes的基础知识,请参考: 基于朴素贝叶斯分类器的文本聚类算法 (上) http://www.cnblogs.com/phinecos/archive/2008/10/21/1315948.h ...

  10. $.format,jquery.format 使用说明

    为jquery添加 format  功能 $.format = function (source, params) { if (arguments.length == 1) return functi ...