結果

問題 No.2829 GCD Divination
ユーザー ecottea
提出日時 2024-08-02 21:51:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 14,878 bytes
コンパイル時間 4,843 ms
コンパイル使用メモリ 262,708 KB
最終ジャッジ日時 2025-02-23 20:00:18
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#ifndef HIDDEN_IN_VS //
//
#define _CRT_SECURE_NO_WARNINGS
//
#include <bits/stdc++.h>
using namespace std;
//
using ll = long long; using ull = unsigned long long; // -2^63 2^63 = 9 * 10^18int -2^31 2^31 = 2 * 10^9
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
//
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
//
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
//
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 n-1
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s t
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s t
#define repe(v, a) for(const auto& v : (a)) // a
#define repea(v, a) for(auto& v : (a)) // a
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} //
#define EXIT(a) {cout << (a) << endl; exit(0);} //
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) //
//
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // true
    
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // true
    
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // mod
//
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif //
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = static_modint<999676999>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // Visual Studio
#include "local.hpp"
#else // gcc
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE MLE
#endif
//
/*
* Div_mul_transform<T>(int n) : O(n log(log n))
* n
*
* divisor_zeta(vT& a) : O(n log(log n))
* A[i] = Σ_(j | i) a[j] A
*
* divisor_mobius(vT& A) : O(n log(log n))
* A[i] = Σ_(j | i) a[j] a
*
* vT lcm_convolution(vT a, vT b) : O(n log(log n))
* c[k] = Σ_(LCM(i, j) = k) a[i] b[j] c
* c[n]
*
* multiple_zeta(vT& a) : O(n log(log n))
* A[i] = Σ_(i | j) a[j] A
*
* multiple_mobius(vT& A) : O(n log(log n))
* A[i] = Σ_(i | j) a[j] a
*
* vT gcd_convolution(vT a, vT b) : O(n log(log n))
* c[k] = Σ_(GCD(i, j) = k) a[i] b[j] c
*
* 1-indexed a[0], b[0] 使
*/
template <typename T>
class Div_mul_transform {
// : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5
vi ps; //
public:
// n
Div_mul_transform(int n) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
// is_prime[i] : i
vb is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
int i = 2;
// √n i
for (; i <= n / i; i++) if (is_prime[i]) {
ps.push_back(i);
for (int j = i * i; j <= n; j += i) is_prime[j] = false;
}
// √n i
for (; i <= n; i++) if (is_prime[i]) ps.push_back(i);
}
Div_mul_transform() {}
// A[i] = Σ_(j | i) a[j] A
void divisor_zeta(vector<T>& a) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
//n = 8
// A[1] = a[1]
// A[2] = a[1] + a[2]
// A[3] = a[1] + a[3]
// A[4] = a[1] + a[2] + a[4]
// A[5] = a[1] + a[5]
// A[6] = a[1] + a[2] + a[3] + a[6]
// A[7] = a[1] + a[7]
// A[8] = a[1] + a[2] + a[4] + a[8]
//
// a[1..n] α(s) = Σ_i a[i] i^(-s)
// α(s) ζ(s) = Σ_i i^(-s)
int n = sz(a) - 1;
//
repe(p, ps) repi(i, 1, n / p) a[p * i] += a[i];
}
// A[i] = Σ_(j | i) a[j] a
void divisor_mobius(vector<T>& A) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
//n = 8
// a[1] = A[1]
// a[2] = -A[1] + A[2]
// a[3] = -A[1] + A[3]
// a[4] = - A[2] + A[4]
// a[5] = -A[1] + A[5]
// a[6] = A[1] - A[2] - A[3] + A[6]
// a[7] = -A[1] + A[7]
// a[8] = - A[4] + A[8]
int n = sz(A) - 1;
//
repe(p, ps) repir(i, n / p, 1) A[p * i] -= A[i];
}
// c[k] = Σ_(LCM(i, j) = k) a[i] b[j] c
vector<T> lcm_convolution(vector<T> a, vector<T> b) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
int n = sz(a) - 1;
// max LCM max
divisor_zeta(a); divisor_zeta(b);
repi(i, 1, n) a[i] *= b[i];
divisor_mobius(a);
return a;
}
// A[i] = Σ_(i | j) a[j] A
void multiple_zeta(vector<T>& a) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
//n = 8
// A[1] = a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8]
// A[2] = a[2] + a[4] + a[6] + a[8]
// A[3] = a[3] + a[6]
// A[4] = a[4] + a[8]
// A[5] = a[5]
// A[6] = a[6]
// A[7] = a[7]
// A[8] = a[8]
//
// a[1..n] α(s) = Σ_i a[i] i^(-s)
// α(s) ζ(-s) = Σ_i i^s
int n = sz(a) - 1;
//
repe(p, ps) repir(i, n / p, 1) a[i] += a[p * i];
}
// A[i] = Σ_(i | j) a[j] a
void multiple_mobius(vector<T>& A) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
//n = 8
// a[1] = A[1] - A[2] - A[3] - A[5] + A[6] - a[7]
// a[2] = A[2] - A[4] - A[6]
// a[3] = A[3] - A[6]
// a[4] = A[4] - A[8]
// a[5] = A[5]
// a[6] = A[6]
// a[7] = A[7]
// a[8] = A[8]
int n = sz(A) - 1;
//
repe(p, ps) repi(i, 1, n / p) A[i] -= A[p * i];
}
// c[k] = Σ_(GCD(i, j) = k) a[i] b[j] c
vector<T> gcd_convolution(vector<T> a, vector<T> b) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
int n = sz(a) - 1;
// min GCD min
multiple_zeta(a); multiple_zeta(b);
repi(i, 1, n) a[i] *= b[i];
multiple_mobius(a);
return a;
}
};
//O(n log(log n))
/*
* i∈[1..n] φ(i)
*
*
*/
vl euler_phi(int n) {
// : https://maspypy.com/%E6%95%B0%E5%AD%A6
        -%E7%95%B3%E3%81%BF%E8%BE%BC%E3%81%BF%E5%85%A5%E9%96%80%EF%BC%9Adirichlet%E7%A9%8D%E3%81%A8%E3%82%BC%E3%83%BC%E3%82%BF%E5%A4%89%E6%8F%9B%E3%8
        3%BB%E3%83%A1%E3%83%93%E3%82%A6
// verify : https://yukicoder.me/problems/no/2249
//
// i d GCD(i, x) = d x∈[1..i]
// x GCD(i/d, y) = 1 y∈[1..i/d] x = y d
// φ(i/d)
// d [1..i] i GCD
// i = Σ_(d|i) φ(i/d)
// ⇔ i = Σ_(d|i) φ(d)
// φ a[i] = i
vl a(n + 1);
repi(i, 1, n) a[i] = i;
// int
Div_mul_transform<ll> dt(n);
dt.divisor_mobius(a);
return a;
}
double TLE(int n) {
auto phi = euler_phi(n);
vd dp(n + 1);
dp[n] = 1;
repir(i, n - 1, 1) {
for (int j = i * 2; j <= n; j += i) {
dp[i] += dp[j] * phi[j / i] / (j - 1);
}
}
dump(dp);
double res = 0;
repi(i, 2, n) {
res += dp[i] * i / (i - 1);
}
return res;
}
//O(√n)
/*
* n pps pps
* pps[p] = d n p d
*/
template <class T>
map<T, int> factor_integer(T n) {
// verify : https://algo-method.com/tasks/457
map<T, int> pps;
for (T i = 2; i * i <= n; i++) {
int d = 0;
while (n % i == 0) {
d++;
n /= i;
}
if (d > 0) pps[i] = d;
}
if (n > 1) pps[n] = 1;
return pps;
}
//O(√n)
/*
* φ(n)
*
*
*/
ll euler_phi1(ll n) {
// verify : https://judge.yosupo.jp/problem/tetration_mod
// n pps
auto pps = factor_integer(n);
// φ(n)
ll res = 1;
repe(pp, pps) {
res *= (pp.first - 1) * powi(pp.first, pp.second - 1);
}
return res;
}
//O(√n)
/*
* n
*/
template <class T>
vector<T> divisors(T n) {
// verify : https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_fb
vector<T> ds;
if (n == 1) {
ds.push_back(1);
return ds;
}
T i = 1;
for (; i * i < n; i++) {
if (n % i == 0) {
ds.push_back(i);
ds.push_back(n / i);
}
}
if (i * i == n) ds.push_back(i);
sort(all(ds));
return ds;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
int n;
cin >> n;
dump(TLE(n)); dump("--------");
auto ds = divisors(n);
dump(ds);
// unordered_map<int, double> dp;
// dp[n] = 1;
int K = sz(ds);
vd dp(K);
dp[K - 1] = 1;
repir(k, K - 2, 0) {
repi(k2, k + 1, K - 1) {
if (ds[k2] % ds[k] == 0) {
int q = ds[k2] / ds[k];
dp[k] += dp[k2] * euler_phi1(q) / (ds[k2] - 1);
}
}
}
dump(dp);
double res = 0;
repi(k, 1, K - 1) {
res += dp[k] * ds[k] / (ds[k] - 1);
}
cout << res << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0