Submission #113084

# Submission time Handle Problem Language Result Execution time Memory
113084 2019-05-23T14:30:47 Z popovicirobert Tents (JOI18_tents) C++14
100 / 100
133 ms 35832 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const int MOD = (int) 1e9 + 7;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD) x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

vector <int> fact, invfact;

inline void prec(int n) {
    fact.resize(n + 1), invfact.resize(n + 1);

    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    for(int i = 0; i <= n; i++) {
        invfact[i] = lgput(fact[i], MOD - 2);
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, j, n, m;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> m;

    prec(n + m);

    vector < vector <int> > dp(n + 1, vector <int>(m + 1));

    for(i = 0; i <= n; i++) {
        dp[i][0] = 1;
    }
    for(i = 0; i <= m; i++) {
        dp[0][i] = 1;
    }

    for(i = 1; i <= n; i++) {
        for(j = 1; j <= m; j++) {
            dp[i][j] = dp[i - 1][j];
            dp[i][j] = (dp[i][j] + 4LL * j * dp[i - 1][j - 1]) % MOD;
            if(j >= 2) {
                dp[i][j] = (dp[i][j] + 1LL * comb(j, 2) * dp[i - 1][j - 2]) % MOD;
            }
            if(i >= 2) {
                dp[i][j] = (dp[i][j] + 1LL * j * (i - 1) * dp[i - 2][j - 1]) % MOD;
            }
        }
    }

    dp[n][m] += MOD - 1;
    mod(dp[n][m]);

    cout << dp[n][m];

    //cin.close();
    //cout.close();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 304 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 304 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 768 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 86 ms 22452 KB Output is correct
16 Correct 7 ms 1792 KB Output is correct
17 Correct 20 ms 5248 KB Output is correct
18 Correct 26 ms 6516 KB Output is correct
19 Correct 97 ms 26032 KB Output is correct
20 Correct 81 ms 20864 KB Output is correct
21 Correct 52 ms 13824 KB Output is correct
22 Correct 52 ms 13696 KB Output is correct
23 Correct 30 ms 7780 KB Output is correct
24 Correct 133 ms 35832 KB Output is correct
25 Correct 102 ms 26656 KB Output is correct
26 Correct 120 ms 30464 KB Output is correct
27 Correct 131 ms 34436 KB Output is correct