链接: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.

输入描述:

  1. Input has only one line containing a positive integer N.
  2.  
  3. 1 N 10

7

输出描述:

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

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

-->

示例1

输入

  1. 3

输出

  1. 2
示例2

输入

  1. 5

输出

复制

  1. 6
  2.  
  3. 题意:输入一个nn里面选一对数,满足 这两个式子的数都是素数,不同顺序也算是另一对
  4.  
  5. 思路:我们会发现ij都是素数的话,那么最大公约数为1,那么肯定是一对,然后我们再想想(610),(69)...都是
    那么他们有什么规律呢,就是我们要使除了两个数的最大公约数之后都是素数,那么说明两个数分解之后就应该是 a=(素数)x*n b=(素数)y*n
    n是最大公约数那么其他的满足这个条件对数其实就是一个素数对,同时乘以一个数那么也是,例如(23)那么(46)(69)(812)都是
    满足条件的数,那么我们应该怎么计算呢,下面我们讲个例子
  6.  
  7. 首先想10以内有几个3的倍数呢,10/3=3个,这是常识
    那么我们就来计算,由所有的素数对扩展
    10以内的所有对,因为我们首先应该找出素数对,所以我们应该是遍历所有的素数,
    第一个 2 10/2=510以内有52的倍数,我们再看2的前面有没有素数,没有,不计算
    第二个 3 10/3=3 ....3 6 9,前面有素数2,我们就可以找到素数2组成(23),然后两个数同时乘以23,因为前面的
    小,所以我们始终能在6 9 前面找到4 6组成(46)(69
    第三个:5...
    第四个:7...
  1.  下面看代码实现
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define ll long long
  4. #define pll pair<int,int>
  5. #define se second
  6. #define mod 1000000007
  7. using namespace std;
  8. const int maxn = ;
  9. bool isPrime[maxn];
  10. ll prime[maxn];
  11. ll sum[maxn];
  12. ll add[maxn];
  13. ll total=;
  14. map< pll ,int> mp;
  15. void makePrime2()//筛法找出所有的素数
  16. {
  17. memset(isPrime,true,sizeof(isPrime));
  18. memset(prime,,sizeof(prime));
  19. sum[]=;
  20. for(int i=; i<maxn; i++)
  21. {
  22. if(isPrime[i])
  23. {
  24. prime[total++]=i;
  25. sum[i]=sum[i-]+;//用于存当前位置有多少个素数
  26. }
  27. else sum[i]=sum[i-];
  28. for(int j=; j<total && i*prime[j]<maxn; j++)
  29. {
  30. isPrime[i*prime[j]]=false;
  31. if(i%prime[j]==) break;
  32. }
  33. }
  34. }
  35. int main()
  36. {
  37. makePrime2();
  38. ll n;
  39. scanf("%lld",&n);
  40. ll ans=;
  41. for(int i=; i<total&&prime[i]<=n; i++)
  42. {
  43. int p=n/prime[i];//找出n以内有多少个素数prime[i]的倍数
  44. ans+=(sum[prime[i]]-)*p;//-1因为本身这个素数不算,然后和前面的素数进行匹配与扩展
  45. }
  46. printf("%lld\n",ans*);
  47. }

牛客第三场多校 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. LeetCode--400--第N个数字

    问题描述: 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231). ...

  2. Fiddler抓包分析

    在Fiddler的web session界面捕获到的HTTP请求如下图所示:   各字段的详细说明已经解释过,这里不再说明.需要注意的是#号列中的图标,每种图标代表不同的相应类型,具体的类型包括:   ...

  3. Django中CBV及其源码解释

    FBV(function base views) 就是在视图里使用函数处理请求. CBV(class base views) 就是在视图里使用类处理请求. Python是一个面向对象的编程语言,如果只 ...

  4. sublime 3的破解和安装

    http://www.xue51.com/mac/1518.html 啥都别问,问就是按照上面的网址操作就行,本人亲测可用.

  5. spring boot(十一)MongoDB的使用

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  6. [java]转:String Date Calendar之间的转换

    String Date Calendar之间的转换 String Date Calendar  1.Calendar 转化 String Calendar calendat = Calendar.ge ...

  7. java进行url编码和解码

    public static String getURLEncoderString(String str) { String result = ""; if (null == str ...

  8. python-flask基本应用模板

    1.模板继承 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  9. 码云插件Gitee:Couldn't get the list of Gitee repositories

    20:02 Couldn't get the list of Gitee repositories Can't get available repositories Not Found

  10. 水题系列二:PhoneNumbers

    问题描述: Phonenumbers 企业喜欢用容易被记住的电话号码.让电话号码容易被记住的一个办法是将它写成一 个容易记 住的 单词或 者短语 .例如 ,你 需要给 滑铁卢 大学打 电话时 ,可 以 ...