【OpenJudge 191】【POJ 1189】钉子和小球

时间:2021-08-19 17:38:03

http://noi.openjudge.cn/ch0405/191/

http://poj.org/problem?id=1189

一开始忘了\(2^{50}\)没超long long差点写高精度QvQ

很基础的dp,我先假设有\(2^n\)个球,分开时就分一半,这样每次都能除开。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 53;
int in() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - 48;
return k * fh;
} char c[N][N];
ll f[N][N];
int n, m; int main() {
n = in(); m = in();
char ch;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= i; ++j) {
for(ch = getchar(); ch != '*' && ch != '.'; ch = getchar());
c[i][j] = ch;
}
f[1][1] = 1ll << n;
for(int i = 2; i <= n + 1; ++i)
for(int j = 1; j <= i; ++j) {
if (j - 1 > 0 && c[i - 1][j - 1] == '*')
f[i][j] += f[i - 1][j - 1] >> 1;
if (j < i && c[i - 1][j] == '*')
f[i][j] += f[i - 1][j] >> 1;
if (i > 2 && 1 <= j - 1 && j - 1 <= i - 2 && c[i - 2][j - 1] == '.')
f[i][j] += f[i - 2][j - 1];
}
ll fz = f[n + 1][m + 1], fm = 1ll << n;
while (fz % 2 == 0 && fm % 2 == 0)
fz >>= 1, fm >>= 1;
if (fz == 0) fm = 1;
printf("%I64d/%I64d\n", fz, fm);
return 0;
}