题目链接: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. [Module] 06 - DataBinding and MVVM

    下一步学习列表: Android DataBinding使用总结(一) *** Android DataBinding使用总结(二) Android DataBinding使用总结(三)列表展示 An ...

  2. nuget类库xml说明以及类库说明文件添加到包中

    1.nuget包制作添加xml操作:项目右键属性,生成配置输出xml文档文件,debug,release都配置一下,项目右键 yesway.redis.csproj 文件增加: 添加类库说明文件con ...

  3. Centos6.3 下使用 Tomcat-6.0.43 非root用户 jsvc模式部署 生产环境 端口80 vsftp

    一.安装JDK环境 方法一. 官方下载链接 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260 ...

  4. Python easyGUI 猜数字

    import easygui as g import random d=random.randint(0,10) while 1: g.msgbox("现在开始猜数字小游戏:") ...

  5. python virtualenv安装说明

    环境说明: virtualenv安装: 第一步,创建目录code: 第二步,创建一个独立的Python运行环境,命名为venv: 新建的Python环境被放到当前目录下的venv目录. 第三步,有了v ...

  6. mybatis 之 resultType="Integer"

    public class EcPromoteRuleAdditionalNew extends BaseBO { private String[] promoteRuleIds; public Str ...

  7. React Native(十)——TextInput一点小结

    11.24(后续的道路会更加漫长,一点一点总结上去吧~): 从昨天开始接触Mac,实在让自己有点“奔溃”的赶脚……老大说,“不要紧,多接触接触就好了.” 于是,我就开始了跟Mac死磕到底的准备……就先 ...

  8. vmp3.0.9全保护拆分解析

    https://mp.weixin.qq.com/s/WO6w_L-cYwH5KB2rilZdag 以下为了避免插件干扰,故采用x64dbg原版进行分析. 首先我通过检测到调试器的弹窗进行栈回溯,定位 ...

  9. opencv3——ANN算法的使用

    最近刚转用opencv3,使用ANN算法时遇到了一些问题,记录下来. 训练神经网络的代码如下: //创建ANN Ptr<ANN_MLP> bp = ANN_MLP::create(); 设 ...

  10. linux计划任务之crontab

    语法:        crontab [ -u user ] file        crontab [ -u user ] [ -i ] { -e | -l | -r } 说明: crontab命令 ...