1 条题解

  • 0
    @ 2025-11-23 21:53:42

    C++ :

    #include <iostream>
    #include "bit"
    #include "vector"
    #include "unordered_set"
    #include "set"
    #include "queue"
    #include "algorithm"
    #include "bitset"
    
    using namespace std;
    
    /*
    如果一个正整数的最大质因子不超过 B,则该正整数为 B-smooth 数。小杨同学想知道,对于给定的 n 和 B,有多少个不超过 n 的 B-smooth 数。
    1 if x <= B then x is a B-smooth number
    2 if x > B && x is prime number, then x is not a  B-smooth  number
    3 if x > B && x is product of prime number, then x is not a B-smooth  number
     */
    
    const int N = 1e6 + 1;
    bitset<N> s, t;
    int p[N], n, B, x = 0;
    
    int main() {
        cin >> n >> B;
        if (n <= B) {
            cout << n;
            return 0;
        }
    
        for (int i = 2; i <= n; i++) {
            if (!s.test(i)) p[x++] = i;
            for (int j = 0; j < x && p[j] * i <= n; j++) {
                s.set(p[j] * i);
                if (i % p[j] == 0) break;
            }
        }
    
        int y = upper_bound(p, p + x, B) - p;
    
        for (; y < x; y++) {
            for (int i = 1; p[y] * i <= n; i++) {
                t.set(p[y] * i);
                //if(i % p[y] == 0) break;
            }
        }
    
        int sum = 0;
        for(int i = B + 1; i <= n; i++){
            if(t.test(i)) sum++;
        }
    
        cout << n - sum;
    
    }
    
    

    信息

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