Sum of Consecutive Prime Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23335 | Accepted: 12757 |
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.
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
题意:给你一个数n,让你求出有多少组连续的质数之和等于这个数
如此只要涉及到连续区间取值的,我们都可以优先想到尺取法,至于质数直接用筛选法处理即可
/*头文件模板*/ #include <map> #include <set> #include <cmath> #include <ctime> #include <queue> #include <vector> #include <cctype> #include <cstdio> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <typeinfo> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define pb push_back #define mp make_pair #define mem(a, x) memset(a, x, sizeof(a)) #define copy(a, b) memcpy(a, b, sizeof(a)) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define FIN freopen("input.txt", "r", stdin) #define FOUT freopen("output.txt", "w", stdout) typedef long long LL; typedef pair<int, int > PII; typedef pair<int,string> PIS; typedef unsigned long long uLL; template<typename T> void print(T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; while(p != q) { if(flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if(p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void print(const T &a, string bes = "") { int len = bes.length(); if(len >= 2)cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } void IO_Init() { ios::sync_with_stdio(false); } LL LLabs(LL a) { return a > 0 ? a : -a; } const double PI = 3.1415926535898; const double eps = 1e-10; const int MAXM = 1e4 + 5; const int MAXN = 1e5 + 5; const LL INF = 0x3f3f3f3f; /*头文件模板*/ int primes[MAXN], cnt, n; bool vispri[MAXN]; void get_primes() { cnt = 1; memset(vispri, false, sizeof(vispri)); for(int i = 2; i < MAXN; i ++) { if(vispri[i]) continue; primes[cnt ++] = i; for(int j = i * 2; j < MAXN; j += i) { vispri[j] = true; } } primes[0] = 0; } void solve(int n) { //(l, r] int l = 0, r = 1, sum = 0, res = 0; while(r <= cnt && primes[r] <= n) { sum += primes[r]; while(sum > n && l < r) { l ++; sum -= primes[l]; } if(sum == n) { res ++; } r ++; } printf("%d\n", res); } int main() { get_primes(); while(~scanf("%d", &n), n) { solve(n); } return 0; }