#include <bits/stdc++.h> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } int p; int mySum(int a, int b) { a += b; if (a >= p) a -= p; return a; } int mySub(int a, int b) { a -= b; if (a < 0) a += p; return a; } int myMax(int a, int b) { return a > b ? a : b; } int main() { int n = nextInt(); int m = nextInt(); p = nextInt(); vector<int> v(m + 1); vector<int> vv(m + 1); vector<int> s(m + 1); for (int i=0; i<=m; ++i) v[i] = i; s[0] = 0; for (int k=2; k<=n; ++k) { for (int i=1; i<=m; ++i) s[i] = mySum(s[i-1], v[i]); int tmp = 0; for (int i=0; i<m; ++i) { vv[i + 1] = (1LL * (mySub(s[m], s[m - 1 - i])) * (1LL + i)) % p; tmp = mySum(tmp, s[i]); vv[i + 1] = mySub(vv[i + 1], tmp); } swap(v, vv); } int res = 0; for (int i=1; i<=m; ++i) res = mySum(res, v[i]); printf("%d\n", res); return 0; }
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 | #include <bits/stdc++.h> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } int p; int mySum(int a, int b) { a += b; if (a >= p) a -= p; return a; } int mySub(int a, int b) { a -= b; if (a < 0) a += p; return a; } int myMax(int a, int b) { return a > b ? a : b; } int main() { int n = nextInt(); int m = nextInt(); p = nextInt(); vector<int> v(m + 1); vector<int> vv(m + 1); vector<int> s(m + 1); for (int i=0; i<=m; ++i) v[i] = i; s[0] = 0; for (int k=2; k<=n; ++k) { for (int i=1; i<=m; ++i) s[i] = mySum(s[i-1], v[i]); int tmp = 0; for (int i=0; i<m; ++i) { vv[i + 1] = (1LL * (mySub(s[m], s[m - 1 - i])) * (1LL + i)) % p; tmp = mySum(tmp, s[i]); vv[i + 1] = mySub(vv[i + 1], tmp); } swap(v, vv); } int res = 0; for (int i=1; i<=m; ++i) res = mySum(res, v[i]); printf("%d\n", res); return 0; } |