zoukankan      html  css  js  c++  java
  • 杭电 Problem 2008 分拆素数和 【打表】

    分拆素数和

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 33196    Accepted Submission(s): 14406


    Problem Description
    把一个偶数拆成两个不同素数的和,有几种拆法呢?
     

    Input
    输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
     

    Output
    对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
     

    Sample Input
    30 26 0
     

    Sample Output
    3 2
     
     首先,先打一个素数表,然后用一个ans数组标记状态。两个不相等的素数和为下标,数组每被调用一次,就加一,这样所求偶数对应数组下表下的值就是答案。

    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <cstdlib>
    #include <cstdio>
    #define MAX_N   10005
    #define TY  int
    #define MAX(a, b)   ((a > b)? a: b)
    #define MIN(a, b)   ((a < b)? a: b)
    using namespace std;
    
    bool s[MAX_N];
    int ans[MAX_N];
    void init() {
        memset(s, false, sizeof(s));
        s[0] = s[1] = true;
        for (int i = 0; i < MAX_N; i++) {
            if (s[i])   continue;
            for (int j = 2*i;  j < MAX_N; j += i) {
                s[j] = true;
            }
        }
    }
    void solve() {
        for (int i = 2; i < MAX_N - 2; i++) {
            if (s[i]) continue;
            for (int j = i + 1; j < MAX_N - 1; j++) {
                if (s[j] || i + j > MAX_N) continue;
                    ans[i + j]++;
            }
        }
    }
    int main() {
        int n;
        init();
        solve();
        while (scanf("%d", &n), n) {
            printf("%d
    ", ans[n]);
        }
        return 0;
    }
    


  • 相关阅读:
    url-pattern / /*匹配
    velocity入门
    配置eclipse插件
    Myeclipse 2014 破解
    Eclipse kepler 安装 Dynamic Web Project差距WTP
    Errors running builder 'Faceted Project Validation Builder' on project
    JSF web.xml的各类参数属性配置
    bpm 学习笔记一
    love is ... ...
    .sh_history文件的管理机制
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770930.html
Copyright © 2011-2022 走看看