結果
| 問題 |
No.959 tree and fire
|
| コンテスト | |
| ユーザー |
homesentinel
|
| 提出日時 | 2019-12-15 00:11:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 4,737 bytes |
| コンパイル時間 | 991 ms |
| コンパイル使用メモリ | 116,968 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-06 14:27:48 |
| 合計ジャッジ時間 | 2,437 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 54 |
ソースコード
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)
template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//edit
class Solve {
public:
Int N, M;
double p;
void init() {
cin >> N >> M >> p;
}
void init(Int N_, Int M_, double p_) {
N = N_;
M = M_;
p = p_;
}
double solve() {
if (N == 1 && M == 1) {
cout << p << endl;
return p;
}
if (N > M) swap(N, M);
if (N == 1) {
double ans = (M - 2) * pow(p, 3) + 2 * pow(p, 2);
cout << ans << endl;
return ans;
}
Int cnt_corner = 4;
// if (N == 1 && M == 1) cnt_corner = 1;
// else if (N == 1 || M == 1) cnt_corner = 2;
Int cnt_outer = max(0ll, N - 2) * 2 + max(0ll, M - 2) * 2;
Int cnt_inner = N * M - cnt_corner - cnt_outer;
double p_corner = pow(p, 3);
double p_outer = pow(p, 4);
double p_inner = pow(p, 5);
double ans = cnt_corner * p_corner + cnt_outer * p_outer + cnt_inner * p_inner;
cout << ans << endl;
return ans;
}
};
class Fool {
public:
Int N, M;
double p;
void init() {
cin >> N >> M >> p;
}
void init(Int N_, Int M_, double p_) {
N = N_;
M = M_;
p = p_;
}
double solve() {
// モンテカルロ法で解く
Int mon = (Int) 1e3;
Int total_score = 0;
rep(_, 0, mon) {
vector<vector<Int>> field(N + 2, vector<Int>(M + 2));
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= M; ++j) {
Int R = 1000000;
if (rand() % R < R * p) {
field[i][j] = 1;
} else {
field[i][j] = -1;
}
}
}
Int score = 0;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= M; ++j) {
bool flag = field[i][j] == 1;
int di[] = {1, 0, -1, 0};
int dj[] = {0, 1, 0, -1};
for (int k = 0; k < 4; ++k) {
if (field[i + di[k]][j + dj[k]] == -1) {
flag = false;
}
}
if (flag) score++;
}
}
total_score += score;
}
double ans = 1. * total_score / mon;
cout << ans << endl;
return ans;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// Solve().solve();
bool debug = true;
debug = false;
if (!debug) {
Solve solve;
solve.init();
solve.solve();
} else {
while (true) {
Int MIN_N = 1;
Int MAX_N = 100;
Int MIN_M = 1;
Int MAX_M = 100;
Int R = 10000000;
Int N = MIN_N + rand() % (MAX_N - MIN_N + 1);
Int M = MIN_M + rand() % (MAX_M - MIN_M + 1);
double p = 1. * (rand() % R) / R;
cout << "Input" << endl;
cout << N << " " << M << " " << p << endl;
cout << "Output" << endl;
Solve solve;
solve.init(N, M, p);
double sans = solve.solve();
Fool fool;
fool.init(N, M, p);
double fans = fool.solve();
double sotai = (sans - fans) / fans;
if (fabs(sotai) > 1e-1 && abs(sans - fans) > 1e-1) {
exit(-1);
}
}
}
return 0;
}
homesentinel