poj 3292 Semi-prime H-numbers

打表的一个水题。

/*
 * Author: stormdpzh
 * Created Time: 2012/6/1 15:44:30
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>

#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define mset(a, b) memset(a, b, sizeof(a))
#define wh(n) while(scanf("%d", &n) != EOF)
#define whz(n) while(scanf("%d", &n) != EOF && n != 0)
#define lint long long

using namespace std;

const int MaxN = 1000015;

int rec[MaxN];
int res[MaxN];

void init()
{
    mset(rec, -1);
    for(int i = 0; i <= (MaxN >> 2); i++)
        rec[(i << 2) ^ 1] = 0;
    rec[1] = 2;
    for(int i = 2; i * i <= MaxN; i++) {
        if(rec[i] != -1) {
            for(int j = i; ; j++) {
                if(rec[j] == -1) continue;
                int tmp = i * j;
                if(tmp > MaxN) break;
                if(rec[i] == 0 && rec[j] == 0)
                    rec[tmp] = 1;
                else rec[tmp] = 2;
            }
        }
    }
    mset(res, 0);
    repf(i, 1, MaxN) res[i] = rec[i] == 1 ? (res[i - 1] + 1) : res[i - 1];
}

int main()
{
    init();
    
    int num;
    whz(num) {
        printf("%d %d\n", num, res[num]);
    }
    
    return 0;
}


你可能感兴趣的:(poj 3292 Semi-prime H-numbers)