链接:https://www.nowcoder.com/acm/contest/141/H
来源:牛客网

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,n里面选一对数,满足 这两个式子的数都是素数,不同顺序也算是另一对

思路:我们会发现i,j都是素数的话,那么最大公约数为1,那么肯定是一对,然后我们再想想(6,10),(6,9)...都是
那么他们有什么规律呢,就是我们要使除了两个数的最大公约数之后都是素数,那么说明两个数分解之后就应该是 a=(素数)x*n b=(素数)y*n
n是最大公约数那么其他的满足这个条件对数其实就是一个素数对,同时乘以一个数那么也是,例如(2,3)那么(4,6)(6,9)(8,12)都是
满足条件的数,那么我们应该怎么计算呢,下面我们讲个例子 首先想10以内有几个3的倍数呢,10/3=3个,这是常识
那么我们就来计算,由所有的素数对扩展
10以内的所有对,因为我们首先应该找出素数对,所以我们应该是遍历所有的素数,
第一个 2 :10/2=5,10以内有5个2的倍数,我们再看2的前面有没有素数,没有,不计算
第二个 3 :10/3=3 ....3 6 9,前面有素数2,我们就可以找到素数2组成(2,3),然后两个数同时乘以2,3,因为前面的
小,所以我们始终能在6 9 前面找到4 6组成(4,6)(6,9)
第三个:5...
第四个:7...
 下面看代码实现
#include<bits/stdc++.h>
#define fi first
#define ll long long
#define pll pair<int,int>
#define se second
#define mod 1000000007
using namespace std;
const int maxn = ;
bool isPrime[maxn];
ll prime[maxn];
ll sum[maxn];
ll add[maxn];
ll total=;
map< pll ,int> mp;
void makePrime2()//筛法找出所有的素数
{
memset(isPrime,true,sizeof(isPrime));
memset(prime,,sizeof(prime));
sum[]=;
for(int i=; i<maxn; i++)
{
if(isPrime[i])
{
prime[total++]=i;
sum[i]=sum[i-]+;//用于存当前位置有多少个素数
}
else sum[i]=sum[i-];
for(int j=; j<total && i*prime[j]<maxn; j++)
{
isPrime[i*prime[j]]=false;
if(i%prime[j]==) break;
}
}
}
int main()
{
makePrime2();
ll n;
scanf("%lld",&n);
ll ans=;
for(int i=; i<total&&prime[i]<=n; i++)
{
int p=n/prime[i];//找出n以内有多少个素数prime[i]的倍数
ans+=(sum[prime[i]]-)*p;//-1因为本身这个素数不算,然后和前面的素数进行匹配与扩展
}
printf("%lld\n",ans*);
}

牛客第三场多校 H Diff-prime Pairs的更多相关文章

  1. 牛客第三场多校 E Sort String

    链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...

  2. PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)

    题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...

  3. uestc summer training #9 牛客第三场 BFS计数

    G.coloring tree BFS计数 题目:给你n(<=5000)个节点的一颗树 你有K(<=5000)种颜色 你可以给每一个节点染一种颜色 总共有Kn种染色方法 在一种染色方法中 ...

  4. 牛客第五场多校 J plan 思维

    链接:https://www.nowcoder.com/acm/contest/143/J来源:牛客网 There are n students going to travel. And hotel ...

  5. 2019牛客第八场多校 E_Explorer 可撤销并查集(栈)+线段树

    目录 题意: 分析: @(2019牛客暑期多校训练营(第八场)E_Explorer) 题意: 链接 题目类似:CF366D,Gym101652T 本题给你\(n(100000)\)个点\(m(1000 ...

  6. 牛客第五场多校 A gpa 分数规划(模板)

    链接:https://www.nowcoder.com/acm/contest/143/A来源:牛客网 Kanade selected n courses in the university. The ...

  7. Shuffle Cards(牛客第三场+splay)

    题目: 题意:将1~n的数进行m次操作,每次操作将第pi位到pi+si-1位的数字移到第一位,求最后的排列. 思路:现在还没不会写splay,在知道这是splay模板题后找了一波别人的模板,虽然过了, ...

  8. 牛客第三场 J LRU management

    起初看到这道题的时候,草草就放过去了,开了另一道题,结果开题不顺利,总是感觉差一点就可以做出来,以至于一直到最后都没能看这道题qaq 题意:类似于操作系统上讲的LRU算法,有两个操作,0操作代表访问其 ...

  9. 最长相同01数的子串(map搞搞)--牛客第三场 -- Crazy Binary String

    题意: 如题. 或者用我的数组分治也可以,就是有点愚蠢. //#include <bits/stdc++.h> #include <map> #include <iost ...

随机推荐

  1. java测试感想

    package ATM; public class Account { private String accountID; private String accountname; private St ...

  2. 分离vue组件内部css

    当我们使用vue组件的时候,使用webpack打包的时候,默认会把vue组件内部的css打包到页面上,但是打包到页面上很丑陋,所以我们希望可以把vue组件内部的css抽离到css文件中,使用vue-s ...

  3. CentOS7 安装PHP7的redis扩展:

      phpredis-4.2.0.tar.gz:下载:wget https://github.com/phpredis/phpredis/archive/4.2.0.tar.gz   $ tar -z ...

  4. 删除gitlab 上的文件

  5. shiro中记住我功能

    Shiro提供了记住我(RememberMe)的功能,比如访问如淘宝等一些网站时,关闭了浏览器下次再打开时还是能记住你是谁,下次访问时无需再登录即可访问,基本流程如下: 1.首先在登录页面选中Reme ...

  6. shiro中拦截器机制

    8.1 拦截器介绍 Shiro使用了与Servlet一样的Filter接口进行扩展:所以如果对Filter不熟悉可以参考<Servlet3.1规范>http://www.iteye.com ...

  7. centos7 --kubeadm安装

    One or more machines running one of: Ubuntu 16.04+ Debian 9 CentOS 7 RHEL 7 Fedora 25/26 (best-effor ...

  8. 纯css实现顶部进度条随滚动条滚动

    <!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv=" ...

  9. gleez框架获得时间控件

    1,首先你要在你的页面上引用一个js <script src="<?php echo URL::base() ?>media/vendor/datepicker/Wdate ...

  10. UI基础三:简单的BOL报表开发

    巧了...刚好一个需求,就直接来撸起来吧. 需要做一个报表: 1.创建查询结构和结果结构 2.创建实施类: SE24创建ZCL_JPEXPORT_ORDER_IL 更改父类:CL_WCF_GENIL_ ...