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. 为什么要使用markdown?

    markdown是什么? markdown是一种标记语言,它不是编程语言 为什么要使用markdown? markdown可以使用任何文本编辑器编写和查看 markdown编写的文件可以转化为html ...

  2. [转]QT 4.8 静态库编译方法

    最最初踏上QT之路是受到了XiaomaGee的指点,相比于常规的窗口程序开发,QT有着以下特点: 1. 优良的跨平台特性(支持Win.Linux.Mac 不同的平台下只需重新编译即可使用) 2. 面向 ...

  3. IntelliJ、ReSharper 4折 加入慧都“惊喜惠”

    慧都2013岁末回馈惊喜不断!著名的软件开发公司JetBrains旗下所有产品加入"惊喜惠"活动环节, JAVA IDE——IntelliJ IDEA,.NET效率工具集——ReS ...

  4. Springboot开源项目实例整理

    https://www.imooc.com/article/67664 ---------------------------------------------------------------- ...

  5. Cocos2d-x v3.1项目创建(三)

    Cocos2d-x v3.1项目创建(三) Cocos2d-x官方为我们提供了用于创建.编译.运行和部署的一套命令行的工具集,也就是上篇文章中我们所提到的Cocos2d-Console,它位于我们的引 ...

  6. Struts2_用DomainModel接收参数

    用域模型接收参数 User类 package com.bjsxt.struts2.user.model; public class User { private String name; privat ...

  7. VS2015卸载方法

    VS2015卸载 直接再控制面板的卸载程序中找到 VS2015 的程序,邮件更改,安装程序会被打开,里面有三个选项包含卸载,点击卸载[记得在卸载前如果有打开过 VS 最好重启一下,重启后不要打开 VS ...

  8. 关于 no device found for connection ‘ System eth0′问题

    在Vmware上面安装CentOS,开机后,使用:service network restart时,会提示一下错误: Shutting down loopback interface:         ...

  9. BZOJ 2735: 世博会 主席树+切比雪夫距离转曼哈顿距离

    2735: 世博会 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 124  Solved: 51[Submit][Status][Discuss] D ...

  10. ELF文件格式与进程地址空间的联系

    http://blog.csdn.net/q_l_s/article/details/52597330 三.分析在fork产生新进程中ELF文件格式与进程地址空间的联系 1.进程的虚拟地址空间 每个程 ...