POJ 2739
来源:互联网 发布:路由器mac地质作用 编辑:程序博客网 时间:2024/06/01 08:21
Sum of Consecutive Prime Numbers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 25167 Accepted: 13722
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
题意:输入一个n,让你确定n可以等于由几种按顺序的素数相加的到,如果n本身是素数,也算一种情况。这一题的思路很简单, 就是暴力的取找,满足条件的情况。显然, 让你可以从最小素数2开始,往上按顺序不断的加下一个素数,如果某一时刻这些素数的和正好等于n, 很显然,这种情况符合,计数加一;如果大于n,说明以这个素数为累加起点不能使得相加和为n, 则转换为下一个比这个起点大且相邻的素数为起点,继续重复上述步骤,直到,这个起点素数大于n为止。#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#define N 10005using namespace std;int is_prime[N], prime[N];int n, num;int Eratosthenes(int n){ memset(is_prime, 0, sizeof(is_prime)); memset(prime, 0, sizeof(prime)); int cot = 0; for(int i = 2; i <= n; i++) { if(prime[i] == 0) { is_prime[cot++] = i; } for(int j = i * i; j <= n; j += i) { prime[j] = 1; } } return cot;}void dfs(int sum, int first){ if(sum == n) { num++; return; } if(sum+is_prime[first] > n) return; else { dfs(sum+is_prime[first], first+1); } return;}int main(){ int cot; cot = Eratosthenes(10005); while(~scanf("%d", &n) && n != 0) { num = 0; int k = 0; for(int i = 0; i <= cot; i++) { if(is_prime[i] > n) { k = i; break; } } for(int i = 0; i <= k; i++) { dfs(0, i); } cout << num << endl; } return 0;}
阅读全文
0 0
- POJ 2739
- poj 2739
- poj 2739
- POJ 2739
- POJ 2739
- poj-2739
- POJ 2739
- poj 2739
- poj 2739
- POJ 2739
- POJ 2739
- poj 2739
- Poj 2739
- POJ 2739
- POJ 2739
- POJ 2739
- poj 2739
- 【POJ】2739
- Java:简单的RMI
- 验证码生成及使用
- Activity的生命周期
- 欢迎使用CSDN-markdown编辑器
- [leetcode] predict the winner
- POJ 2739
- HDU
- CodeForces
- JavaWeb环境搭建
- Apache
- 11-散列1 电话聊天狂人 (25分)
- Apache
- java异常分类
- JavaBean快捷键的使用