Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals), problem: (D) Singer House Solution In C/C++

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL long long
using namespace std;
const LL Maxn = 410;
const LL Mod = 1e9+7;
LL f[Maxn][Maxn], n;
void add(LL &x, LL y) { x = (x+y)%Mod; }
int main() {
LL i, j, k;
scanf(“%I64d”, &n);
f[n][0] = f[n][1] = 1;
for(i = n-1; i >= 1; i–){
for(j = 0; j <= i; j++){
for(k = 0; k <= j; k++){
add(f[i][j], f[i+1][k]*f[i+1][j-k]%Mod*(j*2+1)%Mod);
if(k != j) add(f[i][j], f[i+1][k]*f[i+1][j-1-k]%Mod);
add(f[i][j], f[i+1][k]*f[i+1][j+1-k]%Mod*k*(j+1-k)*2%Mod);
}
for(k = 2; k <= j+1; k++) add(f[i][j], f[i+1][k]*f[i+1][j+1-k]%Mod*k*(k-1)*2%Mod);
}
}
printf(“%I64d\n”, f[1][1]);
return 0;
}

(Visited 36 times, 1 visits today)

About the Author:

Leave A Comment