Semi-prime H-numbers(数学 + 素数筛选 + 剪枝)

Semi-prime H-numbers
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7063   Accepted: 3032

Description

This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 numbers. Here, we do only a bit of that.

An H-number is a positive number which is one more than a multiple of four: 1, 5, 9, 13, 17, 21,... are the H-numbers. For this problem we pretend that these are the only numbers. The H-numbers are closed under multiplication.

As with regular integers, we partition the H-numbers into units, H-primes, and H-composites. 1 is the only unit. An H-number h is H-prime if it is not the unit, and is the product of two H-numbers in only one way: 1 × h. The rest of the numbers are H-composite.

For examples, the first few H-composites are: 5 × 5 = 25, 5 × 9 = 45, 5 × 13 = 65, 9 × 9 = 81, 5 × 17 = 85.

Your task is to count the number of H-semi-primes. An H-semi-prime is an H-number which is the product of exactly two H-primes. The two H-primes may be equal or different. In the example above, all five numbers are H-semi-primes. 125 = 5 × 5 × 5 is not an H-semi-prime, because it's the product of three H-primes.

Input

Each line of input contains an H-number ≤ 1,000,001. The last line of input contains 0 and this line should not be processed.

Output

For each inputted H-number h, print a line stating h and the number of H-semi-primes between 1 and h inclusive, separated by one space in the format shown in the sample.

Sample Input

21 
85
789
0

Sample Output

21 0
85 5
789 62

 

    题意:

    定义 H 数是满足 H = 4x + 1的数。分为 H 合数 ,H 素数 和 1(1 即不是 H 素数也不是 H 合数)。H 素数指的是其因子只有 1 和 其本身。H-semi-prime 指的是只能由两个 H 素数相乘得来的数,这两个数可以相等也可以不相等。给出 H (小于1000001),求出 H 之内一共有多少个 H-semi-prime。

 

    思路:

    数学 + 素数筛选 + 剪枝。

    要注意的是要分清我们平常的数和H数,9 也是 H 素数,不能拆成 3 X 3 这样子,因为3不是H数,更加谈不上是 H 素数了,所以 9 只能由 1 和 9 本身组成,故 9 也为 H 素数,类似 9 这样的数也如此。

    筛选的时候直接以4为阶加上去即可,同时将 H 素数保存在另外一个数组中,以省时。要求由两个数 H 素数相乘而得,那么枚举每对 H 素数相乘的得数,得数再判断是不是 H 数即可,两个 H 素数相乘所得的数不能保证一定是 H 数,故还需要判断。

    除此之外要剪枝,两趟循环都必须剪枝,当大于题目给出的 1000001 时,不必再判断下去了,直接跳出循环即可,不然会导致超时,并且运行不了。最后相乘所得的数也要另外开个数组标记,标记后要叠加计算好区间和,不然再多项输入里面再一个个算的话也会导致超时。

 

    AC:

#include <stdio.h>
#include <string.h>
#define MAX 1000005

int h_pri[MAX],ans[MAX],num[MAX];
int res;

void solve() {
    memset(h_pri,0,sizeof(h_pri));
    memset(num,0,sizeof(num));

    res = 0;
    h_pri[1] = 1;
    for(int i = 5;i < MAX;i += 4) {
        if(h_pri[i]) continue;
        else {
            ans[res++] = i;
            for(int j = i + i;j < MAX;j += i)
                h_pri[j] = 1;
        }
    }

    for(int i = 0;i < res;i++) {
        if(ans[i] * ans[i] > MAX) break;  //剪枝
        for(int j = i;j < res;j++) {
            int k = ans[i] * ans[j];
            if(k > MAX) break;   //剪枝
            if(!((k - 1) % 4)) {
                num[k] = 1;
            }
        }
    }

    for(int i = 1;i < MAX;i++)  num[i] += num[i - 1];
}

int main() {
    int n;

    solve();

    while(~scanf("%d",&n) && n) {
        printf("%d %d\n",n,num[n]);
    }

    return 0;
}

 

 

你可能感兴趣的:(number)