zoukankan      html  css  js  c++  java
  • Poj 2739 Sum of Consecutive Prime Numbers

    1.Link

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

    2.Content

    Sum of Consecutive Prime Numbers
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 19167   Accepted: 10513

    Description

    Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime
    numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
    Your mission is to write a program that reports the number of representations for the given positive integer.

    Input

    The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

    Output

    The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

    Sample Input

    2
    3
    17
    41
    20
    666
    12
    53
    0

    Sample Output

    1
    1
    2
    3
    0
    0
    1
    2

    Source

    3.method筛素数法,求次数时利用之前的计算结果,减少计算次数

    4.Code

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring> 
     4 #include <cmath>
     5 #include <vector> 
     6 
     7 #define MAX_NUM 10000
     8 
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     //freopen("D://input.txt", "r", stdin);
    14     
    15     int i,j;
    16     
    17     //prime
    18     bool * arr_prime = new bool[MAX_NUM + 1];
    19     
    20     for(i = 3; i <= MAX_NUM; i += 2) arr_prime[i] = true;
    21     for(i = 4; i <= MAX_NUM; i += 2) arr_prime[i] = false;
    22     arr_prime[2] = true;
    23     
    24     int sqrt_mn = sqrt(MAX_NUM);
    25     
    26     for(i = 3; i < sqrt_mn; i += 2)
    27     {
    28         if(arr_prime[i])
    29         {
    30             for(j = i + i; j <= MAX_NUM; j += i) arr_prime[j] = false;
    31         }
    32     }
    33     
    34     int *arr_res = new int[MAX_NUM + 1];
    35     memset(arr_res, 0, sizeof(int) * (MAX_NUM + 1));
    36     
    37     vector<int> v_sum;
    38     for(i = 2; i <= MAX_NUM; ++i)
    39     {
    40         if(arr_prime[i])
    41         {
    42             v_sum.push_back(i);
    43             vector<int>::size_type v_i;
    44             for(v_i = 0; v_i != v_sum.size() - 1; ++v_i)
    45             {
    46                 v_sum[v_i] += i;
    47             }
    48             
    49             for(v_i = 0; v_i != v_sum.size(); ++v_i)
    50             {
    51                 if(v_sum[v_i] <= MAX_NUM)
    52                 {
    53                     ++(arr_res[v_sum[v_i]]);
    54                     //cout << v_sum[v_i] << endl;
    55                 }
    56             }            
    57         }
    58     }
    59     
    60     int a;
    61     cin >> a;
    62     while(a != 0)
    63     {
    64         cout << arr_res[a] << endl;
    65         
    66         cin >> a;
    67     }
    68     
    69     
    70     delete [] arr_res;
    71     
    72     delete [] arr_prime;
    73      
    74     return 0;
    75 }
    76  

    5.Reference

    http://blog.csdn.net/liukehua123/article/details/5482854

  • 相关阅读:
    PL/pgSQL的RETURN QUERY例子
    PL/pgSQL的 RETURN NEXT例子
    PL/pgSQL学习笔记之二
    基于React的PC网站前端架构分析
    DialogFragment创建默认dialog
    一个RecycleView的强大adapter
    企业者的福音之8266接入阿里智能,点亮一盏灯。
    基于webmagic的种子网站爬取
    自上而下渐显图片的CSS3实现
    用SwiftGen管理UIImage等的String-based接口
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3933391.html
Copyright © 2011-2022 走看看