POJ 3292 Semi-prime H-numbers(数)

Semi-prime H-numbers

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

题意  所有可以表示为4*k+1(k>=0)的数都称为“H数”  而在所有“H数”中只能被1和自身整除的H数称为“H素数“   能表示成两个”H素数“积的数又称为”Semi-prime H数“

输入n  求1到n之间有多少个”Semi-prime H数“;

方法  先打个H素数表  再用H素数表中的数依次相乘  得到的数都标记  再用一个数组保存每个数以内的标记数   输入n后直接读数组就行了

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1000001;
int vis[N],hp[N],ans[N],n;
int main()
{
    int num=0,m=sqrt(N+0.5);
    for(int i=5;i<=m;i+=4)
    {
        if(vis[i]==0)
            for(int j=i*i;j<=N;j+=i)
            vis[j]=1;
    }
    for(int i=5;i<N;i+=4)
        if(!vis[i]) hp[++num]=i;

    memset(vis,0,sizeof(vis));
    for(int i=1;hp[i]*hp[i]<=N;++i)
        for(int j=i;hp[i]*hp[j]<=N;++j)
            ++vis[hp[i]*hp[j]];
    num=0;
    for(int i=1;i<N;++i)
        {
            if(vis[i]>=1) ++num;
            ans[i]=num;
        }

    while(scanf("%d",&n),n)
        printf("%d %d\n",n,ans[n]);

    return 0;
}

POJ 3292 Semi-prime H-numbers(数),布布扣,bubuko.com

时间: 2024-12-09 20:19:41

POJ 3292 Semi-prime H-numbers(数)的相关文章

poj 3292 Semi-prime H-numbers 筛数打表

题意: 给定n,求1至n中有多少个数能仅表示成两个(4*x+1,x>=1)的数之积. 分析: 打表,一开始的list[i*j]代表i*j的组成方式数,不需要用乘法,加法就好. 代码: //poj 3292 //sep9 #include <iostream> using namespace std; int list[1200000]; int main() { memset(list,0,sizeof(list)); int i,j,n,m; for(i=5;i<=1001;i+

POJ 2560 Freckles Prime算法题解

本题是求最小生成树. 给出的是坐标节点,然后需要根据这些坐标计算出各个点之间的距离. 除此就是标准的Prime算法了,能使用Prime的基本上都可以使用Kruskal. 这些经典的算法一定要多写,熟练掌握,否则很难灵活运用的. 而且经典的算法之所以为经典,原因之一是没那么容易自己凭空想象出来的,所以要熟练. #include <stdio.h> #include <string.h> #include <queue> #include <float.h> #

【POJ 3292】 Semi-prime H-numbers

[POJ 3292] Semi-prime H-numbers 打个表 题意是1 5 9 13...这样的4的n次方+1定义为H-numbers H-numbers中仅仅由1*自己这一种方式组成 即没有其它因子的 叫做H-prime 两个H-prime的乘积叫做H-semi-prime 另一个要求是H-semi-prime仅仅能由两个H-prime组成 即4个H-number 不可由3个或几个H-number构成 筛出来个满足题意的表 把每一个数内满足的个数存起来O(1)输出就可以 代码例如以下

POJ 3292 Semi-prime H-numbers (仿素数筛)

题目地址:POJ 3292 先利用素数筛的原理把H_prime筛出来,然后打表.要预处理,否则TLE. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #in

HDU 4417.Super Mario-无修改区间小于等于H的数的个数-可持久化线段树

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9618    Accepted Submission(s): 4074 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability

poj 3292 Semi-prime H-numbers

题目链接:http://poj.org/problem?id=3292 题目大意:就是给你一个模4余1的数H-number,如果一个H-number是H-primes 当且仅当它的因子只有1和它本身(除1外).一个H-number是H-semi-prime当且仅当它只由两个H-primes的乘积表示.H-number剩下其他的数均为H-composite.给你一个数h,问1到h有多少个H-semi-prime数. 思路:这题坑惨啦,开始以为数据比较大,就在想办法优化打表,但是结果没想到直接暴力打

POJ题目2689 Prime Distance(任何区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13459   Accepted: 3578 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】

转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面额的钱币,每种各ni张,求机器吐出小于等于所要求钱币的最大值 解析1:这题大牛都用了多重背包,不过,我一同学想出了一种就这题而言特别简单有效的方 法.(话说我就认为这题本来就不需要用到背包的,因为n的范围只到10,太小了).方法就是对钱进行遍历,看这些钱一共能组成多少面额的钱,然后从 cash向下枚

POJ - 2485(最小生成树.prime)

题目链接: http://poj.org/problem?id=2485 题目: Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36525   Accepted: 16329 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the