Files
python/GESP/Level5/GESP202403/20240302.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

51 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int primes[N], cnt;
bool st[N];
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i])
primes[cnt++] = i;
for (int j = 0; primes[j] * i <= n; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0)
break;
}
}
}
int getMx(int x) {
int mx = 0;
// 1、如果x是质数,那么它的最大质数因子就是它自己
if (!st[x])
return x;
// 2、枚举x的所有可能的质数小因子
for (int i = 0; primes[i] * primes[i] <= x; i++) { // 2*2*7=28
while (x % primes[i] == 0) { // primes[i]是小质因子
mx = max(mx, primes[i]); // 小质数因子也可能是最大质数因子,比如8=2*2*2
if (!st[x / primes[i]]) // 也可能大因子是质数
mx = max(mx, x / primes[i]); // 大质数因子就会成为最大质数因子
x /= primes[i];
}
}
return mx;
}
int main() {
int n, b;
cin >> n >> b;
get_primes(N);
for (int i = 1; i <= 20; i++)
cout << getMx(i) << " ";
int ans = 0;
for (int i = 1; i <= n; i++) {
if (getMx(i) <= b)
ans++;
}
cout << ans << endl;
return 0;
}