题意:所有形如4n+1(n为非负整数)的数叫H数。定义1是唯一的单位H数,H素数是指本身不是1,且不能写成两个不是1的H数的乘积。H-半素数是指能写成两个H素数的乘积的H数(这两个数可以相同也可以不同)。输入一个H数h(h <=1000001),输出1~h之间有多少个H-半素数。
分析:
1、筛选法求H素数。
2、再枚举求H-半素数。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1e6 + 10; const int MAXT = 10000 + 10; using namespace std; vector<int> Hprime; int vis[MAXN]; void init(){ for(int i = 5; i < MAXN; i += 4){ if(vis[i]) continue; Hprime.push_back(i); for(int j = i * 2; j < MAXN; j += i){ vis[j] = 1; } } } int solve(int n){ int ans = 0; memset(vis, 0, sizeof vis); int len = Hprime.size(); for(int i = 0; i < len; ++i){ if(Hprime[i] >= n) break; if((LL)Hprime[i] * Hprime[i] > n) break; for(int j = i; j < len; ++j){ if(Hprime[j] >= n) break; LL tmp = (LL)Hprime[i] * Hprime[j]; if(tmp > n) break; if(vis[tmp]) continue; ++ans; vis[tmp] = 1; } } return ans; } int main(){ init(); int n; while(scanf("%d", &n) == 1){ if(!n) return 0; printf("%d %d\n", n, solve(n)); } return 0; }