From b4f738178b241f70bb61cfc0b3a18169db3b5c10 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 1 Oct 2022 09:14:35 +0800 Subject: [PATCH] =?UTF-8?q?P4397=20[JLOI2014]=E8=81=AA=E6=98=8E=E7=9A=84?= =?UTF-8?q?=E7=87=95=E5=A7=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/88041741 --- Luogu/P4397/P4397.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Luogu/P4397/P4397.cpp diff --git a/Luogu/P4397/P4397.cpp b/Luogu/P4397/P4397.cpp new file mode 100644 index 00000000..1f9039ed --- /dev/null +++ b/Luogu/P4397/P4397.cpp @@ -0,0 +1,79 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +int cnt; +long long s, ans[500005]; +int p, primes[50005]; +bool not_prime[50005]; + +bool is_prime(long long x) { + if (x < 2) return false; + + for (int i = 2; static_cast(i) * i <= x; i++) { + if (x % i == 0) return false; + } + + return true; +} + +void dfs(long long now, int pos, long long sum) { + if (now == 1) { + ans[++cnt] = sum; + + return; + } + + if (is_prime(now - 1) && now > primes[pos]) { + ans[++cnt] = (now - 1) * sum; + } + + for (int i = pos; + static_cast(primes[i]) * primes[i] <= now; + i++) { + for (long long t = primes[i], + s = t + 1; + s <= now; + s += t *= primes[i]) { + if (now % s == 0) { + dfs(now / s, i + 1, sum * t); + } + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + for (int i = 2; i <= 50000; i++) { + if (!not_prime[i]) primes[++p] = i; + + for (int j = 1; i * primes[j] <= 50000; j++) { + not_prime[i * primes[j]] = true; + if (i % primes[j] == 0) break; + } + } + + while (cin >> s) { + cnt = 0; + + dfs(s, 1, 1); + std::sort(ans + 1, ans + cnt + 1); + + cout << cnt << endl; + + if (cnt) { + for (int i = 1; i <= cnt; i++) { + cout << ans[i] << ' '; + } + + cout << endl; + } + } + + return 0; +}