From c92a7218b921d73288a25d360a05e09b3503bfde Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Mon, 27 Sep 2021 19:23:44 +0800 Subject: [PATCH] =?UTF-8?q?#1023.=20[NOIP2015=E5=9B=9B=E6=A0=A1=E8=81=94?= =?UTF-8?q?=E8=AE=ADDay8]=E8=B4=A8=E6=95=B0=E7=94=9F=E6=88=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/36746 --- S2OJ/1023/1023.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 S2OJ/1023/1023.cpp diff --git a/S2OJ/1023/1023.cpp b/S2OJ/1023/1023.cpp new file mode 100644 index 00000000..daca913b --- /dev/null +++ b/S2OJ/1023/1023.cpp @@ -0,0 +1,82 @@ +#pragma GCC optimize("Ofast") + +#include + +using namespace std; + +int t, n, m, phi[1000005], cnt, prime[1000005]; +bool vis[1000005]; + +void euler(int n) { + phi[1] = 1; + for (int i = 2; i <= n; i++) { + if (!vis[i]) { + phi[i] = i - 1; + prime[++cnt] = i; + } + for (int j = 1; j <= cnt; j++) { + if (1ll * i * prime[j] >= n) break; + vis[i * prime[j]] = true; + if (i % prime[j]) { + phi[i * prime[j]] = phi[i] * (prime[j] - 1); + } else { + phi[i * prime[j]] = phi[i] * prime[j]; + break; + } + } + } +} + +int find(int l, int r, int x) { + int res = 0; + while (l <= r) { + int mid = (l + r) >> 1; + if (prime[mid] >= x) { + res = mid; + r = mid - 1; + } else { + l = mid + 1; + } + } + return res; +} + +int main() { + std::ios::sync_with_stdio(false); + cin >> t; + euler(1000000); + while (t--) { + memset(vis, 0x00, sizeof(vis)); + cin >> m >> n; + if (n <= 1000000) { + int l = find(0, cnt, m); + for (int i = l; i <= cnt; i++) { + if (prime[i] > n) break; + cout << prime[i] << endl; + } + } else { + int l = m + (m & 1); + if (l == 2) l += 2; + for (int i = l; i <= n; i += 2) { + vis[i - m] = true; + } + int d = sqrt(n) + 1; + for (int i = 2; i <= cnt; i++) { + if (prime[i] > d) break; + int p = (m - 1) / prime[i] + 1; + p += !(p & 1); + if (p == 1) p += 2; + for (int j = p; j * prime[i] <= n; j++) { + vis[j * prime[i] - m] = true; + } + } + for (int i = m; i <= n; i++) { + if (!vis[i - m]) { + cout << i << '\n'; + } + } + } + cout << endl; + } + return 0; +}