結果

問題 No.301 サイコロで確率問題 (1)
ユーザー pekempeypekempey
提出日時 2015-11-14 01:56:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 31 ms / 1,000 ms
コード長 1,932 bytes
コンパイル時間 1,197 ms
コンパイル使用メモリ 148,096 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 17:38:54
合計ジャッジ時間 1,921 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

typedef long double D;

const int maxdim = 8;
int dim;
typedef D mat[maxdim][maxdim];
mat M0, M1, M2, M3, M4;
mat Mp[80];

void matmul(mat A, mat B, mat res) {
    rep (i, dim) rep (j, dim) M0[i][j] = 0;
    rep (i, dim) rep (k, dim) rep (j, dim) {
        M0[i][j] += A[i][k] * B[k][j];
    }
    rep (i, dim) rep (j, dim) res[i][j] = M0[i][j];
}

void matpow(mat A, ll b, mat res) {
    rep (i, dim) rep (j, dim) M1[i][j] = A[i][j];
    rep (i, dim) rep (j, dim) M2[i][j] = i == j;
    int i = 1;
    while (b > 0) {
        if (b & 1) matmul(Mp[i], M2, M2);
        b >>= 1;
        i++;
    }
    rep (i, dim) rep (j, dim) res[i][j] = M2[i][j];
}
void companion(vector<D> a, mat res) {
    int n = a.size();
    rep (i, n - 1) res[i][i + 1] = 1;
    rep (i, n) res[n - 1][i] = a[i];
    res[n][1] = res[n][n] = 1;
}

int main() {
    int T;
    cin >> T;
    dim = 7;
    D p = (long double)1.0 / 6;
    companion(vector<D>(6, p), M4);
    rep (i, 7) rep (j, 7) Mp[1][i][j] = M4[i][j];
    rep (i, 1, 70) {
        matmul(Mp[i], Mp[i], Mp[i + 1]);
    }
    rep (i_, T) {
        ll N;
        cin >> N;
		if (N < 1000) {
	        matpow(M4, N + 4, M3);
	        D s = M3[6][4], t = M3[6][5];
	        D ans = t / (s - t * 5.0 / 6.0);
	        cout << fixed << setprecision(50) << ans << endl;
		} else {
        	cout << (N + 1) << ".66666666666666666666666" << endl;
        }
    }
    return 0;
}
0