uva 11105 - Semi-prime H-numbers(数论)

题目链接:uva 11105 - Semi-prime H-numbers

题目大意:H-number为4k+1(k为非负数),H-composites为因子中含有H-number(不包括自己本身)的数,反之久是H-prime,给定n,求有多少H-composites。

解题思路:首先用筛选法求出范围内的H-prime,然后枚举两个判断乘积是否在范围内

#include <cstdio>
#include <cstring>

const int maxn = 1e6+5;
typedef long long ll;

int v[maxn], prime[maxn], cp;

void primeHtable(int n) {
    cp = 0;
    memset(v, 0, sizeof(v));

    for (int i = 5; i < n; i += 4) {
        if (v[i])
            continue;

        prime[cp++] = i;
        for (int j = i * 2; j < n; j += i)
            v[j] = 1;
    }
}

int solve (int n) {
    int ans = 0;
    memset(v, 0, sizeof(v));

    for (int i = 0; prime[i] < n && i < cp; i++) {
        if ((ll)prime[i] * prime[i] > n)
            break;

        for (int j = i; prime[j] < n && j < cp; j++) {

            ll u = (ll)prime[i] * prime[j];
            if (u > n)
                break;


            if (v[u])
                continue;
            ans++;
            v[u] = 1;
        }
    }
    return ans;
}

int main () {
    primeHtable(maxn);
    int n;
    while (scanf("%d", &n) == 1 && n) {
        printf("%d %d\n", n, solve(n));
    }
    return 0;
}

你可能感兴趣的:(uva 11105 - Semi-prime H-numbers(数论))