POJ 2739 - Sum of Consecutive Prime Numbers(素数筛法+前缀和 / 尺取法)
来源:互联网 发布:网络水军有多少 编辑:程序博客网 时间:2024/04/30 00:27
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.
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
2317412066612530
Sample Output
11230012
Way1:
素性测试O(根号n)
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int num[1500];bool test(int x){ for(int i = 2; i * i <= x; ++i) { if(x % i == 0) return false; } return true;}int job(){ int k= 1; num[0] = 0; num[k] = 2; for(int i = 3; i < 10000; ++i) { if(test(i)) { k++; num[k] = num[k - 1] + i; } } return k;}int main(){ int n; int s = job(); while(~scanf("%d", &n)) { if(n == 0) break; int ans = 0; int tmp = 0; for(int i = 0; i < s; ++i) { for(int j = i + 1; j < s; ++j) { if(num[j] - num[i] > n) break; if(num[j] - num[i] == n) ans++; } } printf("%d\n", ans); } return 0;}
Way2:
埃氏素数筛法O(nloglogn)
CODE:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX = 10005;int pri[MAX], vis[MAX], ans[MAX];int main(){ int n, p = 0; memset(ans, 0, sizeof(ans)); memset(vis, 1, sizeof(vis)); for(int i = 2; i <= MAX; ++i) { if(!vis[i]) continue; pri[p++] = i; for(int j = 2*i; j < MAX; j+=i) vis[j] = 0; } for(int i = 0; i < p; ++i) { int s = 0; for(int j = i; j < p; ++j) { s += pri[j]; if(s > MAX) break; ans[s]++; } } while(~scanf("%d", &n)) { if(n == 0) break; printf("%d\n", ans[n]); } return 0;}
尺取法.
AC.
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX = 100005;int pri[MAX], vis[MAX];int n, p;void ready(){ p = 0; memset(vis, 0, sizeof(vis)); for(int i = 2; i < MAX; ++i) { if(vis[i]) continue; pri[p++] = i; for(int j = i*2; j < MAX; j += i) vis[j] = 1; }}void solve(){ int sum = 0, k = 0, t = 0; int ans = 0; while(1) { while(t <= p && sum < n) sum += pri[t++]; if(pri[t-1] > n) break; if(sum == n) ans++; sum -= pri[k++]; } printf("%d\n", ans);}int main(){//freopen("in", "r", stdin); ready(); while(~scanf("%d", &n)) { if(n == 0) break; solve(); } return 0;}
0 0
- 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 E - 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
- 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) 素数筛法+暴力 Java
- POJ 2739 Sum of Consecutive Prime Numbers(素数表)
- poj 2739 Sum of Consecutive Prime Numbers(筛选素数 预处理)
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)
- 使用JavaScript进行基本图形操作与处理
- HTML5 File API: How to see the result of readAsText()
- 安装eclipse maven插件(手动离线安装)
- html5学习4:HTML5之input:file说明
- 机器学习与智能优化 之 学习有道
- POJ 2739 - Sum of Consecutive Prime Numbers(素数筛法+前缀和 / 尺取法)
- mahout-0.9的安装配置
- LightOJ 1189 - Sum of Factorials(贪心)
- 用绘制控件绘制要素示例(openlayers)
- [System Design]Scalability for Dummies学习笔记
- LightOJ 1078 - Integer Divisibility(取模运算)
- 从网络获取时实天气信息并动态生成
- 树莓派-上手体验1
- X-UA-Compatible 兼容模式