結果
問題 | No.2829 GCD Divination |
ユーザー |
|
提出日時 | 2024-09-06 16:53:15 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,437 ms / 2,000 ms |
コード長 | 1,062 bytes |
コンパイル時間 | 3,444 ms |
コンパイル使用メモリ | 261,740 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-06 16:53:27 |
合計ジャッジ時間 | 12,009 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 35 |
ソースコード
#include <bits/stdc++.h> #define cout std::cout #define cin std::cin #define endl '\n' using ll = long long; std::vector<ll> divisor(ll n) { std::vector<ll> ret; for(ll x = 1; x * x <= n; x++) { if(n % x == 0) ret.push_back(x); } for(ll i = (int)ret.size() - (ret.back() * ret.back() == n); i--; ) { ret.push_back(n / ret[i]); } return ret; } std::map<ll, double> memo; double f(ll n) { if(n == 1) return 0; if(memo.find(n) != memo.end()) { return memo[n]; } auto d = divisor(n); std::map<ll, ll> mp; for(int i = (int)d.size(); i--; ) { mp[d[i]] = n / d[i]; for(int j = 2 * d[i]; j <= n; j += d[i]) { if(mp.find(j) != mp.end()) mp[d[i]] -= mp[j]; } } double ret = 0; for(auto [di, cnt]: mp) { if(di != n) ret += (f(di) + 1) * cnt; } return memo[n] = double(ret + 1)/(n - 1); } int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; cout << std::setprecision(15) << f(n) << endl; }