标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20560 | Accepted: 11243 |
Description
Input
Output
Sample Input
2 3 17 41 20 666 12 53 0
Sample Output
1 1 2 3 0 0 1 2
Source
题目大意:
这道题是说,我给你一个数字n,然后你求出n==prime[i]+prime[i+1]+prime[i+2]+...+prime[i+tt]的方案数。
解题思路:
很水的一道题,上来后先打一个10000以内的素数表,然后你只需要处理好素数串的头和尾就行了,什么是头和尾呢?
就是for( int i = 1;prime[i] <= n;i++) ,因为我们这样想啊,如果prime[i] > n 的话,肯定是直接退出的,不用在判断了
然后内部的判断用一个tt变量边走边标记就好
代码:
1 # include<cstdio> 2 # include<iostream> 3 # include<fstream> 4 # include<algorithm> 5 # include<functional> 6 # include<cstring> 7 # include<string> 8 # include<cstdlib> 9 # include<iomanip> 10 # include<numeric> 11 # include<cctype> 12 # include<cmath> 13 # include<ctime> 14 # include<queue> 15 # include<stack> 16 # include<list> 17 # include<set> 18 # include<map> 19 20 using namespace std; 21 22 const double PI=4.0*atan(1.0); 23 24 typedef long long LL; 25 typedef unsigned long long ULL; 26 27 # define inf 999999999 28 # define MAX 10000+4 29 30 int prime[MAX]; 31 int book[MAX]; 32 int len = 0; 33 34 void init() 35 { 36 for ( int i = 2;i <= MAX;i++ ) 37 { 38 book[i] = 1; 39 } 40 for ( int i = 2;i <= MAX;i++ ) 41 { 42 if ( book[i]==1 ) 43 { 44 for ( int j = 2*i;j <= MAX;j+=i ) 45 { 46 book[j] = 0; 47 } 48 } 49 } 50 len = 0; 51 for ( int i = 2;i <= MAX;i++ ) 52 { 53 if ( book[i]==1 ) 54 { 55 prime[++len] = i; 56 } 57 } 58 // for ( int i = 1;i <= len;i++ ) 59 // { 60 //// cout<<prime[i]<<" "; 61 // } 62 // cout<<endl; 63 64 } 65 66 67 int main(void) 68 { 69 init(); 70 // cout<<prime[1]<<endl; 71 int n; 72 while ( cin>>n ) 73 { 74 if ( n==0 ) 75 break; 76 77 int tot = 0; 78 int temp = 0; 79 int j = 1; 80 81 for ( int i = 1;prime[i] <= n;i++ ) 82 { 83 temp = 0; 84 for ( int tt = i;tt <= len;tt++ ) 85 { 86 temp+=prime[tt]; 87 if ( temp==n ) 88 { 89 tot++; 90 break; 91 } 92 if ( temp > n ) 93 { 94 break; 95 } 96 } 97 } 98 99 cout<<tot<<endl; 100 101 } 102 103 104 105 return 0; 106 }
POJ 2739 Sum of Consecutive Prime Numbers(水题)
标签:
原文地址:http://www.cnblogs.com/wikioibai/p/4449024.html