POJ3292(素数筛选)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8706 | Accepted: 3809 |
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
思路:注意H数的域是模4余1的数。如9是H数,虽然在自然数范围内9不是素数但是在H数域内9是H-prime。
#include <iostream>
#include <string.h>
using namespace std;
const int MAXN=;
bool isHprime[MAXN];
int Hprime[MAXN],top;
int h[MAXN];
void sieve()
{
memset(isHprime,true,sizeof(isHprime));
for(int i=;i<MAXN;i+=)
{
if(isHprime[i])
{
Hprime[top++]=i;
for(int j=i+i;j<MAXN;j+=i)
{
if((j-)%==)
{
isHprime[j]=false;
}
}
}
}
for(int i=;i<top;i++)
{
if(Hprime[i]>) break;
for(int j=i;j<top;j++)
{
int mul=Hprime[i]*Hprime[j];
if(mul>=MAXN)
{
break;
}
h[mul]=;
}
}
for(int i=;i<MAXN;i++)
{
h[i]+=h[i-];
}
}
int main()
{
sieve();
int n;
while(cin>>n&&n!=)
{
cout<<n<<" "<<h[n]<<endl;
}
return ;
}
POJ3292(素数筛选)的更多相关文章
- 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...
- codeforces Soldier and Number Game(dp+素数筛选)
D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- php数组函数-array_key_exists()
array_key_exists()函数判断某个数组中是否存在指定的key,如果key存 在,则返回true,否则返回flase array_key_exists(key,array); key:必需 ...
- IOS中大文件拷贝算法
+ (void)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath { //每次读取数据大小 #define READ_SI ...
- linux 虚拟机在线添加新磁盘
在线添加磁盘,扩展LVM卷案例 一.添加硬盘,在线扫描出来 首先到虚拟机那里添加一块硬盘,注意必须是SCSI类型的硬盘. 扫描硬盘,不用重启操作系统的. echo "- - -" ...
- 使用iView时报"Parsing error: x-invalid-end-tag"错误的解决方案
一. 问题日志 二. 问题原因iView将标签渲染为原生html标签时,由于这些标签是自闭合的,所以有end标签会报错. 三. 解决方案修改配置文件,忽略该项检查: 根目录下 - .eslintrc. ...
- R语言学习笔记(1)
第一章:R语言介绍 一 R的使用 1 R是一种区分大小写的解释型语言.R语句由函数和赋值构成.R使用<-作为赋值符号.例如: x<-rnorm(5) 创建了一个名为x的向量对象,它包含5个 ...
- NLP-特征选择
文本分类之特征选择 1 研究背景 对于高纬度的分类问题,我们在分类之前一般会进行特征降维,特征降维的技术一般会有特征提取和特征选择.而对于文本分类问题,我们一般使用特征选择方法. 特征提取:PCA.线 ...
- JavaWeb -- 邮件收发
smtp协议: telnet smtp.qq.com 25 ehlo kevin auth login eGlhbmdqaWU1NUBxcS5jb20= a2V2aW5feGlhbmc1NQ== ma ...
- jquery jsonp模版
$.ajax({ dataType: "jsonp", url: "http://www.b.com/b.php", jsonp: "callback ...
- SSIS的控制流之For循环容器
SSIS包由一个控制流以及一个或多个数据流(可选)组成.下面的关系图显示具有一个容器和六项任务的控制流. 这些任务中有五项定义于包级别,还有一项定义于容器级别.任务位于容器内.在控制流中的工具箱.我们 ...
- ICollectionView
引自:http://www.cnblogs.com/Joetao/articles/2168577.html ICollectionView让MVVM更简单 (一)ICollectionView的 ...