zoukankan      html  css  js  c++  java
  • 【原】 POJ 2262 Goldbach's Conjecture 筛素数 解题报告

    http://poj.org/problem?id=2262

    方法:
    首先筛得1000000以下的素数表,复杂度N*lglgN
    再用测试值减去素数表中从小到大的值,判断如果该值是素数,则不需要再接着找了,直接输出。复杂度N

    注意点:
    1、由于N很大,所以筛素数在计算i*i时可能会超过int的范围,所以需要采用__int64
    2、需要符合要求的a,b使得a+b=c的方法:
           a)枚举的方法。复杂度n^2
           b)顺序扫描符合要求的元素a,判断c-a是否也符合要求。复杂度n

    Description

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

    Every even number greater than 4 can be 
    written as the sum of two odd prime numbers.

    For example:

    8 = 3 + 5. Both 3 and 5 are odd prime numbers. 
    20 = 3 + 17 = 7 + 13. 
    42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

    Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.) 
    Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million.

    Input

    The input will contain one or more test cases. 
    Each test case consists of one even integer n with 6 <= n < 1000000. 
    Input will be terminated by a value of 0 for n.

    Output

    For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's conjecture is wrong."

    Sample Input

    8

    20

    42

    0

    Sample Output

    8 = 3 + 5

    20 = 3 + 17

    42 = 5 + 37

       1: #include <iostream>
       2:  
       3: using namespace std ;
       4:  
       5: const __int64 N = 1000001 ;
       6: const __int64 M = 78500;
       7: char table[N] = {0} ;
       8: //int prime[M] ;
       9:  
      10: //0为素数
      11: void GetPrimeTable()
      12: {
      13:     __int64 i ;
      14:     __int64 j ;
      15:  
      16:     table[0]=table[1]=1;
      17:     i=2 ;
      18:     while( i<N )
      19:     {
      20:         for( j=i*i ; j<N ; j+=i )
      21:             table[j]=1 ;
      22:         do ++i ;
      23:         while( i<N && table[i]==1 );
      24:     }
      25:     /*
      26:     for( i=2,j=0 ; i<N ; ++i )
      27:     {
      28:         if( table[i]==0 )
      29:             prime[j++]=i ;
      30:     }
      31:     */
      32: }
      33:  
      34:  
      35: void run2262()
      36: {
      37:     GetPrimeTable() ;
      38:  
      39:     int val ;
      40:     int a,b ;
      41:     int i,j ;
      42:     int sum ;
      43:     bool flag ;
      44:     while(cin>>val && val!=0)
      45:     {
      46:         /* TLE
      47:         //枚举 n^2
      48:         flag = false ;
      49:         for( i=0 ; i<M && flag==false ; ++i )
      50:         {
      51:             for( j=i+1 ; j<M ; ++j )
      52:             {
      53:                 sum = prime[i]+prime[j] ;
      54:                 if( sum > val )
      55:                 {
      56:                     flag = true ;
      57:                     break ;
      58:                 }
      59:                 else if( sum == val )
      60:                 {
      61:                     a = prime[i] ;
      62:                     b = prime[j] ;
      63:                     flag = true ;
      64:                     break ;
      65:                 }
      66:             }
      67:         }
      68:         */
      69:         for( i=2 ; i<N ; ++i )
      70:         {
      71:             if( table[i]==0 && table[val-i]==0 )
      72:             {
      73:                 a = i ;
      74:                 b = val-i ;
      75:                 break ;
      76:             }
      77:         }
      78:         cout<<val<<" = "<<a<<" + "<<b<<endl ;
      79:     }
      80: }
  • 相关阅读:
    angularjs html事件(ng-click,ng-hide,ng-show)
    《javascript设计模式与开放实践》学习(二)对象方法的借用
    《javascript设计模式与开放实践》学习(二)Function.prototype.bind
    《javascript设计模式与开放实践》学习(一)javascript实现多态2
    《javascript设计模式与开放实践》学习(一)javascript实现多态1
    关于BFC
    css三角形的绘制
    ER图
    如何使用powerDesigner 生成 html文件
    spring aop 会根据实际情况(有无接口)自动选择 两种 动态代理(jdk和cglib)之一
  • 原文地址:https://www.cnblogs.com/allensun/p/1870215.html
Copyright © 2011-2022 走看看