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
| const int N = 300005; int n, m, k; ll mod; using poly = vector<ll>;
poly operator*(const poly &a, const poly &b) { poly c(k); for (int i = 0; i < a.size(); i++) for (int j = 0; j < b.size(); j++) c[(i + j) % k] = (c[(i + j) % k] + a[i] * b[j]) % mod; return c; }
poly fpow(poly x, ll r) { poly ans{1}; while (r) { if (r & 1)ans = ans * x; r >>= 1; x = x * x; } return ans; }
int main() { int p, q, u, v, w, x, y, z, T; int r; cin >> n >> mod >> k >> r; poly F{1, 1}; cout << fpow(F, 1ll * n * k)[r]; return 0; }
|