結果

問題 No.75 回数の期待値の問題
ユーザー koba-e964koba-e964
提出日時 2015-06-08 22:09:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,660 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 62,532 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 20:00:32
合計ジャッジ時間 1,493 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 5 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
/*
 * Header requirement: vector
 */
template<class T = double>
class Matrix {
private:
  std::vector<std::vector<T> > mat;
  int n, m;
public:
  Matrix(int n, int m) : n(n), m(m), mat(n) {
    for (int i = 0; i < n; ++i) {
      mat[i] = std::vector<T>(m);
    }
  }
  std::vector<T> &operator[](int i) {
    return mat[i];
  }
  const std::vector<T> &operator[](int i) const {
    return mat[i];
  }
  /* Returns the rank of this matrix. */
  int eliminate(void) {
    int r = 0;
    for (int c = 0; c < m; ++c) {
      int r1 = -1;
      for (int i = r; i < n; ++i) {
	if (r1 == -1 || std::max(mat[i][c], -mat[i][c]) > std::max(mat[r1][c], -mat[r1][c])) {
	  r1 = i;
	  break;
	}
      }
      if (r1 == -1) {
	continue;
      }
      std::swap(mat[r], mat[r1]);
      for (int i = 0; i < m; i++) {
	if (i == c) {
	  continue;
	}
	mat[r][i] /= mat[r][c];
      }
      mat[r][c] = 1;
      for (int i = 0; i < n; ++i) {
	if (i != r) {
	  T fact = mat[i][c];
	  for (int j = c; j < m; ++j) {
	    mat[i][j] -= mat[r][j] * fact;
	  }
	}
      }
      ++r;
    }
    return r;
  }
};
#include <iostream>
#include <cassert>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PI;
const double EPS=1e-9;


int main(void){
  int k;
  cin >> k;
  Matrix<> mat(k, k + 1);
  REP(i, 0, k) {
    mat[i][i] = -1;
    mat[i][k] = 1;
    REP(j, 1, 7) {
      if (i + j <= k) {
	mat[i][i + j] += i + j == k ? 0 : 1.0 / 6;
      } else {
	mat[i][0] += 1.0 / 6;
      }
    }
  }
  int r = mat.eliminate();
  assert (r == k);
  cout << -mat[0][k] << endl;
}
0