結果
| 問題 |
No.2829 GCD Divination
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-06 16:58:45 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,380 ms / 2,000 ms |
| コード長 | 1,312 bytes |
| コンパイル時間 | 3,479 ms |
| コンパイル使用メモリ | 267,220 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-06 16:58:56 |
| 合計ジャッジ時間 | 10,730 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
std::map<ll, std::vector<ll>> divs;
void init(ll n) {
auto d = divisor(n);
for(int i = 0; i < (int)d.size(); i++) {
for(int j = 0; j <= i; j++) {
if(d[i] % d[j] == 0)divs[d[i]].push_back(d[j]);
}
}
}
double f(ll n) {
if(n == 1) return 0;
if(memo.find(n) != memo.end()) {
return memo[n];
}
auto d = divs[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;
init(n);
cout << std::setprecision(15) << f(n) << endl;
}