fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const double p_suc = 1.0/4;
  5. const double p_fail = 3.0/2000;
  6. const double p_any = p_suc + p_fail;
  7.  
  8. vector<double> chain(vector< vector<double> > A, vector<double> b) {
  9. while(accumulate(b.begin()+1, prev(b.end()), 0.0) > 1e-9){
  10. vector<double> c(b.size(), 0.0);
  11. for(int j=0;j<A.size();++j){
  12. for(int i=0;i<b.size();++i){
  13. c[j] += A[j][i] * b[i];
  14. }
  15. }
  16. b = c;
  17. }
  18. return b;
  19. }
  20.  
  21. double get_failprob(int n){
  22. vector<vector<double> > A(2*n+1, vector<double>(2*n+1, 0.0));
  23. A[0][0] = A[2*n].back() = 1.0;
  24. for(int i=1;i<2*n;++i){
  25. A[i-1][i] = p_fail / p_any;
  26. A[i+1][i] = p_suc / p_any;
  27. }
  28. vector<double> b(2*n+1, 0);
  29. b[n] = 1;
  30. auto ans = chain(A, b);
  31.  
  32. for(auto &e:ans) cerr << e << " "; cerr << "\n";
  33.  
  34. return ans[0];
  35. }
  36.  
  37. signed main()
  38. {
  39. int n = 5;
  40. vector<double> v(n);
  41. for(int i=1;i<n;++i){
  42. v[i] = get_failprob(i);
  43. }
  44. for(int i=1;i<n;++i){
  45. cout << i << " : " << setw(12) << v[i] << " " << 1000*(v[i]) << "\n";
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout #stderr 0s 15240KB
stdin
Standard input is empty
stdout
1 :   0.00596421 5.96421
2 :  3.59987e-05 0.0359987
3 :     2.16e-07 0.000216
4 :    1.296e-09 1.296e-06
stderr
0.00596421 0 0.994036 
3.59987e-05 0 2.34383e-10 0 0.999964 
2.16e-07 0 1.88805e-13 0 3.14674e-11 0 1 
1.296e-09 0 2.95343e-15 0 6.96135e-13 0 8.20396e-11 0 1