#include <iostream> using namespace std; long long dp[1005][1005]; long long dp2[1005][1005]; int n, m, p; long long calc(int l, int i) { long long ans = 0; for (int s = 1; s <=m; s++) { ans += dp[s][min(i + l - 1, m - s + 1)] - dp[s][max (1, i - s + 1) - 1]; if (ans < 0) ans += p; if (ans >= p) ans -= p; /*for (int k = max (1, i - s + 1); k <= i + l - 1; k++) { ans += dp[s][k]; if (ans >= p) ans -= p; }*/ } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> p; for (int l = 1; l <= m; l++) { for (int i = 1; i + l - 1 <= m; i++) { dp[l][i] = dp[l][i - 1] + 1; dp2[l][i] = 1; } } //cout << dp[1][1] << dp[1][2] << dp[2][1] << dp[2][2] << "\n"; for (int j = 1; j < n; j++) { for (int l = 1; l <= m; l++) { for (int i = 1; i + l - 1 <= m; i++) dp2[l][i] = calc(l, i); } //cout << dp2[1][1] << dp2[1][2] << dp2[2][1] << dp2[2][2] << "\n"; for (int l = 1; l <= m; l++) { for (int i = 1; i + l - 1 <= m; i++) { dp[l][i] = dp[l][i -1] + dp2[l][i]; } } //cout << dp[1][1] << dp[1][2] << dp[2][1] << dp[2][2] << "\n"; } long long odp = 0; for (int l = 1; l <= m; l++) { for (int i = 1; i <= m; i++) { odp += dp2[l][i]; if (odp >= p) odp -= p; } } cout << odp; }
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 | #include <iostream> using namespace std; long long dp[1005][1005]; long long dp2[1005][1005]; int n, m, p; long long calc(int l, int i) { long long ans = 0; for (int s = 1; s <=m; s++) { ans += dp[s][min(i + l - 1, m - s + 1)] - dp[s][max (1, i - s + 1) - 1]; if (ans < 0) ans += p; if (ans >= p) ans -= p; /*for (int k = max (1, i - s + 1); k <= i + l - 1; k++) { ans += dp[s][k]; if (ans >= p) ans -= p; }*/ } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> p; for (int l = 1; l <= m; l++) { for (int i = 1; i + l - 1 <= m; i++) { dp[l][i] = dp[l][i - 1] + 1; dp2[l][i] = 1; } } //cout << dp[1][1] << dp[1][2] << dp[2][1] << dp[2][2] << "\n"; for (int j = 1; j < n; j++) { for (int l = 1; l <= m; l++) { for (int i = 1; i + l - 1 <= m; i++) dp2[l][i] = calc(l, i); } //cout << dp2[1][1] << dp2[1][2] << dp2[2][1] << dp2[2][2] << "\n"; for (int l = 1; l <= m; l++) { for (int i = 1; i + l - 1 <= m; i++) { dp[l][i] = dp[l][i -1] + dp2[l][i]; } } //cout << dp[1][1] << dp[1][2] << dp[2][1] << dp[2][2] << "\n"; } long long odp = 0; for (int l = 1; l <= m; l++) { for (int i = 1; i <= m; i++) { odp += dp2[l][i]; if (odp >= p) odp -= p; } } cout << odp; } |