POJ:3292-Semi-prime H-numbers(艾氏筛选法拓展)
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
解题心得:
- 题意:
- 如果一个数是4*n+1,那么这个数是H-number
- 如果一个数是H-number且这个数是一个素数,那么这个数是H-prime
- 如果一个数是H-number且这个数的因子仅仅有两个H-prime那么这个数是H-semi-prime(不包括1和他本身)
- H-number剩下的数是H-composite
- 其实就是一个艾氏筛选法的拓展,可以借鉴素数筛选。
#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(艾氏筛选法拓展)的更多相关文章
- poj 3292 H-素数问题 扩展艾氏筛选法
题意:形似4n+1的被称作H-素数,两个H-素数相乘得到H-合成数.求h范围内的H-合成数个数 思路: h-素数 ...
- HDU 2136 Largest prime factor(查找素数,筛选法)
题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...
- POJ 3292
Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7059 Accepted: 3 ...
- 【POJ 3292】 Semi-prime H-numbers
[POJ 3292] Semi-prime H-numbers 打个表 题意是1 5 9 13...这样的4的n次方+1定义为H-numbers H-numbers中仅仅由1*自己这一种方式组成 即没 ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- POJ 3292 Semi-prime H-numbers (素数筛法变形)
题意:题目比较容易混淆,要搞清楚一点,这里面所有的定义都是在4×k+1(k>=0)这个封闭的集合而言的,不要跟我们常用的自然数集混淆. 题目要求我们计算 H-semi-primes, H-sem ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- ACM/ICPC 之 数论-素数筛选法 与 "打表"思路(POJ 1595)
何为"打表"呢,说得简单点就是: 有时候与其重复运行同样的算法得出答案,还不如直接用算法把这组数据所有可能的答案都枚举出来存到一个足够大的容器中去-例如数组(打表),然后再输入数据 ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
随机推荐
- vue-样式问题
问题: 今天在用vue开发单页面应用的时候,遇到一个问题,在A页面,直接刷新,页面的布局样式之类的是没有问题的,不过在B页面跳转到A页面,那么A页面有一些样式就不是预期的效果. 发现解决问题: 用调试 ...
- mongodb客户端操作常用命令
一启动mongodb数据库mongod --dbpath E:\mongo\data\db(这里些自己的mongodb数据库存放目录)二客户端操作1.显示数据库集合show dbs2.新建数据库use ...
- Struts_OGNL(Object Graph Navigation Language) 对象图导航语言
1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...
- [topcoder]TheGridDivTwo
http://community.topcoder.com/stat?c=problem_statement&pm=13628&rd=16278 标程是BFS,我用DFS,都可解. 这 ...
- Spring Cloud入门程序
本文手把手教你,做出第一个Spring Cloud程序,Eureka的简单入门使用 1.创建Spring Starter Project工程 点击next,添加项目名 2.引入Spring Cloud ...
- 笨办法学Python(二)
习题 2: 注释和井号 程序里的注释是很重要的.它们可以用自然语言告诉你某段代码的功能是什么.在你想要临时移除一段代码时,你还可以用注解的方式将这段代码临时禁用.接下来的练习将让你学会注释: #-- ...
- S/4HANA和CRM Fiori应用的搜索分页实现
在我的博客Paging Implementation in S/4HANA for Customer Management 我介绍了S/4HANA for Customer Management里采用 ...
- jQuery 遍历函数包括了用于筛选、查找和串联元素的方法。
jQuery 参考手册 - 遍历 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合中. .children() 获得匹配元素集合中每个 ...
- Matlab将矩阵保存为图像
imwrite(image,'image.jpg'); image为矩阵的内容 image.jpg为要保存的图像的名字
- DESCryptoServiceProvider 类加密解密
DESCryptoServiceProvider 点击查看介绍 加密解密辅助类:点击查看 私钥加密 定义:定义一个包装对象来访问加密服务提供程序 (CSP) 版本的数据加密标准 (DES) 算法. ...