mr_dot_convict

J-Sushi_AtCoder_mr.convict

Jun 29th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ] "
  17. #define x        first
  18. #define y        second
  19. using ll = long long;
  20. using ff = long double;
  21. using pii = pair<int,int>;
  22.  
  23. #define debug(args...) { \
  24. /* WARNING : do NOT compile this debug func calls with following flags: // \
  25.  * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  26.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  27.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  28. }
  29. void err(istream_iterator<string> it) { it->empty();
  30.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  31. }
  32. template<typename T, typename... Args>
  33. void err(istream_iterator<string> it, T a, Args... args) {
  34.     cerr << fixed << setprecision(15) << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  35.     err(++it, args...);
  36. }
  37.  
  38. // let k = a + b + c
  39. // E(a,b,c) := Expected no. of trials required to reach (0,0,0) from (a,b,c)
  40. // E(a,b,c) = (n-k)/n * (1 + E(a,b,c)) + a/n * (1 + E(a-1,b,c)) + b/n * (1 + E(a+1,b-1,c)) + c/n * (1 + E(a,b+1,c-1))
  41. // hence, E(a,b,c) = n/k + a/k * E(a-1,b,c) + b/k * E(a+1,b-1,c) + c/k * E(a,b+1,c-1)
  42.  
  43. const int N = 304;
  44.  
  45. ff E[N][N][N];
  46. int n, cnt[3];
  47.  
  48. void solve() {
  49.    E[0][0][0] = 0;
  50.  
  51.    for (int c = 0; c <= n; ++c) {
  52.       for (int b = 0; b+c <= n; ++b) {
  53.          for (int a = 0; a+b+c <= n; ++a) {
  54.             if (a + b + c > n) continue;
  55.             if (!a && !b && !c) continue;
  56.  
  57.             int k = a + b + c;
  58.             ff repeat = 1.0*n/k;
  59.             ff p_a = 1.0*a/k, p_b = 1.0*b/k, p_c = 1.0*c/k;
  60.  
  61.             E[a][b][c] = repeat;
  62.  
  63.             if (a) E[a][b][c] += E[a-1][b][c] * p_a;
  64.             if (b) E[a][b][c] += E[a+1][b-1][c] * p_b;
  65.             if (c) E[a][b][c] += E[a][b+1][c-1] * p_c;
  66.          }
  67.       }
  68.    }
  69.    cout << E[cnt[0]][cnt[1]][cnt[2]] << '\n';
  70. }
  71.  
  72. signed main() {
  73.    IOS; PREC;
  74.    cin >> n;
  75.  
  76.    cnt[0] = cnt[1] = cnt[2] = 0;
  77.  
  78.    for (int i = 0, foo; i < n; ++i)
  79.       cin >> foo, cnt[foo-1]++;
  80.  
  81.    cerr << cnt[0] << ' ' << cnt[1] << ' ' << cnt[2] << '\n';
  82.  
  83.    solve();
  84.  
  85.    return EXIT_SUCCESS;
  86. }
Add Comment
Please, Sign In to add comment