poj3292 Semi-prime H-numbers

来源:互联网 发布:《算法》pdf 编辑:程序博客网 时间:2024/06/09 15:35

Semi-prime H-numbers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8845 Accepted: 3896

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 857890

Sample Output

21 085 5789 62

这个题一直哇!哇!的QAQ。

题意:在这里我们认为只有4n+1的数就是5...9...13....4n+1,然后我们规定有3类数:

1.H-prim 顾名思义,就是在这个系统里面的质数,因子没有别的4n+1的数。注意是在这个4n+1系统里的而不是整个实数哦~

2.H-semi-prim两个素数的乘积,注意!是准确的两个素数的乘积,像125可能有3个就不对了。

3.其余的那些数就是H-composites~了。

思路:类似素数筛的方法,把1-1000001的素数筛出来,然后依次枚举两个的乘积,用一个数组加标记,最后累加就可以辣~

其余的也没什么好说的。

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int MAXN=1000000+10;bool prim[MAXN];int primnum[MAXN];int ans[MAXN];int ha[MAXN];int hcnt;void get_prim(){    int i,j;    hcnt=0;    for(i=5; i<=1000001; i+=4)    {        if(prim[i])continue;        primnum[hcnt++]=i;        for(j=i; j<=1000001/i; j+=4)prim[i*j]=1;    }}void get_ans(){    int i,j;    for(i=0; i<hcnt; ++i)    {        for(j=i; j<hcnt; ++j)        {            long long x=(long long)primnum[i]*primnum[j];            if(x>1000001)break;            ha[x]++;        }    }    int cnt=0;    for(i=5;i<=1000001;i+=4)    {        if(ha[i])ans[i]=++cnt;        else ans[i]=cnt;    }}int main(){    int n;    get_prim();    get_ans();    while(~scanf("%d",&n)&&n)    {        printf("%d %d\n",n,ans[n]);    }    return 0;}

还有一种做法就是,整体来筛选。(看起来就像是直接暴力枚举两个数的乘积)

因为这个系统无非就是3种数:素数,我们要找的数,和那个其余数。

不难想,任何数都可以最终分解成n个素数乘积的形式。所以我们可以这样做:

起始全标记为0(默认为素数),依次枚举两个的乘积,如果是两个素数的乘积(我们要找的数)就标记上1,否则就标记上个别的(其余数)。

最后累加1的就可以了~

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int MAXN=1000000+10;int prim[MAXN];int ans[MAXN];int ha[MAXN];int hcnt;void get_prim(){    int i,j;    hcnt=0;    for(i=5; i<=sqrt(1000001); i+=4)//其实思想依旧是欧拉筛的思想    {        for(j=i; j<=1000001/i; j+=4)        {            if(!prim[i]&&!prim[j])prim[i*j]=1;            else prim[i*j]=-1;        }    }}void get_ans(){    int i;    int cnt=0;    for(i=5; i<=1000001; i+=4)    {        if(prim[i]==1)ans[i]=++cnt;        else ans[i]=cnt;    }   /* for(i=5; i<=10001; ++i)    {        if(prim[i]==1)            printf("%d ",i);    }*/}int main(){    int n;    get_prim();    get_ans();    while(~scanf("%d",&n)&&n)    {        printf("%d %d\n",n,ans[n]);    }    return 0;}






0 0
原创粉丝点击