链接: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. Zabbix安装(debian,centos)

    lnmp和lamp架构搭建一键安装脚本下载地址:https://lnmp.org/download.html  https://github.com/teddysun/lamp/tree/master ...

  2. Django admin 管理工具

    admin 组件的使用 Django 提供了基于 web 的管理工具.Django 自动管理工具是 django.contrib 的一部分. INSTALLED_APPS = [ 'django.co ...

  3. 4.1.7 Cutting Game(POJ 2311)

    Problem description: 两个人在玩如下游戏. 准备一张分成 w*h 的格子的长方形纸张,两人轮流切割纸张.要沿着格子的边界切割,水平或者垂直地将纸张切成两部分.切割了n次之后就得到了 ...

  4. Python 2.7.x 使用Requests发起https请求时报Warning的问题

    warning :如下 /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:852: ...

  5. leetcode-algorithms-17 Letter Combinations of a Phone Number

    leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...

  6. [luogu P3195] [HNOI2008]玩具装箱TOY

    [luogu P3195] [HNOI2008]玩具装箱TOY 题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆, ...

  7. Pick-up sticks

    Pick-up sticks Stan has n sticks of various length. He throws them one at a time on the floor in a r ...

  8. 如何在Linux中tomcat下运行一个web项目

    如何在Linux中tomcat下运行一个web项目 然后启动Tomcat项目.运行的运行后会自动将war包解压. 如果页面报404,那么请查看tomcat日志文件,它一定是报错了....

  9. Nop 4.1版本已经迁移到.net core2.1版本

    1. github 下载,4.1版本,运行, install时,会让你新增后台账户密码,sql服务器 2. 在Configuration 新增Language 3. 上传中文语言包 , 你也可以先导出 ...

  10. vue 中使用 Toast弹框

    import { ToastPlugin,ConfirmPlugin,AlertPlugin} from 'vux' Vue.use(ToastPlugin) Vue.use(ConfirmPlugi ...