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 ...
随机推荐
- 关闭Selinux 命令
在nginx 配置文件中,新增location中的内容,完成后,web上403报错 方法:关闭Selinux即可. Follow below steps: 虚拟机服务器环境补充: # vim/etc/ ...
- 实现HTML格式的数据报表邮件
- linux系统内SAMBA共享问题
最近将项目迁移到了公司服务器上,以后客户端调试和服务端开发都要去链接这台服务器,但是开发就需要调试,也需要log信息,同一局域网内,如何链接服务器并随时查看服务器上的log信息呢? 今天搞了一下,把步 ...
- HDU 6096 AC自动机
n个字符串 m个询问 每个询问给出前后缀 并且不重合 问有多少个满足 m挺大 如果在线只能考虑logn的算法 官方题解:对n个串分别存正序倒序 分别按照字典序sort 每一个串就可以被化作一个点 那么 ...
- Ceilometer 数据库比较
Ceilometer和其他OpenStack项目相比,部署中比较不同的是Ceilometer可以选用多种不同的后台数据库来保存测量值和警告器. 目前Ceilometer支持的后台数据库及及其相应的配置 ...
- 根据图片名字在drawable中得到图片
int imageId = context.getResources().getIdentifier("图片的名字","drawable", "包名& ...
- 从性能的角度谈SQL Server聚集索引键的选择
简介 在SQL Server中,数据是按页进行存放的.而为表加上聚集索引后,SQL Server对于数据的查找就是按照聚集索引的列作为关键字进行了.因此对于聚集索引的选择对性能的影响就变得十分重要 ...
- css——被自己遗忘的css属性
[属性选择器]顾名思义,属性选择器可以根据属性是否存在或属性的值来寻找元素,因此能实现某些效果.例如当鼠标停留在一个具有title属性的元素上的时候,大多数浏览器会显示一个工具提示,可以根据这个特性来 ...
- mac 不再接受预览版提示
系统偏好设置 - App Store - 点“电脑已设置为接收预发布的软件更新种子”的“更改”按钮 - 点“不显示预发布更新”.
- uva 1025 A Spy int the Metro
https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...