//42 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef uint32_t ul; typedef int32_t l; typedef uint64_t ull; typedef int64_t ll; template <typename T> using statistics_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const l INF = 1000000000 + 9; const l PRIME = 200003; const ll L_INF = 1000000000000000000LL + 7; const double EPS = 1e-5; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end() #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second const l MAXN = 1000005; l n, m, idx=1; ll MOD; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m >> MOD; ll **dp[2]; dp[0] = new ll*[m+1]; dp[1] = new ll*[m+1]; ll* acc = new ll[m+1]; ll* suffix_sum = new ll[m+1]; FORE(0, m, i) { dp[0][i] = new ll[m+1]; dp[1][i] = new ll[m+1]; } FORE(1, m, i) { FORE(i, m, j) { dp[0][i][j] = 1; acc[i] += 1; acc[j+1] -= 1; } suffix_sum[i] = m-i+1; } FORE(2, n, k) { FORE(1, m, i) FORE(i, m, j) dp[idx][i][j] = 0; ll score{0}; FORE(1, m, i) { score += acc[i]; dp[idx][i][i] = score; acc[i] = 0; } FORE(1, m, i) FORE(i+1, m, j) { dp[idx][i][j] = (dp[idx][i][j-1] + suffix_sum[j])%MOD; } if(k != n) { FORE(1, m, i) { suffix_sum[i] = 0; FORE(i, m, j) { acc[i] = (acc[i] + dp[idx][i][j])%MOD; acc[j+1] = ((acc[j+1] - dp[idx][i][j])%MOD + MOD)%MOD; suffix_sum[i] = (suffix_sum[i] + dp[idx][i][j])%MOD; } } } idx = (idx+1)%2; } ll res{0}; FORE(1, m, i) FORE(i, m, j) res = (res+dp[(idx-1+2)%2][i][j])%MOD; cout << res << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | //42 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef uint32_t ul; typedef int32_t l; typedef uint64_t ull; typedef int64_t ll; template <typename T> using statistics_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const l INF = 1000000000 + 9; const l PRIME = 200003; const ll L_INF = 1000000000000000000LL + 7; const double EPS = 1e-5; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end() #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second const l MAXN = 1000005; l n, m, idx=1; ll MOD; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m >> MOD; ll **dp[2]; dp[0] = new ll*[m+1]; dp[1] = new ll*[m+1]; ll* acc = new ll[m+1]; ll* suffix_sum = new ll[m+1]; FORE(0, m, i) { dp[0][i] = new ll[m+1]; dp[1][i] = new ll[m+1]; } FORE(1, m, i) { FORE(i, m, j) { dp[0][i][j] = 1; acc[i] += 1; acc[j+1] -= 1; } suffix_sum[i] = m-i+1; } FORE(2, n, k) { FORE(1, m, i) FORE(i, m, j) dp[idx][i][j] = 0; ll score{0}; FORE(1, m, i) { score += acc[i]; dp[idx][i][i] = score; acc[i] = 0; } FORE(1, m, i) FORE(i+1, m, j) { dp[idx][i][j] = (dp[idx][i][j-1] + suffix_sum[j])%MOD; } if(k != n) { FORE(1, m, i) { suffix_sum[i] = 0; FORE(i, m, j) { acc[i] = (acc[i] + dp[idx][i][j])%MOD; acc[j+1] = ((acc[j+1] - dp[idx][i][j])%MOD + MOD)%MOD; suffix_sum[i] = (suffix_sum[i] + dp[idx][i][j])%MOD; } } } idx = (idx+1)%2; } ll res{0}; FORE(1, m, i) FORE(i, m, j) res = (res+dp[(idx-1+2)%2][i][j])%MOD; cout << res << "\n"; } |