• POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)


    描述


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

    多次询问,对于一个给定的n,求有多少组连续的素数,满足连续素数之和为n.

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

    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

    分析


    尺取法.

    打个素数表,对于每一个n,尺取之.

    换了种尺取法的写法= =.

     1 #include<cstdio>
     2 
     3 const int maxn=10005;
     4 int p,n;
     5 int prime[maxn];
     6 bool is_prime[maxn];
     7 
     8 void get_prime()
     9 {
    10     for(int i=1;i<maxn;i++) is_prime[i]=true;
    11     is_prime[0]=is_prime[1]=false;
    12     p=0;
    13     for(int i=2;i<maxn;i++)
    14     {
    15         if(is_prime[i])
    16         {
    17             prime[++p]=i;
    18             for(int j=2*i;j<maxn;j+=i) is_prime[j]=false;
    19         }
    20     }
    21 }
    22 
    23 void solve()
    24 {
    25     int l=1,r=0,sum=0,ans=0;
    26     while(l<=p&&r<=p&&prime[l]<=n&&prime[r]<=n)
    27     {
    28         if(sum==n) { ans++; sum-=prime[l++]; }
    29         else if(sum>n) sum-=prime[l++];
    30         else sum+=prime[++r];
    31     }
    32     printf("%d
    ",ans);
    33 }
    34 
    35 void init()
    36 {
    37     get_prime();
    38     while(scanf("%d",&n)==1&&n!=0) solve();
    39 }
    40 
    41 int main()
    42 {
    43     freopen("sum.in","r",stdin);
    44     freopen("sum.out","w",stdout);
    45     init();
    46     fclose(stdin);
    47     fclose(stdout);
    48     return 0;
    49 }
    View Code
  • 相关阅读:
    python线程详解
    Beego框架在模板中使用自定义函数
    golang打印英文格式时间日期
    如何让SQL语句不执行默认排序,而是按照in语句的顺序返回结果
    Git clone 报错 Unable to negotiate with xxx.xxx.xxx.xxx port 12345: no matching cipher found. Their offer: aes128-cbc,3des-cbc,blowfish-cbc
    Mysql去掉html标签函数
    Nodejs的npm安装模块时候报错:npm ERR! Error: CERT_UNTRUSTED的解决方法
    树莓派使用DHT11温湿度传感器
    ubuntu源列表(清华,阿里,官方,选一即可)
    将tgz文件解压到指定目录
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5424017.html
Copyright © 2020-2023  润新知