POJ3292 Semi-prime H-numbers [数论,素数筛]

  题目传送门

Semi-prime H-numbers

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10871   Accepted: 4881

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

Source

Waterloo Local Contest, 2006.9.30



  分析:

  一道素数筛法的变式题。

  把素数筛法改一下,预处理出所有答案,然后直接输出每个答案就行了。

  Code:

//It is made by HolseLee on 2nd Sep 2018
//POJ3292
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;

const int N=1e6+7;
int n,ans[N],q[N],top;
bool no[N],yes[N];

int main()
{
    ios::sync_with_stdio(false);
    for(int i=5; i<N; i+=4) {
        if(no[i])continue;
        q[++top]=i;
        for(int j=5*i; j<N; j+=i*4) no[j]=1;
    }
    for(int i=1; i<=top; ++i)
    for(int j=1; j<=i && q[i]*q[j]<N; ++j)
    yes[q[i]*q[j]]=1;
    for(int i=1; i<N; ++i)
    ans[i]=ans[i-1]+yes[i];

    while(555) {
        cin>>n; if(!n) break;
        cout<<n<<" "<<ans[n]<<"\n";
    }
    return 0;
}

原文地址:https://www.cnblogs.com/cytus/p/9572886.html

时间: 2024-10-29 09:55:42

POJ3292 Semi-prime H-numbers [数论,素数筛]的相关文章

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11

素数筛(1) 埃氏筛法

其原理就是先将2-n之内的所有数存在一个数组里,初始化所有数全为素数,然后从2开始寻找,只要标记是素数便将他的所有倍数的标记都改为合数,依次类推.时间复杂度为O(nloglogn). 代码实现 1 void prime_table() 2 { 3 for(int i=2;(LL)i<=n;i++) prime[i]=1; 4 for(int i=2;(LL)i*i<=n;i++) 5 if(prime[i]) for(LL j=i*i;j<=n;j+=i) prime[j]=0; 6 }

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);

uva 10539 - Almost Prime Numbers(数论)

题目链接:uva 10539 - Almost Prime Numbers 题目大意:给出范围low~high,问说在这个范围内有多少个数满足n=pb,(p为素数). 解题思路:首先处理出1e6以内的素数,然后对于每个范围,用solve(high)?solve(low?1),solve(n)用来处理小于n的满足要求的数的个数.枚举素数,判断即可. #include <cstdio> #include <cstring> typedef long long ll; const int

POJ 3126 Prime Path (bfs+欧拉线性素数筛)

Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. - It is a matter of security to change such things every now

数论线性筛总结 (素数筛,欧拉函数筛,莫比乌斯函数筛,前n个数的约数个数筛)

线性筛 线性筛在数论中起着至关重要的作用,可以大大降低求解一些问题的时间复杂度,使用线性筛有个前提(除了素数筛)所求函数必须是数论上定义的积性函数,即对于正整数n的一个算术函数 f(n),若f(1)=1,且当a,b互质时f(ab)=f(a)f(b),在数论上就称它为积性函数,若a,b不互质也满足的话则称作完全积性函数,下面说明每个筛子是怎么筛的. 最基础的是素数筛,其它三个筛都是以素数筛为前提 素数筛 void get_prime() { int pnum = 0; for(int i = 2;

数论2:素数筛

埃氏筛 判断素数可通过试除小于\(\sqrt n\)的素数来实现,那么将其反过来,只要将\(<= \sqrt n\)的素数的倍数都删掉,那么就能得到一张\(<=n\)的素数表,\(O(n\lg \lg n)\) // 需划掉合数,所以最初假设均为素数,即数组初始化为0 bool composite[maxn]; void generate(int n) { for(int i = 2, lim = sqrt(n); i <= lim; ++i) { if(composite[i]) co

数论-素数

    素数定义: 质数(prime number)又称素数,有无限个.质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数,这样的数称为质数.例 子:2.3.5.7.11.13.17.19. 素数一些性质: 质数p的约数只有两个:1和p: 任一大于1的自然数,要么本身是质数,要么可以分解为几个质数之积,这种分解是唯一的: 一个偶数可以写成两个合数之和,其中每一个合数都最多只有9个质因数: 一个偶数必定可以写成一个质数加上一个合成数,其中合数的因子个数有上界: 素数应用: 数学上来看,

POJ 2262-Goldbach&#39;s Conjecture(素数筛)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39435   Accepted: 15119 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2635 The Embarrassed Cryptographer (同余线性方程+素数筛)

题目地址:POJ 2635 先用素数筛把10^6万以内素数筛出来.然后把输入的那个大数转化成数组,并且每三位存成一个数,这样可以节约内存和时间,然后利用同余线性的原理,对那个小整数以内的所有素数枚举,然后判断是否整除,找到最小的能被整除的. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i