結果
問題 |
No.2829 GCD Divination
|
ユーザー |
![]() |
提出日時 | 2024-08-03 16:47:50 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 131 ms / 2,000 ms |
コード長 | 2,489 bytes |
コンパイル時間 | 3,187 ms |
コンパイル使用メモリ | 252,392 KB |
実行使用メモリ | 120,716 KB |
最終ジャッジ日時 | 2024-08-03 16:47:57 |
合計ジャッジ時間 | 6,293 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 35 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template <bool GETS_ONLY_PRIME> std::vector<int> prime_sieve(const int n) { std::vector<int> smallest_prime_factor(n + 1), prime; std::iota(smallest_prime_factor.begin(), smallest_prime_factor.end(), 0); for (int i = 2; i <= n; ++i) { if (smallest_prime_factor[i] == i) [[unlikely]] prime.emplace_back(i); for (const int p : prime) { if (i * p > n || p > smallest_prime_factor[i]) break; smallest_prime_factor[i * p] = p; } } return GETS_ONLY_PRIME ? prime : smallest_prime_factor; } struct Divisor { const std::vector<int> smallest_prime_factor; explicit Divisor(const int n) : smallest_prime_factor(prime_sieve<false>(n)) {} std::vector<int> query(int n) const { std::vector<int> res{1}; while (n > 1) { const int prime_factor = smallest_prime_factor[n], d = res.size(); int tmp = 1; for (; n % prime_factor == 0; n /= prime_factor) { tmp *= prime_factor; for (int i = 0; i < d; ++i) { res.emplace_back(res[i] * tmp); } } } // std::sort(res.begin(), res.end()); return res; } }; int main() { int n; cin >> n; const Divisor divisor(n); vector<double> dp(n + 1, 0); for (const int d : divisor.query(n)) { if (d == 1) continue; const vector<int> ds = divisor.query(d); const int l = ds.size(); vector<int> num(l, 0); for (int i = l - 1; i >= 0; --i) { num[i] = d / ds[i]; FOR(j, i + 1, l) { if (ds[j] % ds[i] == 0) num[i] -= num[j]; } } dp[d] = d; FOR(i, 1, l - 1) dp[d] += num[i] * dp[ds[i]]; dp[d] /= d - 1; } cout << dp[n] << '\n'; return 0; }