POJ 3292 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 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
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
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 areH
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-numbers是一類4n+1的數(在這個問題裡,只有這類數字)
H-primes是一類因子只有1和它本身的H-numbers(類似於平常我們見到的質數)
H-semi-primes是一類數是兩個H-primes的乘積
打個表,然後輸出就可以
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int H_number[1000011];
void fun()
{
int i,j;
memset(H_number,0,sizeof(H_number));
for(i=5;i<=1000001;i+=4)
{
for(j=5;j<=1000001;j+=4)
{
int t = i*j;
if(t > 1000001)
break;
if(H_number[i] == 0 && H_number[j] == 0)
H_number[t] = 1;
else
H_number[t] = -1;
}
}
int cnt = 0;
for(i=1;i<=1000001;i++)
{
if(H_number[i] == 1)
{
cout << i << endl;
cnt++;
}
H_number[i] = cnt;
}
}
int main(void)
{
int n;
fun();
while(scanf("%d",&n)&&n)
{
printf("%d %d\n",n,H_number[n]);
}
return 0;
}
相關推薦
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
POJ:3292-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
初始 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
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
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
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-1316(類素數篩選法)
分析:如果按照定義對每個數進行判斷則需要O(N*N)的時間,但用類似篩素數的思想,我們可以mark出所有不滿足條件的,自然而然的就可以輸出滿足條件的了,時間複雜度是O(Nlog10(N)) #include <cstdio> const int MAX_N
POJ 2478 歐拉函數(歐拉篩法) HDU 1576 逆元求法
ios size col add 求和 。。 結果 names const 相關逆元求法,我之前有寫過,還有歐拉函數的求法,歐拉函數與逆元的關系 點擊POJ 2478又是一個打表的題目,一眼看出結果就是前n個歐拉函數值的和。這裏直接計算歐拉函數值求和會超時,看見多組數據。
【POJ 3292】 Semi-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
3292(Semi-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
(POJ3292)Semi-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 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
!POJ 2689 Prime Distance-卡時間-(素數篩法)
題意:給定兩個數l,r求這之間最近和最遠的兩個素數。資料範圍是整數的上限。r-l<=10^6 分析:總思路是把l和r間的素數全部找出來,然後遍歷一遍求最小距離和最大距離。用一個函式預處理資料範圍內的所有素數是不現實的,一來陣列不可r能開那麼大二來會超時。想想素數篩的思
poj 2689 Prime Distance(大數區間素數篩法)
題意:給定區間[L,R],求區間內距離最近的相鄰素數對和距離最遠的相鄰素數對,區間長度不超過1e6。 解題方案:用篩法求出[L,R]的所有素數——利用“合數n一定有小於或等於sqrt(n)的素數因子“這條性質,先預處理出sqrt(2,147,483,647)範圍內的所有素
洛谷P2640 神秘磁石(歐拉篩法)
for names pty AI 註意 坐標 class 長度 syn 題目背景 在遙遠的阿拉德大陸,有一種神秘的磁石,是由魔皇制作出來的, 題目描述 1.若給他一個一維坐標系,那麽他的磁力一定要在素數坐標的位置上才能發揮的最大(不管位置坐標的大小,只要是素數那麽磁力就一樣
C++中對於類來說標頭檔案(.h)和原始檔(.cpp)都應該寫些什麼 (類的常規建立)
寫類的宣告(包括類裡面的成員和方法的宣告)、函式原型、#define常數等,但一般來說不寫出具體的實現。 在寫標頭檔案時需要注意,在開頭和結尾處必須按照如下樣式加上預編譯語句(如下): #ifndef CIRCLE_H#define CIRCLE_H//你的程式碼寫在這裡#endif