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. Android setUserVisibleHint-- fragment真正的onResume和onPause方法

    这个情况仅适合与多个fragment之间切换时统计,而非activity和fragment同时交互,因当时项目为首页4个fargment时长统计,因此适合,经下面网友评论指出,特在这里写出此问题,因最 ...

  2. 浏览器中使用calc不识别

    在使用css3中的calc运算函数时,发现浏览器不识别,当时代码是这样的 width:calc(100%-50px); 经过查询官网原来发现这里有个需要注意的地方就是在进行加减运算的时候,必须在运算符 ...

  3. April 16 2017 Week 16 Sunday

    Happiness is a way station between too much and too little. 幸福就是刚刚好. I don't want to talk about it a ...

  4. 【Tim Sweeney】Why C++ for Unreal 4?

    The first three generations of the Unreal Engine included a sandboxed scripting language, UnrealScri ...

  5. java的sleep方法详解

    java的sleep方法详解: sleep并不是永久占有CPU,没有那个线程能永久占用CPU.它是指在自己时间片内睡眠,而不是急着交出CPU.yield()就是自己愿意立即交出时间片.因此一个线程sl ...

  6. selenium添加cookie切换到不同环境

    背景:网站中需要切环境到预发布,在用谷歌浏览器可以使用工具,但是在selenium启动时,是不会带任何插件,向开发了解下,切换环境本质是添加cookie值,那么这个就简单了 1.使用selenium中 ...

  7. 2017.9.28 web设计简单的购物车应用案例--session的简单应用

    该购物过程是在session范围内完成的,需要使用session对象实现信息的共享 (1)购买“肉类”商品的页面 <%@ page language="java" impor ...

  8. AI-Info-Micron-Insight:5G、人工智能和即将到来的移动革命

    ylbtech-AI-Info-Micron-Insight:5G.人工智能和即将到来的移动革命 1.返回顶部 1. 5G.人工智能和即将到来的移动革命 人们都说自己的手机“智能”,但究竟有多智能?凡 ...

  9. Multigrid for Poisson’s Equation

    泊松方程如何用多重网格求解 来源:佐治亚理工学院 教授:Prof. Richard Vuduc 主页:http://vuduc.org/index.php 教授内容:http://vuduc.org/ ...

  10. requireJS的学习

    官方文档 http://www.requirejs.cn/ 参考链接 http://www.w3cschool.cc/w3cnote/requirejs-tutorial-1.html http:// ...