<OJ_Sicily>Sum of Consecutive Primes

40 篇文章 0 订阅

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.

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.

题目解释:对于一个数n,求其能够用顺序素数相加得到的情况数。如41,41 = 2+3+5+7+11,41=11+13+17,41=41,因此对于41有3种情况

解题思路:

(1)对于求解素数,可以使用筛选法求素数。例如求解1~100内的全部素数:首先排除1,然后用2除3~100的数,如果后面的数能够被2整除则说明该数不是素数,将该数删除。接着用3除剩下的数,以此类推,挖掉不是素数的数,剩下的就是素数了

(2)对于最终结果,也就是能够用顺序素数相加得到的情况数,使用逐个遍历的方法,不会超时。从第一个素数开始,往后相加,直到相加的和不小于该数,最后判断是否等于该数,要是相等,则说明是可行的。

#include <iostream>
#include <math.h>
#include <string.h>
#define MAX 10005
using namespace std;
bool isPrime[MAX];
int prime[MAX];
int prime_num;

void getPrime(){
    int maxV = sqrt(MAX);
    memset(isPrime, true, sizeof(isPrime));
    for (int i = 2; i < maxV; i++) {   // 使用筛选法求素数
        if (isPrime[i]) {
            for (int j = i+1; j < MAX; j++) {
                if (j%i == 0) {
                    isPrime[j] = false;
                }
            }
        }
    }
    prime_num = 0;
    for (int i = 2; i < MAX; i++) {  // 将所有的素数放在一个数组里面
        if (isPrime[i]) {
            prime[prime_num++] = i;
        }
    }
}
int main(int argc, const char * argv[]) {
    // insert code here...
    getPrime();
    int n;
    while (cin >> n) {
        if (n == 0 || n < 2 || n > 10000) break;
        int result = 0;
        for (int i = 0; i < prime_num; i++) {
            int sum  = prime[i];
            if (sum  > n) break;
            int j = i+1;      // 以第i个素数为起始点,开始进行顺序相加
            while (sum < n) {
                sum += prime[j++];
            }
            if (sum == n) {
                result ++;
            }
        }
        cout << result << endl;
    }
    return 0;
}


如果要在不使用 `#include <unordered_set>` 的情况下实现类似功能,我们可以使用组或列表来模拟邻接表,并使用位运算来标记已访问的箱子。这里是一个基于组的解决方案: ```cpp #include <iostream> #include <vector> using namespace std; const int MAX_N = 100; // 假设箱子不超过100个 // 用一个布尔组表示箱子是否被访问过,初始化全为false bool visited[MAX_N + 1]; // 定义邻接矩阵,使用二维组表示箱子之间的钥匙关系 vector<vector<int>> graph(MAX_N + 1, vector<int>(MAX_N + 1)); void findUnlockable(int n, vector<int>& graph, vector<int>& result) { // 初始化遍历标志 for (int i = 0; i <= n; ++i) { visited[i] = false; } // 从第一个箱子开始,进行深度优先搜索 dfs(1, graph, result); } void dfs(int node, vector<int>& graph, vector<int>& result) { // 标记当前箱子已访问 visited[node] = true; // 添加当前箱子到结果中 result.push_back(node); // 遍历邻居节点 for (int nei : graph[node]) { // 如果邻居未被访问,则递归继续搜索 if (!visited[nei]) { dfs(nei, graph, result); } } } int main() { int n, m; cin >> n >> m; // 读取钥匙隐藏关系并更新邻接矩阵 for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; graph[u][v] = 1; // 表示箱子u的钥匙在箱子v中,这里的1只是一个标记 } vector<int> unlockable; // 用于存储结果 findUnlockable(n, graph, unlockable); // 输出结果 for (int box : unlockable) { cout << box << " "; } cout << endl; return 0; } ``` 请注意,这种方法仅适用于箱子量较小的情况,因为组大小是固定的,不适合大规模的据。如果输入规模未知或者可能会很大,建议还是使用 `unordered_set` 或其他哈希集合来优化查找效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值