#include <bits/stdc++.h> #define ll long long #define st first #define nd second #define mk std::make_pair #define debug if(0) ll p; ll n, m; std::vector<std::vector<ll> > dp0; std::vector<std::vector<ll> > dp1; // sum1[i][m] = sum{1, m}(dp0[i][m]/m), dp[i][0] = 0 dla wszystkich i std::vector<std::vector<ll> > sum1; // sum2[i][m] = sum{1, m}( (dp0[i][m] + dp1[i][m]) * m), dp[i][0] = 0 dla wszystkich i std::vector<std::vector<ll> > sum2; // sum3[i][m] = sum{1, m}( dp0[i][m] + dp1[i][m] ), dp[i][0] = 0 dla wszystkich i std::vector<std::vector<ll> > sum3; // sum4[i][m] = sum{1, m}( sum3[i][m] ) std::vector<std::vector<ll> > sum4; void prepare(std::vector<std::vector<ll> > &v) { v.clear(); v.reserve(n+5); std::vector<ll> tmp; tmp.reserve(m+5); for (int i = 0; i <= m+3; i++) tmp.push_back(0); for (int i = 0; i <= n+3; i++) v.push_back(tmp); } void input() { std::cin >> n >> m >> p; // teraz wszystkie tablice musza miec wymiary n x m prepare(dp0); prepare(dp1); prepare(sum1); prepare(sum2); prepare(sum3); prepare(sum4); } void base_cases() { // dp0[1][x], dp0[2][x], dp1[1][x], dp1[2][x] dla kazdego x \in [1, m] // dp0[1][x] = x, dp1[1][x] = 0 for (ll x = 1; x <= m; x++) { dp0[1][x] = x; sum2[1][x] = (sum2[1][x-1] + ((dp0[1][x] * x)%p)) % p; sum3[1][x] = (sum3[1][x-1] + dp0[1][x]) % p; sum4[1][x] = (sum4[1][x-1] + sum3[1][x]) % p; debug std::cout << "sum4[1][" << x << "] = " << sum4[1][x] << "\n"; } // dp0[2][x] - >= x, x // dp0[2][x] = (m-x+1) * x^2 for (ll x = 1; x <= m; x++) { ll a = m-x+1; ll b = x * x; a %= p; b %= p; dp0[2][x] = (a*b) % p; sum1[2][x] = (sum1[2][x-1] + ((a*x)%p)) % p; } // dp1[2][x] - <x, x // dp1[2][x] = (x-1)^2 + (x-2)^2 + ... + 2^2 + 1^2 = [(x-1)x(2(x-1) + 1)]/6 for (ll x = 1; x <= m; x++) { ll a = x-1; ll b = x; ll c = 2*(x-1) + 1; if (a % 2 == 0) a /= 2; else b /= 2; if (a % 3 == 0) a /= 3; else if (b % 3 == 0) b /= 3; else c /= 3; ll tmp = (a * b) % p; tmp = (tmp * c) % p; dp1[2][x] = tmp; sum2[2][x] = (sum2[2][x-1] + ( ((dp0[2][x] + dp1[2][x]) * x) % p )) % p; sum3[2][x] = (sum3[2][x-1] + ( dp0[2][x] + dp1[2][x] )) % p; sum4[2][x] = (sum4[2][x-1] + sum3[2][x]) % p; } } void count_dp() { for (int i = 3; i <= n; i++) { // dp0[i][x] dla kazdego x for (ll x = 1; x <= m; x++) { // dp0[i][x] = A + B + C // A = [ sum{x' = x, m} [ dp0[i-1][x']/x' ] ] * x^2 = x^2 * [ sum1[i-1][m] - sum1[i-1][x-1] ] // B = [ sum{x'' = 1, x-1} [ (dp0[i-2][x''] + dp1[i-2][x'']) * x'' ] ] * (m-x+1) * x = (m-x+1) * x * [ sum2[i-2][x-1] ] // C = [ sum{x' = x, m} [ sum3[i-2][m'-1] ] - (m-x+1) * sum3[i-2][x-1] ] * x^2 = // = [ (sum4[i-2][m-1] - sum4[i-2][x-1] ) - (m-x+1) * sum3[i-1][x-1] ] * x^2 // A ll a = (x*x) % p; ll b = (sum1[i-1][m] - sum1[i-1][x-1]) % p; b = (b + p) % p; ll A = (a * b) % p; ll AA = (x * b) % p; debug { std::cout << "licze dp0[" << i << "][" << x << "], konkretnie A:\n"; std::cout << "a = " << a << "\n"; std::cout << "b = " << b << "\n"; std::cout << "A = " << A << "\n"; } // B a = (m-x+1); b = x; ll c = sum2[i-2][x-1]; ll B = (a * b) % p; B = (B * c) % p; ll BB = (a * c) % p; debug std::cout << "B = " << B << "\n"; // C if (x == 1) a = (sum4[i-2][m-1] - sum4[i-2][x-1]) % p; else a = (sum4[i-2][m-1] - sum4[i-2][x-2]) % p; a = (a + p) % p; debug std::cout << "sum4[" << i-2 << "][" << m-1 << "] - sum4[" << i-2 << "][" << x-1 << "] = " << a << "\n"; b = (m-x+1); b = (b * sum3[i-2][x-1]) % p; debug std::cout << "(" << m-x+1 << ") * sum3[" << i-2 << "][" << x-1 << "] = " << b << "\n"; a = (a - b) % p; a = (a + p) % p; c = (x * x) % p; ll C = (a * c) % p; ll CC = (a * x) % p; debug std::cout << "C = " << C << "\n"; // dp0[i][x] dp0[i][x] = (A + B + C) % p; sum1[i][x] = (sum1[i][x-1] + (AA + BB + CC)) % p; } // dp1[i][x] dla kazdego x for (ll x = 1; x <= m; x++) { // dp1[i][x] = sum{x' < x} [ (dp0[i-1][x'] + dp1[i-1][x']) * x' ] = sum2[i-1][x-1] dp1[i][x] = sum2[i-1][x-1]; sum2[i][x] = (sum2[i][x-1] + ( (( dp0[i][x] + dp1[i][x] ) * x) % p) ) % p; sum3[i][x] = (sum3[i][x-1] + ( dp0[i][x] + dp1[i][x] )) % p; sum4[i][x] = (sum4[i][x-1] + sum3[i][x]) % p; } } } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); base_cases(); count_dp(); ll res = 0; for (int x = 1; x <= m; x++) { res = (res + dp0[n][x]) % p; res = (res + dp1[n][x]) % p; } debug { std::cout << "Wyniki dp:\n"; for (int i = 1; i <= n; i++) { for (int x = 1; x <= m; x++) { std::cout << "dp0[" << i << "][" << x << "] = " << dp0[i][x] << "\n"; std::cout << "dp1[" << i << "][" << x << "] = " << dp1[i][x] << "\n"; } } } std::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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | #include <bits/stdc++.h> #define ll long long #define st first #define nd second #define mk std::make_pair #define debug if(0) ll p; ll n, m; std::vector<std::vector<ll> > dp0; std::vector<std::vector<ll> > dp1; // sum1[i][m] = sum{1, m}(dp0[i][m]/m), dp[i][0] = 0 dla wszystkich i std::vector<std::vector<ll> > sum1; // sum2[i][m] = sum{1, m}( (dp0[i][m] + dp1[i][m]) * m), dp[i][0] = 0 dla wszystkich i std::vector<std::vector<ll> > sum2; // sum3[i][m] = sum{1, m}( dp0[i][m] + dp1[i][m] ), dp[i][0] = 0 dla wszystkich i std::vector<std::vector<ll> > sum3; // sum4[i][m] = sum{1, m}( sum3[i][m] ) std::vector<std::vector<ll> > sum4; void prepare(std::vector<std::vector<ll> > &v) { v.clear(); v.reserve(n+5); std::vector<ll> tmp; tmp.reserve(m+5); for (int i = 0; i <= m+3; i++) tmp.push_back(0); for (int i = 0; i <= n+3; i++) v.push_back(tmp); } void input() { std::cin >> n >> m >> p; // teraz wszystkie tablice musza miec wymiary n x m prepare(dp0); prepare(dp1); prepare(sum1); prepare(sum2); prepare(sum3); prepare(sum4); } void base_cases() { // dp0[1][x], dp0[2][x], dp1[1][x], dp1[2][x] dla kazdego x \in [1, m] // dp0[1][x] = x, dp1[1][x] = 0 for (ll x = 1; x <= m; x++) { dp0[1][x] = x; sum2[1][x] = (sum2[1][x-1] + ((dp0[1][x] * x)%p)) % p; sum3[1][x] = (sum3[1][x-1] + dp0[1][x]) % p; sum4[1][x] = (sum4[1][x-1] + sum3[1][x]) % p; debug std::cout << "sum4[1][" << x << "] = " << sum4[1][x] << "\n"; } // dp0[2][x] - >= x, x // dp0[2][x] = (m-x+1) * x^2 for (ll x = 1; x <= m; x++) { ll a = m-x+1; ll b = x * x; a %= p; b %= p; dp0[2][x] = (a*b) % p; sum1[2][x] = (sum1[2][x-1] + ((a*x)%p)) % p; } // dp1[2][x] - <x, x // dp1[2][x] = (x-1)^2 + (x-2)^2 + ... + 2^2 + 1^2 = [(x-1)x(2(x-1) + 1)]/6 for (ll x = 1; x <= m; x++) { ll a = x-1; ll b = x; ll c = 2*(x-1) + 1; if (a % 2 == 0) a /= 2; else b /= 2; if (a % 3 == 0) a /= 3; else if (b % 3 == 0) b /= 3; else c /= 3; ll tmp = (a * b) % p; tmp = (tmp * c) % p; dp1[2][x] = tmp; sum2[2][x] = (sum2[2][x-1] + ( ((dp0[2][x] + dp1[2][x]) * x) % p )) % p; sum3[2][x] = (sum3[2][x-1] + ( dp0[2][x] + dp1[2][x] )) % p; sum4[2][x] = (sum4[2][x-1] + sum3[2][x]) % p; } } void count_dp() { for (int i = 3; i <= n; i++) { // dp0[i][x] dla kazdego x for (ll x = 1; x <= m; x++) { // dp0[i][x] = A + B + C // A = [ sum{x' = x, m} [ dp0[i-1][x']/x' ] ] * x^2 = x^2 * [ sum1[i-1][m] - sum1[i-1][x-1] ] // B = [ sum{x'' = 1, x-1} [ (dp0[i-2][x''] + dp1[i-2][x'']) * x'' ] ] * (m-x+1) * x = (m-x+1) * x * [ sum2[i-2][x-1] ] // C = [ sum{x' = x, m} [ sum3[i-2][m'-1] ] - (m-x+1) * sum3[i-2][x-1] ] * x^2 = // = [ (sum4[i-2][m-1] - sum4[i-2][x-1] ) - (m-x+1) * sum3[i-1][x-1] ] * x^2 // A ll a = (x*x) % p; ll b = (sum1[i-1][m] - sum1[i-1][x-1]) % p; b = (b + p) % p; ll A = (a * b) % p; ll AA = (x * b) % p; debug { std::cout << "licze dp0[" << i << "][" << x << "], konkretnie A:\n"; std::cout << "a = " << a << "\n"; std::cout << "b = " << b << "\n"; std::cout << "A = " << A << "\n"; } // B a = (m-x+1); b = x; ll c = sum2[i-2][x-1]; ll B = (a * b) % p; B = (B * c) % p; ll BB = (a * c) % p; debug std::cout << "B = " << B << "\n"; // C if (x == 1) a = (sum4[i-2][m-1] - sum4[i-2][x-1]) % p; else a = (sum4[i-2][m-1] - sum4[i-2][x-2]) % p; a = (a + p) % p; debug std::cout << "sum4[" << i-2 << "][" << m-1 << "] - sum4[" << i-2 << "][" << x-1 << "] = " << a << "\n"; b = (m-x+1); b = (b * sum3[i-2][x-1]) % p; debug std::cout << "(" << m-x+1 << ") * sum3[" << i-2 << "][" << x-1 << "] = " << b << "\n"; a = (a - b) % p; a = (a + p) % p; c = (x * x) % p; ll C = (a * c) % p; ll CC = (a * x) % p; debug std::cout << "C = " << C << "\n"; // dp0[i][x] dp0[i][x] = (A + B + C) % p; sum1[i][x] = (sum1[i][x-1] + (AA + BB + CC)) % p; } // dp1[i][x] dla kazdego x for (ll x = 1; x <= m; x++) { // dp1[i][x] = sum{x' < x} [ (dp0[i-1][x'] + dp1[i-1][x']) * x' ] = sum2[i-1][x-1] dp1[i][x] = sum2[i-1][x-1]; sum2[i][x] = (sum2[i][x-1] + ( (( dp0[i][x] + dp1[i][x] ) * x) % p) ) % p; sum3[i][x] = (sum3[i][x-1] + ( dp0[i][x] + dp1[i][x] )) % p; sum4[i][x] = (sum4[i][x-1] + sum3[i][x]) % p; } } } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); base_cases(); count_dp(); ll res = 0; for (int x = 1; x <= m; x++) { res = (res + dp0[n][x]) % p; res = (res + dp1[n][x]) % p; } debug { std::cout << "Wyniki dp:\n"; for (int i = 1; i <= n; i++) { for (int x = 1; x <= m; x++) { std::cout << "dp0[" << i << "][" << x << "] = " << dp0[i][x] << "\n"; std::cout << "dp1[" << i << "][" << x << "] = " << dp1[i][x] << "\n"; } } } std::cout << res << "\n"; } |