【UVA】1210 - Sum of Consecutive Prime Numbers

普通的求区间连续和的问题,一开始以为是区间移动,但是怕UVA数据太严,直接打表,后来发现自己的担心是多余的。

14044972 1210 Sum of Consecutive Prime Numbers Accepted C++ 0.049 2014-08-15 10:30:11

打表的话效率可能不是很高.

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF (1 << 10)
#define esp 1e-9
#define MOD 100000007
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 10000 + 1
#define MAX_SIZE 6000000
int Prime_sum[MAXD];
int _count[MAX_SIZE] = {0};
int size = 1;
void Get_Prime(){
    int vis[MAXD] = {0};
    Prime_sum[0] = 0;
    for(int i = 2 ; i < MAXD ; i++)if(!vis[i]){
        if(size == 0)
            Prime_sum[size ++] = i;
        else{
            Prime_sum[size] = Prime_sum[size - 1] + i;
            size++;
        }
        for(int j = i * i ; j < MAXD ; j += i)
            vis[j] = 1;
    }
    return ;
}
void init(){
    Get_Prime();
    for(int i = 1 ; i < size ; i++)
        for(int j = 0 ; j < i ; j++){
            int t = Prime_sum[i] - Prime_sum[j];
            _count[t] ++;
    }
}
int main(){
    init();
    int n;
    while(scanf("%d",&n) && n){
        printf("%d\n",_count[n]);
    }
    return 0;
}

你可能感兴趣的:(【UVA】1210 - Sum of Consecutive Prime Numbers)