【POJ 2739】 Sum of Consecutive Prime Numbers 前缀和预处理 打表 线性筛

17 篇文章 0 订阅

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

题意:给一个数,问它能被分解成几个连续的素数

思路(预处理+前缀和):

写完看有些博主用尺取法,优化的很好。不过这个题数据量小,10000以内的素数才1000个左右,所以可以考虑O(n2)暴力。
1.先线性筛法把素数全部记录在primes数组里面
2.然后对该数组求求缀和记录在sum数组中
3.然后对sum数组枚举区间左右端点,看(sum[i]-sum[j-1]==n)是否成立即可

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <math.h>
#include <queue>
#include <stack>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define maxn 10000+500
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxm = 100000+5;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') ch = getchar();while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x; }
bool vis[maxn];
ll primes[maxn];int tot = 1;
int n;
void Prime()
{
    memset(vis,0,sizeof(vis));
    vis[2] = 0, vis[4] = 1;
    primes[0] = 2;
    for(int i=3;i<maxn;i+=2)
    {
        if(vis[i]==0) primes[tot++] = i;
        for(int j=0;j<tot&&i*primes[j]<maxn;j++)
        {
            vis[i*primes[j]] = 1;
            if(i%primes[j]==0) break;
        }
    }
}
int sum[maxn];
int main()
{
    Prime();
    //cout<<tot<<endl;
    sum[0] = 0;
    for(int i=0;i<tot;i++)
    sum[i+1] = sum[i] + primes[i];
    while(~scanf("%d",&n)&&n)
    {
         int idx = lower_bound(primes,primes+tot,n)-primes;
         if(primes[idx]!=n) idx--;
         int cnt = 0;
         for(int i=idx+1;i>=1;i--)
         {
             for(int j=i;j>=1;j--)
             {
                 if(sum[i]-sum[j-1]==n)  cnt++;
             }
         }
         printf("%d\n",cnt);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值