POJ 2739 Sum of Consecutive Prime Numbers(素数表)
2010-06-13 14:13
513 查看
//简单题 //打一素数表然后预处理下,把所有答案都打出来就可以了 #include<iostream> using namespace std; int ans[10010]; const int MAXP = 10000; int prime[100001];//记录素数数据,prime[0]为素数表的元素个数 bool is_prime[100010];//标号匹配 //筛选法生成素数表模板,感觉像容斥原理,把非质数的标为false void prime_list() { memset(is_prime,true,sizeof(is_prime));//初始化素数表 for (int i = 2;i <= MAXP;i++) { if (is_prime[i])//如果在素数表中匹配的 prime[++prime[0]] = i;//++prime[0],增加素数的个数 for (int j = 1,k; (j <= prime[0]) && (k = i * prime[j]) <= MAXP;j++) //筛选循环 { is_prime[k] = false; if(i % prime[j] == 0) break;//很关键的剪枝步骤 } } } int main() { int n; prime_list(); memset(ans,0,sizeof(ans)); for(int i = 1;i < prime[0];++i) { int temp = prime[i]; for(int j = i+1;j < prime[0];++j) { if(temp > 10000) break; ans[temp]++; temp += prime[j]; } } while(cin >> n && n != 0) { cout << ans << endl; } return 0; }
相关文章推荐
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表 + 暴力)
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
- POJ 2739: Sum of Consecutive Prime Numbers - 素数和
- poj2739 Sum of Consecutive Prime Numbers 尺取法 素数打表
- POJ2739 Sum of Consecutive Prime Numbers【素数筛选+尺取法】
- POJ2739 - Sum of Consecutive Prime Numbers(素数问题)
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表+素数搜索)
- POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)
- POJ2739,Sum of Consecutive Prime Numbers,连续素数相加和,水过
- POJ 2739 Sum of Consecutive Prime Numbers 素数打表+尺取法
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)
- POJ 2739 Sum of Consecutive Prime Numbers(素数序列和,尺取法)
- Sum of Consecutive Prime Numbers POJ - 2739 素数打表—埃氏筛法
- 【原】 POJ 2739 Sum of Consecutive Prime Numbers 筛素数+积累数组 解题报告
- POJ 2739 Sum of Consecutive Prime Numbers-数论-(连续素数和)
- POJ 2739 Sum of Consecutive Prime Numbers(素数表的应用)
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
- poj 2739 Sum of Consecutive Prime Numbers【素数筛】
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】