結果
| 問題 |
No.301 サイコロで確率問題 (1)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-02-19 20:57:19 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,581 bytes |
| コンパイル時間 | 1,029 ms |
| コンパイル使用メモリ | 100,236 KB |
| 実行使用メモリ | 6,940 KB |
| 最終ジャッジ日時 | 2024-09-22 12:22:14 |
| 合計ジャッジ時間 | 2,877 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 2 |
ソースコード
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;
// 行列の積
template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
int a = x.size();
int b = x[0].size();
int c = y[0].size();
vector<vector<T> > z(a, vector<T>(c, 0));
for(int i=0; i<a; ++i){
for(int j=0; j<c; ++j){
for(int k=0; k<b; ++k){
z[i][j] += x[i][k] * y[k][j];
}
}
}
return z;
}
// 行列の累乗
template <class T>
vector<vector<T> > matrixPower(const vector<vector<T> >& x, long long k)
{
int n = x.size();
vector<vector<T> > y(n, vector<T>(n, 0));
for(int i=0; i<n; ++i)
y[i][i] = 1; // 積の単位元
vector<vector<T> > z = x;
while(k > 0){
if(k & 1)
y = matrixProduct(y, z);
z = matrixProduct(z, z);
k >>= 1;
}
return y;
}
int main()
{
vector<pair<double, double> > dp(7);
for(int i=1; i<=6; ++i){
for(int j=i-6; j<=i-1; ++j){
if(j < 0){
dp[i].first += 1.0 / 6.0;
dp[i].second += 1.0 / 6.0;
}
else{
dp[i].first += (dp[j].first + 1.0) / 6.0;
dp[i].second += dp[j].second / 6.0;
}
}
}
vector<vector<double> > a(7, vector<double>(7, 0.0));
vector<vector<double> > b(6, vector<double>(6, 0.0));
for(int i=0; i<6; ++i){
a[0][i] = 1.0 / 6.0;
b[0][i] = 1.0 / 6.0;
}
for(int i=0; i<5; ++i){
a[i+1][i] = 1.0;
b[i+1][i] = 1.0;
}
a[0][6] = 1.0;
a[6][6] = 1.0;
int t;
cin >> t;
while(--t >= 0){
long long n;
cin >> n;
if(n <= 6){
cout << dp[n].first / (1 - dp[n].second) << endl;
continue;
}
vector<vector<double> > c = matrixPower(a, n - 6);
vector<vector<double> > d = matrixPower(b, n - 6);
double x = 0.0;
double y = 0.0;
for(int i=0; i<6; ++i){
x += c[0][i] * dp[i+1].first;
y += d[0][i] * dp[i+1].second;
}
x += c[0][6];
double ans = x / (1 - y);
printf("%.12f\n", ans);
}
return 0;
}