UVA-1210Sum of consecutive Prime Numbers
Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status Description Some positive integers can be is represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer has? For example, the integer is has a representations5 + 7 + + + +and -. The integer Ahas three representations2 + 3 + 5 + 7 + + +,+ + +, and A. The integer 3 has only one representation, which is 3. The integer has no such representations. Note that Summands must is consecutive prime numbers, so neither7 +Nor3 + 5 + 5 + 7is a valid representation for the integer 20. Your mission is to write a program this reports the number of representations for the given positive integer. InputThe input is a sequence of positive integers, each with a separate line. The integers is between 2 and 10000, inclusive. The end of the input is indicated by a zero. OutputThe output should is composed of lines each corresponding to a 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 Nu Mbers. No other characters should is inserted in the output. Sample Input2 3 17 4120 666 12 53 0 Sample Output1 1 2 3 0 0 1 2
Source Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: Mathematics:: Number theory:: Prime Numbers Root:: AOAPC ii:beginning algorithm Contests (Second Edition) (Rujia Liu):: Chapter 10. Maths:: Exercises Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Mathematics: : Java BigInteger Class:: Bonus features:primality TestingSubmit Status |
First the prime table, then preprocessing the prime number prefix and, then enumerate the interval, preprocess the results.
#include <bits/stdc++.h> #define foreach (it,v) for (__typeof ((v). Begin ()) it = (v). Begin (); it! = (v). End (); ++it) Using namespace Std;typedef long long ll;const int MAXN = 10000 + 5;bool check[maxn];const int SZ = 1<<20;struct FAs tio{Char Inbuf[sz]; Char Outbuf[sz]; FastIO () {setvbuf (STDIN,INBUF,_IOFBF,SZ); Setvbuf (STDOUT,OUTBUF,_IOFBF,SZ); }}io;vector<int> init (int n) {memset (check,0,sizeof check); vector<int> Res; for (int i = 2; I <= n; i++) {if (!check[i]) res.push_back (i); int sz = Res.size (); for (int j = 0; j < sz; ++j) {if ((ll) i*res[j]>n) break; CHECK[I*RES[J]] = true; if (i%res[j]==0) break; }} int sz = Res.size (); for (int i = 1; i < sz; i++) res[i] + = res[i-1]; Vector<int> V (n+1,0); for (int i = 0; i < sz; i++) {int L = 0; if (i!=0) L = res[i-1]; for (int j = i; J < Sz; J + +) {int w = res[j]-L; if (w>n) break; v[w]++; }} return v;} int main (int argc, char const *argv[]) {vector<int> res = init (10000); int n; while (~SCANF ("%d", &n) &&n) {printf ("%d\n", Res[n]); } return 0;}
UVA 1210 Sum of consecutive Prime Numbers (number theory)