結果

問題 No.76 回数の期待値で練習
ユーザー square1001square1001
提出日時 2016-11-05 15:04:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,309 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-08-16 13:53:51
合計ジャッジ時間 1,361 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <cassert>
typedef std::vector<double> matrix_base;
typedef std::vector<matrix_base> matrix;
matrix mul(const matrix& a, const matrix& b) {
	assert(a[0].size() == b.size());
	matrix ret(a.size(), matrix_base(b[0].size(), 0));
	for (int i = 0; i < a.size(); i++) {
		for (int j = 0; j < b[0].size(); j++) {
			for (int k = 0; k < b.size(); k++) ret[i][j] += a[i][k] * b[k][j];
		}
	}
	return ret;
}
matrix unit(int n) {
	matrix ret(n, matrix_base(n, 0));
	for (int i = 0; i < n; i++) ret[i][i] = 1;
	return ret;
}
matrix power(const matrix& a, int b) {
	assert(a.size() == a[0].size());
	matrix f = a, ret = unit(a.size());
	while (b) {
		if (b & 1) ret = mul(ret, f);
		f = mul(f, f);
		b >>= 1;
	}
	return ret;
}

#include <iomanip>
#include <iostream>
using namespace std;
int Q, N;
int main() {
	matrix s(7, matrix_base(1));
	s[0][0] = 35077.0 / 20736;
	s[1][0] = 2653.0 / 1728;
	s[2][0] = 181.0 / 144;
	s[3][0] = 13.0 / 12;
	s[4][0] = 1.0;
	s[5][0] = 0.0;
	s[6][0] = 1.0;
	matrix e(7, matrix_base(7));
	e[0] = matrix_base({ 1.0 / 12, 1.0 / 6, 1.0 / 4, 1.0 / 12, 1.0 / 4, 1.0 / 6, 1 });
	e[1][0] = e[2][1] = e[3][2] = e[4][3] = e[5][4] = e[6][6] = 1.0;
	cin >> Q;
	while (Q--) {
		cin >> N;
		cout << fixed << setprecision(15) << mul(power(e, N), s)[5][0] << endl;
	}
	return 0;
}
0