Submission #384520

# Submission time Handle Problem Language Result Execution time Memory
384520 2021-04-01T19:59:50 Z aryan12 Tents (JOI18_tents) C++17
100 / 100
611 ms 71404 KB
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 3010;
long long dp[N][N];
 
long long Recur(long long row, long long col) {
    if(row < 0 || col < 0)
        return 0;
    if(row == 0 || col == 0)
        return 1;
    if(dp[row][col] != -1)
        return dp[row][col];
    long long ans = 0, temp = 0;
 
    //the row is empty
    temp = Recur(row, col - 1);
    ans = temp;
 
    //1 tent facing anywhere
    temp = ((Recur(row - 1, col - 1) * row) % MOD * 4) % MOD;
    ans = (ans + temp) % MOD;
 
    //2 tents facing each other (row-wise)
    temp = (Recur(row - 2, col - 1) * (((row * (row - 1)) / 2) % MOD)) % MOD;
    ans = (ans + temp) % MOD;
 
    //2 tents facing each other (column-wise)
    temp = (Recur(row - 1, col - 2));
    long long x = row * (col - 1) % MOD;
    temp *= x;
    temp %= MOD;
    ans = (ans + temp) % MOD;
    dp[row][col] = ans;
    return ans;
}
 
void Solve() {
    long long n, m;
    cin >> n >> m;
    for(long long i = 0; i <= n; i++) {
        for(long long j = 0; j <= m; j++) {
            dp[i][j] = -1;
        }
    }
    long long ans = Recur(n, m) - 1;
    if(ans < 0)
        ans += MOD;
    cout << ans << endl;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    Solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 1132 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 1388 KB Output is correct
7 Correct 2 ms 876 KB Output is correct
8 Correct 2 ms 1516 KB Output is correct
9 Correct 1 ms 876 KB Output is correct
10 Correct 3 ms 1772 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 5 ms 2284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 1132 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 1388 KB Output is correct
7 Correct 2 ms 876 KB Output is correct
8 Correct 2 ms 1516 KB Output is correct
9 Correct 1 ms 876 KB Output is correct
10 Correct 3 ms 1772 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 5 ms 2284 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 6 ms 9708 KB Output is correct
15 Correct 323 ms 55532 KB Output is correct
16 Correct 17 ms 4204 KB Output is correct
17 Correct 61 ms 13036 KB Output is correct
18 Correct 96 ms 17280 KB Output is correct
19 Correct 396 ms 63212 KB Output is correct
20 Correct 344 ms 51180 KB Output is correct
21 Correct 214 ms 34028 KB Output is correct
22 Correct 195 ms 35564 KB Output is correct
23 Correct 38 ms 27116 KB Output is correct
24 Correct 611 ms 71404 KB Output is correct
25 Correct 443 ms 61700 KB Output is correct
26 Correct 512 ms 67052 KB Output is correct
27 Correct 590 ms 69484 KB Output is correct