UVa1210 Sum of Consecutive Prime Numbers
2014-07-30 10:39
393 查看
题意:求一个数能表示为几组连续素数相加的形式。如41=2+3+5+7+11+13=11+13+17=41,结果就是3。
思路:筛法素数打表,暴力枚举连续素数的起点和终点统计。
思路:筛法素数打表,暴力枚举连续素数的起点和终点统计。
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> #include <iomanip> #include <cstdlib> #include <string> #include <memory.h> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <ctype.h> #include<time.h> #define INF 1000000 using namespace std; bool tab[10010]; int primes[2000]; int ans[10010]; int main(){ for(int i=0;i<10010;i++)tab[i]=1; tab[0]=tab[1]=0; for(int i=2;i<=5000;i++){ for(int j=i*i;j<=10000;j+=i){ tab[j]=0; } } int count=1; primes[1]=2; for(int i=3;i<=10000;i+=2){ if(tab[i])primes[++count]=i; } memset(ans,0,sizeof(ans)); for(int i=1;i<=count;i++){ int tmp=0; for(int j=i;j<=count;j++){ tmp+=primes[j]; if(tmp<=10000)ans[tmp]++; } } int n; while(cin>>n){ if(!n)break; cout<<ans <<endl; } return 0; }
相关文章推荐
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVA 1210 Sum of Consecutive Prime Numbers(数论)
- UVa 1210 - Sum of Consecutive Prime Numbers(素数+连续和)
- UVALive3399 UVA1210 POJ2739 Sum of Consecutive Prime Numbers【素数筛选+尺取法】
- Sum of Consecutive Prime Numbers - UVa 1210 水题
- 【UVA】1210 - Sum of Consecutive Prime Numbers
- 习题10-6 UVA - 1210 Sum of Consecutive Prime Numbers 连续素数之和(滑动窗口)
- Sum of Consecutive Prime Numbers UVA - 1210
- Sum of Consecutive Prime Numbers UVA - 1210 连续素数之和
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers(滑动窗口)
- UVa1210 - Sum of Consecutive Prime Numbers(欧拉筛法即线性筛法)
- UVA - 1210 - Sum of Consecutive Prime Numbers
- [UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers
- UVa1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- Uva-1210 Sum of Consecutive Prime Numbers
- UVA 1210(p343)----Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers