• poj 2739 Sum of Consecutive Prime Numbers 尺取法


    Time Limit: 1000MS   Memory Limit: 65536K

    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
    题意:输入一个数字(<=1e5)求该数可由几种在素数表中连续的素数之和组成
    思路:用尺取法,注意退出循环的情况
      1 #include <iostream>
      2 #include <cstdio>
      3 using namespace std;
      4 #define N 10010
      5 
      6 int prime[N];//素数表
      7 
      8 int quickmod(int a,int b,int c)//快速幂模
      9 {
     10     int ans=1;
     11 
     12     a=a%c;
     13 
     14     while (b)
     15     {
     16         if (b&1)
     17         {
     18             ans=ans*a%c;
     19         }
     20         a=a*a%c;
     21         b>>=1;
     22     }
     23 
     24     return ans;
     25 }
     26 
     27 bool miller(int n)//米勒求素数法
     28 {
     29     int i,s[5]={2,3,5,7,11};
     30 
     31     for (i=0;i<5;i++)
     32     {
     33         if (n==s[i])
     34         {
     35             return true;
     36         }
     37 
     38         if (quickmod(s[i],n-1,n)!=1)
     39         {
     40             return false;
     41         }
     42     }
     43     return true;
     44 }
     45 
     46 void init()
     47 {
     48     int i,j;
     49 
     50     for (i=2,j=0;i<N;i++)//坑点:注意是i<N,而不是j<N
     51     {
     52         if (miller(i))
     53         {
     54             prime[j]=i;
     55             j++;
     56         }
     57     }
     58 }
     59 
     60 void test()
     61 {
     62     int i;
     63     for (i=0;i<N;i++)
     64     {
     65         printf("%6d",prime[i]);
     66     }
     67 }
     68 
     69 int main()
     70 {
     71     int n,l,r,ans,sum;//l为尺取法的左端点,r为右端点,ans为答案,sum为该段素数和
     72 
     73     init();
     74 //    test();
     75 
     76     while (scanf("%d",&n)&&n)
     77     {
     78         l=r=ans=0;
     79         sum=2;
     80 
     81         for (;;)
     82         {
     83             while (sum<n&&prime[r+1]<=n)//prime[r+1]<=n表示该数是可加的,意即右端点还可以继续右移
     84             {
     85                 sum+=prime[++r];
     86             }
     87 
     88             if (sum<n)//右端点无法继续右移,而左端点的右移只能使sum减小,意即sum数组无法再大于等于n,就可以退出循环
     89             {
     90                 break;
     91             }
     92 
     93             else if (sum>n)
     94             {
     95                 sum-=prime[l++];
     96             }
     97 
     98             else if (sum==n)
     99             {
    100                 ans++;
    101                 sum=sum-prime[l];
    102                 l++;
    103             }
    104         }
    105 
    106         printf("%d
    ",ans);
    107     }
    108 
    109     return 0;
    110 }
  • 相关阅读:
    c/c++ # and ## in macros以及宏debug
    postgresql unnamed statement
    postgresql/lightdb a [left/right] join b on true的含义
    openjdk、javafx各发行版
    lightdb for postgresql PL/pgSQL perform、execute、call区别
    postgresql有mysql兼容插件吗?
    各种互联网公司,不要再那么没有分寸的刷屏QPS/TPS/日活千万这些毫无意义的数据了
    PostgreSQL分布式数据库实践
    LightDB发布日常运维管理手册
    恒生电子发布金融分布式数据库LightDB
  • 原文地址:https://www.cnblogs.com/hemeiwolong/p/9072737.html
Copyright © 2020-2023  润新知