zoukankan      html  css  js  c++  java
  • 素数

    以前求0~n之内的素数是一个一个的判断,时间复杂度是O(n^2),速度很慢。
    而今天讲的这种方法速度快多了,时间复杂度是nlong(n),模板如下:
    1 a[0] = a[1] = 1;
    2 for(int i = 2; i <= MAXN; i++){
    3         if(!a[i]){
    4             for(int j = 2*i; j < MAXN; j+=i){
    5                 a[j] = 1;
    6             }
    7         }
    8     }

     其实可以到sqrt(MAXN)就可以了,后面的很多都重复计算了。

    1 int m = (int)sqrt(50000);
    2     for(int i = 2; i <= m; i ++)
    3         if(!p[i])
    4             for(int j=i*i;j <= 50000; j += i) p[j] = 1;
    5     for(int i = 2; i <= 50000; i ++) if(!p[i]) a[++t] = i;
     
    下面贴出几道用到这种方法求答案的题目:
     
    Everybody knows any number can be combined by the prime number.
    Now, your task is telling me what position of the largest prime factor.
    The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
    Specially, LPF(1) = 0.
     
    Input
    Each line will contain one integer n(0 < n < 1000000).
     
    Output
    Output the LPF(n).
     
    Sample Input
    1 2 3 4 5
     
    Sample Output
    0 1 2 1 3
     
    题目是求n的最大素因子是多少,数据大,就可以直接打表了。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int maxn = 1e6+10;
     5 int a[maxn];
     6 
     7 int main(){
     8     int k = 0;
     9     for(int i = 2; i <= 1e6; i++){
    10         if(!a[i]){
    11             a[i] = ++ k;
    12             for(int j = i+i; j <= 1e6; j+=i){
    13                 a[j] = k;
    14             }
    15         }
    16     }
    17     int n;
    18     while(~scanf("%d",&n)){
    19         cout << a[n] << endl;
    20     }
    21     return 0;
    22 }
    If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2".
    Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
     
    Input
    Your program must read test cases from standard input.
    The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
     
    Output
    For each test case, your program must output to standard output. Print in one line the number of twin primes which are no greater than N.
     
    Sample Input
    1 5 20 -2
     
    Sample Output
    0 1 4
     
    题目是求不大于n且满足两个素数相差是2的对数,比如3和5是一对,11和13是一对。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int maxn = 1e5+10;
     5 int a[maxn], b[maxn];
     6 int main(){
     7     a[0] = a[1] = 1;
     8     for(int i = 2; i <= maxn; i++){
     9         if(!a[i]){
    10             for(int j = 2; j*i < maxn; j++){
    11                 a[j*i] = 1;
    12             }
    13         }
    14     }
    15     int k = 0;
    16     for(int i = 2; i <= maxn; i++){
    17         if(a[i] == 0 && a[i-2] == 0){
    18             k++;
    19         }
    20         b[i] = k;
    21     }
    22     int n;
    23     while(~scanf("%d",&n)){
    24         if(n < 0)break;
    25         cout << b[n] << endl;
    26     }
    27     return 0;
    28 }
  • 相关阅读:
    discuz X3.2 自定义系统广告详解
    windows平台myeclipse+PDT+apache+xdebug调试php
    南浮的IT民工
    linux实践——编译安装两个apache
    如何使maven+jetty运行时不锁定js和css[转]
    linux实践——ubuntu搭建 svn 服务
    测试代码插件(插入代码块)
    FTP 文件接口按天批处理脚本实例
    7月份工作小结
    报表开发过程
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/6616904.html
Copyright © 2011-2022 走看看