通过这题学习了线性筛法素数打表的方法,题目本身更像数学思考题。
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> using namespace std; using LL = long long int; const int maxn = 1024000; bool check[maxn]; LL pri[maxn], tot; void init() { for (int i = 2; i < maxn; ++i){ if (!check[i]) pri[tot++] = i; for (int j = 0; j < tot; ++j){ if (pri[j] * i > maxn) break; check[pri[j]*i] = true; if (i % pri[j] == 0) break; } } } int main() { init(); int T; cin >> T; while (T--){ LL L, U, res = 0; cin >> L >> U; for (int i = 0; i < tot && pri[i] * pri[i] <= U; ++i){ LL cur = pri[i] * pri[i]; while (cur <= U){ if (cur >= L) ++res; cur *= pri[i]; } } cout << res << endl; } return 0; }