1. 程式人生 > >POJ:3292-Semi-prime H-numbers(艾氏篩選法拓展)

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

解題心得:

  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;
}

相關推薦

POJ3292-Semi-prime H-numbers篩選拓展

Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10466 Accepted: 4665 Description

POJ 3292 Semi-prime H-numbers類素數篩

Description This problem is based on an exercise of David Hilbert, who pedagogically suggested tha

POJ 3292 Semi-prime H-numbers變形

我只能說這個水篩法寫了我兩個小時。。沒有愛了。 #pragma warning(disable:4996) #include <cstdio> #include <cstring&g

3292 Semi-prime H-numbers素數篩

先求所有的H-pirme,所有的H-prime兩兩相乘打表。 注: 1、H-numbers是所有除以4餘1的數,而H-prime則是隻能在這些H-numbers中分解因式只得到1*本身的數(1除外)。所以9也是H-prime(原來理解錯了)。 2、 H-semi-prim

POJ 3292 Semi-prime H-numbers

初始 table cin isp == padding edi tco turn 題意: H_Number 是一個比4的倍數多1的數,即4n + 1。H_Number 分為 H_Prime 和 H_Comosite。其中 H_Prime 僅能由1×h組成,而 H_Compo

UVA11005 Semi-prime H-numbers

Problem A: Semi-prime H-numbers This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 num

poj3292——Semi-prime H-numbers數論

Description This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 n

POJ 3292Semi-prime H-numbers

遍歷 ems const art cstring times article blank %d 【POJ 3292】 Semi-prime H-numbers 打個表 題意是1 5 9 13...這樣的4的n次方+1定義為H-numbers H

3292Semi-prime H-numbers素數篩的擴充套件

題目大意: 給定4n+1數(1、5、9、13、……)。將這些數分為unit(即為1)和prime(不是真正的素數),composite。規定一個semi-prime數為可為兩個prime數乘積。題目給定一個4n+1數,要判斷1~4n+1數之間(包含1和該4n+1數)的所有

SXYBT-0102H數Semi-prime H-numbers

一道很棒的數學題。 思路:先篩出“素數”,然後將“素數”兩兩相乘,枚舉出範圍以內的“合成數”,再計算字首和。最後直接輸出。 #include<iostream> #include<cstring> #include<cmath> #define INF 10000

POJ3292Semi-prime H-numbers

Semi-prime H-numbers Description This problem is based on an exercise of David Hilbert, who pedagogically suggested that one stud

POJ3292 Semi-prime H-numbers [數論,素數篩]

pos lib The bool sed out soft product -s   題目傳送門 Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Su

POJ3292 UVA11105 Semi-prime H-numbers【篩打表】

問題簡述:參見上述連結。 問題分析: H-number:4n+1的數,n>=0,例如1,5,9,13,17,21,......。 H-prime:H-number數並且其因子只有1和它本身。 H-semi-prime:兩個H-prime的乘積。 H-composit

POJ 3292

題意: H-number:4n+1 H-prime:H-number並且只有兩個H-number因子1和他本身 H-semi-number:為兩個H-number數的乘積 H-semi-prime:H-prime&&H-semi-number #inclu

初遇C#一個簡單的小程序圓形周長,面積計算器

編碼 雙精度 崩潰 輸入 面向對象 窗口 語句 readline 面向對象的語言 作為一個面向對象的語言,與用戶的交互很關鍵! 在此,我們可以先分析一下我們這個小程序要與用戶交互的內容:1.命名很重要,讓用戶看見這個程序就知道這個程序的作用。 2.當用戶打開這個程序時,提示

2.7.1 元素定位selenium消息框處理 alert、confirm、prompt

ttext def 總結篇 必須 tro -s 按鈕 默認 答案 來源:http://blog.csdn.net/cui_angel/article/details/7784211 http://www.cnblogs.com/tobecrazy/p/45

POJ 2482】 Stars in Your Window線段樹+離散化+掃描線

d+ opera algorithm ans som lov ble word wait 【POJ 2482】 Stars in Your Window(線段樹+離散化+掃描線) Time Limit: 1000MS M

plustraceset autotrace trace exp statSP2-0618、SP2-0611

sta exp right str this root vertica rwx echo 1、報錯:當前用戶不能使用autotrace獲得執行計劃 1 SQL> set autotrace trace exp stat; 2 3 SP2-0618: Canno

Bootstrap 4,“未捕獲錯誤Bootstrap工具提示需要Tetherhttp://github.hubspot.com/tether/

targe 出現 一個 bootstrap git bsp lib 錯誤 單獨 如果出現了這個錯誤,我想你是沒有引用tether文件,這在v4之前不需要單獨引入的。 https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js

總綱篇產品結構設計指導VI本博客指引章節

normal 定制化 watermark 設計 規範化 problems square span 博客 本章目的:搭建自己的產品結構設計konw-how體系,從零開始設計一個完整產品。 需知遠途即捷徑! (//作者的結構設計體系尚在搭建中,所有的文章都會定期進行