类似素数筛。。。
Semi-prime H-numbers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6873 Accepted: 2931

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

Source

Waterloo Local Contest, 2006.9.30

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN=1000100;

int H[MAXN],cnt[MAXN];

void Init()
{
    for(int i=1;i<MAXN;i+=4)
    {
        H=1;
        for(int j=5;j*j<=i;j+=4)
        {
            if(i%j==0)
            {
                H=j;
                break;
            }
        }
    }
    for(int i=5;i<MAXN;i+=4)
    {
        cnt=cnt[i-4];
        if(H!=1&&H[i/H]==1)
        {
            cnt++;
        }
    }
}

int main()
{
    Init();
    int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        printf("%d %d\n",n,cnt[n]);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 3292 Semi-prime H-numbers的更多相关文章

  1. 【POJ 3292】 Semi-prime H-numbers

    [POJ 3292] Semi-prime H-numbers 打个表 题意是1 5 9 13...这样的4的n次方+1定义为H-numbers H-numbers中仅仅由1*自己这一种方式组成 即没 ...

  2. POJ 3292 Semi-prime H-numbers (素数筛法变形)

    题意:题目比较容易混淆,要搞清楚一点,这里面所有的定义都是在4×k+1(k>=0)这个封闭的集合而言的,不要跟我们常用的自然数集混淆. 题目要求我们计算 H-semi-primes, H-sem ...

  3. Day7 - I - Semi-prime H-numbers POJ - 3292

    This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study th ...

  4. POJ 3126:Prime Path(素数+BFS)

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  5. 【POJ】1811 Prime Test

    http://poj.org/problem?id=1811 题意:求n最小素因子.(n<=2^54) #include <cstdio> #include <cstring& ...

  6. POJ 3292

    Semi-prime H-numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7059   Accepted: 3 ...

  7. POJ 2560 Freckles Prime问题解决算法

    这个问题正在寻求最小生成树. 给定节点的坐标,那么我们需要根据各个点之间的这些坐标来计算距离. 除了这是标准的Prime算法的,能源利用Prime基本上,你可以使用Kruskal. 经典的算法必须填写 ...

  8. poj 3925 枚举+prime

    /* 因为15很小可以暴力枚举然后用最小生成树的prim来计算 */ #include<stdio.h> #include<string.h> #include<math ...

  9. 【POJ 2689】 Prime Distance

    [题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数 ...

随机推荐

  1. editGrid分录表格

    waf("分录id").wafGrid("setCellEditorAllConfig", "字段名", "filteritem& ...

  2. nginx ssl证书安装配置

    原理图: - 客户端生成一个随机数 random-client,传到服务器端(Say Hello) - 服务器端生成一个随机数 random-server,和着公钥,一起回馈给客户端(I got it ...

  3. Pipe

    #一边压缩一边传 一边解压 到对方的目录为/tlj/2/ / | ssh root@172.16.200.56 tar xzf - -C /tlj #在一个需要文件名的地方 使用-重定向输出到stdo ...

  4. 画虚线 iOS

    整理了一个方法,可以直接绘制虚线,下面直接上代码.参数说明已经给出,可直接copy使用 /** ** lineView: 需要绘制成虚线的view ** lineLength: 虚线的宽度 ** li ...

  5. Default团队项目立题

    PhyLab物理实验网站 接手PhyLab网站,进一步优化网站体验,增加新的功能.

  6. Git连接到Git@OSC

    1.配置本地git $git config --global user.name "xxx" $git config --global user.email "xxxxx ...

  7. HashMap与ArrayList互相嵌套的代码实现

    HashMap嵌套ArrayList的代码实现 结果要求为 三国演义            吕布            周瑜笑傲江湖           令狐冲            林平之神雕侠侣  ...

  8. Spring解析

    Spring还是蛮有技术含量的,可以自己用代码实践一遍,找了一篇实践的案例: http://qingwengang.iteye.com/blog/621678 先mark下,等后面有时间了实践一遍. ...

  9. js与php转换时间戳

    php时间:1368524732 js代码: function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleStri ...

  10. Session的SqlServer模式的配置

    很多时候,由于各种莫名其妙的原因,会导致session丢失.不过ASP.NET还允许将会话数据存储到一个数据库服务器中,方法是将mode属性变成SqlServer. 在这种情况下,ASP.NET尝试将 ...