#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif
#include <array>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
namespace {
void inc(int& out, int delta, int mod)
{
out += delta;
out %= mod;
if (out < 0) out += mod;
}
int mul(int a, int b, int mod)
{
return static_cast<long long>(a) * b % mod;
}
int solve(int n, int m, int mod)
{
array<vector<int>, 2> tab;
for (int c = 0; c < 2; ++c) {
tab[c].resize(m + 1);
}
int c = 0;
int f = 1;
tab[c][m] = 1;
for (int q = 0; q < n; ++q) {
int cur = 0;
for (int i = 1; i <= m; ++i) {
int res = tab[f][i - 1];
int tmp = tab[c][m] - tab[c][m - i];
inc(res, mul(tmp, i, mod), mod);
inc(res, cur, mod);
inc(cur, -tab[c][i], mod);
tab[f][i] = res;
}
swap(c, f);
}
return tab[c][m];
}
}
int main()
{
iostream::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, p;
cin >> n >> m >> p;
cout << solve(n, m, p) << endl;
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 | #if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <array> #include <iostream> #include <utility> #include <vector> using namespace std; namespace { void inc(int& out, int delta, int mod) { out += delta; out %= mod; if (out < 0) out += mod; } int mul(int a, int b, int mod) { return static_cast<long long>(a) * b % mod; } int solve(int n, int m, int mod) { array<vector<int>, 2> tab; for (int c = 0; c < 2; ++c) { tab[c].resize(m + 1); } int c = 0; int f = 1; tab[c][m] = 1; for (int q = 0; q < n; ++q) { int cur = 0; for (int i = 1; i <= m; ++i) { int res = tab[f][i - 1]; int tmp = tab[c][m] - tab[c][m - i]; inc(res, mul(tmp, i, mod), mod); inc(res, cur, mod); inc(cur, -tab[c][i], mod); tab[f][i] = res; } swap(c, f); } return tab[c][m]; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, m, p; cin >> n >> m >> p; cout << solve(n, m, p) << endl; return 0; } |
English