danielvitor23

Greek Casino

Jul 11th, 2024 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef ENABLE_DEBUG
  3.   #define DEBUG(x) std::cout << x << std::endl
  4. #else
  5.   #define DEBUG(x)
  6. #endif
  7. #define fi first
  8. #define se second
  9. #define pb push_back
  10. #define all(x) x.begin(),x.end()
  11. #define rall(x) x.rbegin(),x.rend()
  12. using namespace std;
  13. using ii = pair<int, int>;
  14. using i64 = long long;
  15. using ld = long double;
  16. const int INF = 0x3f3f3f3f;
  17. const i64 INFLL = 0x3f3f3f3f3f3f3f3f;
  18. const int N = 1e5;
  19.  
  20. vector<vector<int>> dv;
  21. vector<vector<int>> fac;
  22.  
  23. int main() {
  24.   cin.tie(0)->sync_with_stdio(0);
  25.  
  26.   dv.assign(N+1, vector<int>());
  27.   fac.assign(N+1, vector<int>());
  28.   vector<int> cnt(N+1, 0);
  29.  
  30.   for (int i = 1; i <= N; ++i) {
  31.     int l = i;
  32.     for (int j = 2; (i64)j * j <= l; ++j) {
  33.       while (l % j == 0) {
  34.         l /= j;
  35.         fac[i].pb(j);
  36.       }
  37.     }
  38.     if (l > 1) fac[i].pb(l);
  39.     for (int j = i; j <= N; j += i) {
  40.       dv[j].pb(i);
  41.     }
  42.   }
  43.   int n; cin >> n;
  44.  
  45.   vector<ld> a(n + 1, 0.0);
  46.   vector<int> b(n + 1);
  47.  
  48.   int sum = 0;
  49.   for (int i = 1; i <= n; ++i) {
  50.     cin >> b[i];
  51.     sum += b[i];
  52.   }
  53.  
  54.   for (int i = 1; i <= n; ++i) {
  55.     a[i] = (ld) 1.0 * b[i] / (ld) sum;
  56.   }
  57.  
  58.   vector<ld> dp(n + 1, 0.0);
  59.   for (int x = n; x >= 1; --x) {
  60.     vector<ii> caras;
  61.  
  62.     for (int y = x; y <= n; y += x) {
  63.       // x | y
  64.  
  65.       int f = 1;
  66.  
  67.       for (int d : fac[x]) {
  68.         ++cnt[d];
  69.       }
  70.  
  71.       int xx = x;
  72.  
  73.       int last = -1;
  74.       int counter = 0;
  75.       int prod = 1;
  76.  
  77.       for (int d : fac[y]) {
  78.         if (d == last) {
  79.           prod *= d;
  80.           ++counter;
  81.         } else {
  82.           if (last != -1 and counter > cnt[last]) {
  83.             f *= prod;
  84.             for (int i = 0; i < cnt[last]; ++i)
  85.               xx /= last;
  86.           }
  87.           prod = d;
  88.           counter = 1;
  89.         }
  90.         last = d;
  91.       }
  92.  
  93.       if (last != -1 and counter > cnt[last]) {
  94.         f *= prod;
  95.         for (int i = 0; i < cnt[last]; ++i)
  96.           xx /= last;
  97.       }
  98.  
  99.       for (int d : dv[xx]) if (d * f <= n) {
  100.         int z = d * f;
  101.         caras.pb({y, z});
  102.       } else {
  103.         break;
  104.       }
  105.  
  106.       for (int d : fac[x]) {
  107.         --cnt[d];
  108.       }
  109.     }
  110.  
  111.     ld s1 = 0;
  112.     ld ss = 0;
  113.     for (auto [z, j] : caras) {
  114.       if (z == x) {
  115.         ss += a[j];
  116.         s1 += a[j];
  117.       } else {
  118.         ss += (ld) a[j] * (1.0 + dp[z]);
  119.       }
  120.     }
  121.  
  122.     dp[x] = ss / ld(1.0 - s1);
  123.   }
  124.  
  125.   cout << fixed << setprecision(10) << dp[1] << '\n';
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment