1 条题解

  • 0
    @ 2026-11-2 12:34:44

    报数游戏(Number Game) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    using namespace std;
    
    const int MAXV = 10000010;
    static bool bad[MAXV + 5];
    static int nxt_ok[MAXV + 5];
    
    bool has7(int x) {
        while (x > 0) {
            if (x % 10 == 7) return true;
            x /= 10;
        }
        return false;
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        for (int i = 1; i <= MAXV; ++i) {
            if (bad[i]) continue;
            if (has7(i)) {
                for (int j = i; j <= MAXV; j += i) bad[j] = 1;
            }
        }
        int last = MAXV + 1;
        for (int i = MAXV; i >= 1; --i) {
            nxt_ok[i] = last;
            if (!bad[i]) last = i;
        }
        int T;
        if (!(cin >> T)) return 0;
        while (T--) {
            int x;
            cin >> x;
            if (bad[x]) cout << -1 << "\n";
            else cout << nxt_ok[x] << "\n";
        }
        return 0;
    }
    
    • 1

    信息

    ID
    8599
    时间
    1000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者