Semi-prime H-numbers

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 10466 Accepted: 4665

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


解题心得:

  1. 题意:

    • 如果一个数是4*n+1,那么这个数是H-number
    • 如果一个数是H-number且这个数是一个素数,那么这个数是H-prime
    • 如果一个数是H-number且这个数的因子仅仅有两个H-prime那么这个数是H-semi-prime(不包括1和他本身)
    • H-number剩下的数是H-composite
  2. 其实就是一个艾氏筛选法的拓展,可以借鉴素数筛选。

#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 1e6+100;
int prim[maxn]; void get_h_prim() {
for(int i=5;i<maxn;i+=4)
for(int j=5;j<maxn;j+=4) {
long long temp = i*j;
if(temp > maxn)
break;
if(prim[i] == prim[j] && prim[i] == 0)
prim[temp] = 1;
else
prim[temp] = -1;
} int cnt = 0;
for(int i=0;i<maxn;i++) {
if(prim[i] == 1)
cnt++;
prim[i] = cnt;
}
} int main() {
get_h_prim();
int n;
while(scanf("%d",&n) && n) {
printf("%d %d\n",n,prim[n]);
}
return 0;
}

POJ:3292-Semi-prime H-numbers(艾氏筛选法拓展)的更多相关文章

  1. poj 3292 H-素数问题 扩展艾氏筛选法

    题意:形似4n+1的被称作H-素数,两个H-素数相乘得到H-合成数.求h范围内的H-合成数个数 思路: h-素数                                            ...

  2. HDU 2136 Largest prime factor(查找素数,筛选法)

    题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...

  3. POJ 3292

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

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

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

  5. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

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

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

  7. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  8. ACM/ICPC 之 数论-素数筛选法 与 "打表"思路(POJ 1595)

    何为"打表"呢,说得简单点就是: 有时候与其重复运行同样的算法得出答案,还不如直接用算法把这组数据所有可能的答案都枚举出来存到一个足够大的容器中去-例如数组(打表),然后再输入数据 ...

  9. POJ 3978 Primes(素数筛选法)

    题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...

随机推荐

  1. url长度

    今天在测试Email Ticket的时候发现在进行Mark as Read/Unread操作时,请求是通过GET方式进行的.URL中列出了所有参与该操作的Ticket Id.于是,我想起GET请求是有 ...

  2. CSS 属性之中经常出现的百分比(转)

    CSS 的属性值中经常会出现百分比,搞清它们的基数是一件很重要的事情.我特意查看了一下 W3C 的 CSS2.1 标准,在此整理下: 百分比单位 乘以包含块的宽度 margin, padding, l ...

  3. 基于Python的开源人脸识别库:离线识别率高达99.38%

    项目地址:https://github.com/ageitgey/face_recognition#face-recognition 本文的模型使用了C++工具箱dlib基于深度学习的最新人脸识别方法 ...

  4. Active Directory域服务备份

    此篇介绍如何通过Windows Server Backup工具备份Active Directory域服务 AD 域系统状态 在域控制器上,系统状态通常包括以下内容,但所包含的数据实际上取决于服务器上安 ...

  5. 微信小程序之性能优化

    如果做前端仅仅停留在编码和实现业务功能上面,可能进步速度会有些慢,但是如果经历了对页面的性能优化之后而且有所成绩的话那就不同了,因为你对他背后的机制进行了研究,才能做好性能优化. 做微信小程序也是一样 ...

  6. git/github初级运用自如(转自:虫师)

    注:本文来源于 虫师博客(http://www.cnblogs.com/fnng/archive/2012/01/07/2315685.html) ,内容详尽,真实有用. 另:发一个github使用教 ...

  7. maven项目右键快捷方式,然后点击Run As

    在某一个maven项目右键快捷方式,然后点击Run As就可以发现几个Maven的命令: 1.Maven Build: 这个命令用于编译Maven工程,执行命令后会在target文件夹中的classe ...

  8. CentOS 6\7修改主机名

    1.CentOS6修改主机名 1)临时修改主机名: 显示主机名: oracle@localhost:~$ hostname localhost 修改 oracle@localhost:~$ sudo ...

  9. RHEL/CentOS 6.x使用EPEL6与remi的yum源安装MySQL 5.5.x

    PS:如果既想获得 RHEL 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 FedoraProject 推出的 EPEL(Extra Packages for Ent ...

  10. 将xml转换成Json,数组,对象格式转换方法

    xml字符串:$simplexml 转换成Json格式:json_encode($simplexml) 转换成数组格式:json_decode(json_encode($simplexml),TRUE ...