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. Eclipse:很不错的插件-devStyle,将你的eclipse变成idea风格

    使用教程  https://blog.csdn.net/stillonmyway/article/details/79109741 我使用使用的是护眼型的

  2. Anaconda上安装Tensorflow并在jupyter上运行

    博客原文地址:https://blog.csdn.net/index20001/article/details/73555182 https://www.cnblogs.com/HongjianChe ...

  3. Tensorflow ValueError: Protocol message RewriterConfig has no "layout_optimizer" field

    I changed models/research/object_detection/exporter.py line 71/72 from: rewrite_options = rewriter_c ...

  4. MSMQ学习笔记二——创建Message Queue队列

    一.创建Message Queue队列的主要流程 1.定义MQQUEUEPROPS 结构: 2.设置消息队列属性: 3.初始化MQQUEUEPROPS 结构: 4.调用MQCreateQueue创建队 ...

  5. oracle 11g expdp impdp详细使用方法

    11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法如下图: 二.oracle10g以后提供了expdp/impdp工具,同样可以解决此问题 1.导出expdp工具使用方法: ...

  6. BZOJ 3090: Coci2009 [podjela]

    3090: Coci2009 [podjela] Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 23  Solved: 17[Submit][Statu ...

  7. 数据结构与算法分析java——线性表2(ArrarList )

    ArrayList ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess, C ...

  8. MySQL入门很简单: 1 数据库概述

    1. 数据库概述 1.1 数据存储方式: 1)人工管理阶段 2)文件系统阶段: 文件系统通过文件的存储路径和文件名称访问文件中的数据 3)数据库系统阶段:Oracle, SQL Server, MyS ...

  9. SAP Netweaver和Hybris的数据库层

    ABAP Netweaver 在SAP基于Netweaver的ABAP应用里,应用开发人员用Open SQL访问数据库, 这些Open SQL会被Database interface(数据库接口)转换 ...

  10. c#转载的

    C#做项目时的一些经验分享 1.对于公用的类型定义,要单独抽取出来,放到单独的DLL中. 2.通过大量定义interface接口,来提高模块化程度,不同功能之间通过实现接口来面向接口编程. 3.如果项 ...