求助
查看原帖
求助
1013950
__youzimo2014__楼主2024/7/15 08:15

写了注释,求求大佬帮帮我。

#include <iostream>
#define MAXN 1000005
using namespace std;
bool vis[MAXN]; // 埃氏筛要用
int pre[MAXN], cnt[26]; // pre 数组存储2~MAXN里的所有质数
// cnt[i] 表示在 [2^i, 2^(i+1)) 范围内的质数个数。
void init() { // 埃氏筛和初始化 cnt[i]
    for (int i = 2; i * i <= MAXN; i++) { // 不用我多说了吧
        if (!vis[i]) {
            for (int j = i; i * j <= MAXN; j++) {
                vis[i * j] = true;
            }
        }
    }
    for (int i = 2; i <= MAXN; i++) {
        if (!vis[i]) pre[++pre[0]] = i;
    }
    for (int i = 0; i <= 24; i++) { // 枚举 2^i次方。
        for (int j = 1; j <= pre[0]; j++) {
            if (pre[j] >= (1 << (i+1))) break;
            if (pre[j] < (1 << i)) continue;
            cnt[i]++;
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    init();
    int T;
    cin >> T;
    while (T--) {
        int x;
        cin >> x;
        int ans = 0;
        for (int i = 24; i >= 0; i--) {
            if (x & (1 << i)) { // 很难说,这关系到题目的解法
                ans += cnt[i];
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

2024/7/15 08:15
加载中...