这道题的做法:分层,一层层递推计算,如果计算到有一层酒杯没有溢出则可以退出循环,下面所有酒杯必定为空。注意不用开很大的三维数组,两层二维数组足矣,在循环时用mod2的方法决定当前位置在三维数组的哪一层
#include <iostream>
#include <iomanip>
using namespace std;
double arr[2][400][400];
int main() {
freopen("B-large-practice.in","r",stdin);
freopen("output.txt","w",stdout);
int T,B,L,N;
bool overflow;
cin >> T;
for (int t = 1; t <= T; t++) {
cin >> B >> L >> N;
arr[0][0][0] = 750 * B;
overflow = B >= 1;
int l;
for (l = 1; l < L && overflow; l++) {
overflow = false;
memset(arr[l%2],0,400*400);
for (int i = 0; i < l; i++) {
for (int j = 0; j <= i; j++) {
if (arr[(l-1)%2][i][j] > 250) {
double tmp = (arr[(l-1)%2][i][j] - 250) / 3;
arr[l%2][i][j] += tmp;
arr[l%2][i+1][j] += tmp;
arr[l%2][i+1][j+1] += tmp;
}
}
}
for (int i = 0; i <= l; i++) {
for (int j = 0; j <= i; j++) {
if (arr[l%2][i][j] > 250) {
overflow = true;
}
}
}
}
if (l < L) {
cout << "Case #" << t << ": 0.0000000" << endl;
} else {
int i, j;
for (i = 1; i < N; N -= i, i++);
j = N;
cout << "Case #" << t << ": " << fixed << setprecision(7) << (arr[(L-1)%2][i-1][j-1] > 250.0 ? 250.0 : arr[(L-1)%2][i-1][j-1]) << endl;
}
}
return 0;
}