版权声明:本文为博主原创文章,未经博主同意不得转载。
https://blog.csdn.net/u011328934/article/details/36644069
题目大意:H-number为4∗k+1(k为非负数),H-composites为因子中含有H-number(不包含自己本身)的数。反之久是H-prime,给定n。求有多少H-composites。
解题思路:首先用筛选法求出范围内的H-prime。然后枚举两个推断乘积是否在范围内。
#include#include const int maxn = 1e6+5;typedef long long ll;int v[maxn], prime[maxn], cp;void primeHtable(int n) { cp = 0; memset(v, 0, sizeof(v)); for (int i = 5; i < n; i += 4) { if (v[i]) continue; prime[cp++] = i; for (int j = i * 2; j < n; j += i) v[j] = 1; }}int solve (int n) { int ans = 0; memset(v, 0, sizeof(v)); for (int i = 0; prime[i] < n && i < cp; i++) { if ((ll)prime[i] * prime[i] > n) break; for (int j = i; prime[j] < n && j < cp; j++) { ll u = (ll)prime[i] * prime[j]; if (u > n) break; if (v[u]) continue; ans++; v[u] = 1; } } return ans;}int main () { primeHtable(maxn); int n; while (scanf("%d", &n) == 1 && n) { printf("%d %d\n", n, solve(n)); } return 0;}