MPRIME1 - Sum of Primes

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 11 000, inclusive. The end of the input is indicated by a zero.

SAMPLE INPUT
2
3
17
41
20
666
12
53
0

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.

SAMPLE OUTPUT
1
1
2
3
0
0
1
2


Được gửi lên bởi:psetter
Ngày:2009-02-23
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:Tokyo 2005

hide comments
2020-03-28 09:58:03
Code tham khao:
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
loop:
int n;
cin >> n;
if (n == 0) return 0;
else {
vector <int> isPrime(n+1,true);
isPrime[0] = false;
isPrime[1] = false;
for (int i=2;i*i<=n;i++) {
if (isPrime[i]==true) {
for (int j=i*i;j<=n;j+=i) {
isPrime[j] = false;
}
}
}
vector <int> Prime;
int count = 0;
for (int i=2;i<=n;i++) {
if (isPrime[i]) Prime.push_back(i);
}
int size = Prime.size();
for (int i=0;i<size;i++) {
int sum = 0;
for (int j=i;j<size;j++) {
sum += Prime[j];
if (sum==n) {
count++;
goto end;
}
}
end:;
}
cout << count << "\n";
goto loop;
}
}
2017-01-10 11:42:04
Ai làm rồi cho link thuật toán đi =))
2016-11-29 19:28:08
mảng hằng
2016-11-20 10:10:03 hồ vãn tuấn
sang snt+ tim kiem nhi phan
2016-10-26 13:46:52
có ta

2016-10-26 12:57:41
có ai bít làm ko, cho xin code
2016-08-13 16:26:31
mảng hằng =))
2015-03-27 16:13:34 Prismatic
Đếm phân phối + sàng SNT => AC 1 đấm @@
2014-10-04 15:50:56 [KC]★★★★*-RAMEN
duyệt thần cùi cx ac :v
2014-07-22 08:35:46 [KC]★★★★*-RAMEN
duyệt trâu cũng ac
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.