You are viewing a single comment's thread. Return to all comments →
Java solution:
private static final long MOD = 1_000_000_007; public static int legoBlocks(int n, int m) { if (n == 1) { return m < 5 ? 1 : 0; } long[] permutation = new long[m + 1]; for (int i = 1; i <= m; i++) { if (i < 5) { permutation[i] = 1; for (int j = i - 1; j >= 1; j--) { permutation[i] += permutation[j]; permutation[i] %= MOD; } } else { permutation[i] = permutation[i - 1] + permutation[i - 2] + permutation[i - 3] + permutation[i - 4]; permutation[i] %= MOD; } } // long[] total = new long[m + 1]; for (int i = 1; i <= m; i++) { total[i] = 1; for (int j = 1; j <= n; j++) { total[i] *= permutation[i]; total[i] %= MOD; } } // long[] good = new long[m + 1]; good[1] = 1; long[] bad = new long[m + 1]; bad[1] = 0; for (int i = 2; i <= m; i++) { bad[i] = 0; for (int j = 1; j < i; j++) { bad[i] += good[j] * total[i - j]; bad[i] %= MOD; } good[i] = (total[i] - bad[i] + MOD) % MOD; } return (int)good[m]; }
For n,m from 1 to 7, printed test case(n for row, m for column):
n
m
Seems like cookies are disabled on this browser, please enable them to open this website
Lego Blocks
You are viewing a single comment's thread. Return to all comments →
Java solution:
For n,m from 1 to 7, printed test case(
n
for row,m
for column):