# ZeroJudge - g015: 老鼠愛反悔 ### 題目連結:https://zerojudge.tw/ShowProblem?problemid=g015 ###### tags: `ZeroJudge` `數學` `機率` ```cpp= #include <iostream> #include <iomanip> #include <cmath> using namespace std; #define ERROR 1e-5 static const auto Initialize = [] { cin.sync_with_stdio(false); cin.tie(nullptr); return nullptr; }(); double FastTransitionMatrix(unsigned long long time, double success2Fail, double fail2Success) { double answer[2][2] = { { 1, 0 }, { 0, 1 } }, power[2][2] = { { 1 - success2Fail, fail2Success }, { success2Fail, 1 - fail2Success } }, buffer[2][2]; while (time) { if (time & 1) { for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { buffer[i][j] = 0; for (int k = 0; k < 2; ++k) buffer[i][j] += answer[i][k] * power[k][j]; } for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) if (abs(answer[i][j] - buffer[i][j]) >= ERROR) answer[i][j] = buffer[i][j]; } for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { buffer[i][j] = 0; for (int k = 0; k < 2; ++k) buffer[i][j] += power[i][k] * power[k][j]; } for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) if (abs(power[i][j] - buffer[i][j]) >= ERROR) power[i][j] = buffer[i][j]; time >>= 1; } return answer[0][0] - ERROR; } int main() { unsigned long long time; double success2Fail, fail2Success; while (cin >> time >> success2Fail >> fail2Success) cout << fixed << setprecision(2) << FastTransitionMatrix(time, success2Fail, fail2Success) << '\n'; } ```