#include <bits/stdc++.h> using namespace std; typedef pair<int,int> II; #define INFTY 2000000000 int main() { std::ios::sync_with_stdio(0); int i, j, n, m, p; cin >> n >> m >> p; int l = 0; vector<int> left, right; vector<II> tl, tr; for (i=0; i<m; i++) for (j=i; j<m; j++) { left.push_back(i); right.push_back(j); tl.push_back(II(i,l)); tr.push_back(II(j,l)); l++; } sort(tr.begin(), tr.end()); /* for (auto &e : tl) cerr << "(" << left[e.second] << " " << right[e.second] << "), "; cerr << "\n"; for (auto &e : tr) cerr << "(" << left[e.second] << " " << right[e.second] << "), "; cerr << "\n"; */ vector<int> c(l,1), newc(l,0); vector<int> sumpref_l(l), sumpref_r(l); for (int k=1; k<n; k++) { sumpref_l[0] = c[0]; for (j=1; j<l; j++) sumpref_l[j] = (sumpref_l[j-1] + c[j]) % p; sumpref_r[0] = c[tr[0].second]; for (j=1; j<l; j++) sumpref_r[j] = (sumpref_r[j-1] + c[tr[j].second]) % p; /* cerr << "k=" << k << "\n"; cerr << "c: "; for (auto &e : c) cerr << e << " "; cerr << "\n"; cerr << "sumpref_l: "; for (auto &e : sumpref_l) cerr << e << " "; cerr << "\n"; cerr << "sumpref_r: "; for (auto &e : sumpref_r) cerr << e << " "; cerr << "\n"; */ for (i=0; i<l; i++) { auto upper = upper_bound(tl.begin(), tl.end(), II(right[i],INFTY)); upper--; auto lower = lower_bound(tr.begin(), tr.end(), II(left[i],-INFTY)); lower--; newc[i] = ((upper-tl.begin() >= 0 ? sumpref_l[upper-tl.begin()] : 0) - (lower-tr.begin() >= 0 ? sumpref_r[lower-tr.begin()] : 0)) % p; /* cerr << "i=" << i << "; ("<< left[i] << " " << right[i] << "); "; cerr << "upper=" << (upper-tl.begin()) << "; lower=" << (lower-tr.begin()) << "; newc[i] = " << newc[i] << "\n"; */ } c.swap(newc); } int res = 0; for (auto &e : c) res = (res + e) % p; cout << (p + res) % p << "\n"; 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 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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> II; #define INFTY 2000000000 int main() { std::ios::sync_with_stdio(0); int i, j, n, m, p; cin >> n >> m >> p; int l = 0; vector<int> left, right; vector<II> tl, tr; for (i=0; i<m; i++) for (j=i; j<m; j++) { left.push_back(i); right.push_back(j); tl.push_back(II(i,l)); tr.push_back(II(j,l)); l++; } sort(tr.begin(), tr.end()); /* for (auto &e : tl) cerr << "(" << left[e.second] << " " << right[e.second] << "), "; cerr << "\n"; for (auto &e : tr) cerr << "(" << left[e.second] << " " << right[e.second] << "), "; cerr << "\n"; */ vector<int> c(l,1), newc(l,0); vector<int> sumpref_l(l), sumpref_r(l); for (int k=1; k<n; k++) { sumpref_l[0] = c[0]; for (j=1; j<l; j++) sumpref_l[j] = (sumpref_l[j-1] + c[j]) % p; sumpref_r[0] = c[tr[0].second]; for (j=1; j<l; j++) sumpref_r[j] = (sumpref_r[j-1] + c[tr[j].second]) % p; /* cerr << "k=" << k << "\n"; cerr << "c: "; for (auto &e : c) cerr << e << " "; cerr << "\n"; cerr << "sumpref_l: "; for (auto &e : sumpref_l) cerr << e << " "; cerr << "\n"; cerr << "sumpref_r: "; for (auto &e : sumpref_r) cerr << e << " "; cerr << "\n"; */ for (i=0; i<l; i++) { auto upper = upper_bound(tl.begin(), tl.end(), II(right[i],INFTY)); upper--; auto lower = lower_bound(tr.begin(), tr.end(), II(left[i],-INFTY)); lower--; newc[i] = ((upper-tl.begin() >= 0 ? sumpref_l[upper-tl.begin()] : 0) - (lower-tr.begin() >= 0 ? sumpref_r[lower-tr.begin()] : 0)) % p; /* cerr << "i=" << i << "; ("<< left[i] << " " << right[i] << "); "; cerr << "upper=" << (upper-tl.begin()) << "; lower=" << (lower-tr.begin()) << "; newc[i] = " << newc[i] << "\n"; */ } c.swap(newc); } int res = 0; for (auto &e : c) res = (res + e) % p; cout << (p + res) % p << "\n"; return 0; } |