POJ 3292 Semi-prime H-numbers(筛法变形)

我只能说这个水筛法写了我两个小时。。没有爱了。

#pragma warning(disable:4996)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const long long N = 1000001;
long long H[300000];
int tol;
bool prime[300000];
bool semi[300000];
int ans[300000];

int main() {
	tol = 0;
	for (int i = 0; 4 * i + 1 <= N; i++) {
		H[tol++] = 4 * i + 1;
	}
	memset(prime, true, sizeof prime);
	prime[0] = false;
	
	for (int i = 1; i < tol; i++) {
		if (prime[i]) {
			int s = 1;
			long long j;
			for (j = H[s] * H[i]; s < tol&&j <= N; j = H[i] * H[s]) {
				prime[(j - 1) / 4] = false;
				s++;
			}
		}
	}

	memset(semi, false, sizeof semi);
	for (int i = 1; i < tol; i++) {
		if (prime[i]) {
			for (int j = 1; j < tol; j++) {
				if (prime[j]) {
					long long tmp = (long long)H[i] * H[j];
					if (tmp <= N) {
						semi[(tmp - 1) / 4] = true;
					}
					else break;
				}
			}
		}
	}

	memset(ans, 0, sizeof ans);
	for (int i = 1; i < tol; i++) {
		ans[i] = ans[i - 1];
		if (semi[i])ans[i]++;
	}
	
	int h;
	while (scanf("%d", &h)) {
		if (!h)break;
		int id = (h - 1) / 4;
		printf("%d %d\n", h, ans[id]);
	}
	return 0;
}


你可能感兴趣的:(POJ 3292 Semi-prime H-numbers(筛法变形))