Source
https://www.lydsy.com/JudgeOnline/problem.php?id=4870
Solution
首先,组合数有一个重要的性质:
设:
根据组合数的性质推出:
边界 。
由于 不大,因此用矩阵乘法就能解决这个问题。
Code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
using namespace std; typedef long long ll;
const int N = 55;
int n, PYZ, k, r;
struct cyx {
int n, m, a[N][N]; cyx() {} cyx(int _n, int _m) : n(_n), m(_m)
{memset(a, 0, sizeof(a));}
friend inline cyx operator * (const cyx &a, const cyx &b) {
int i, j, k; cyx res = cyx(a.n, b.m);
For (i, 1, res.n) For (j, 1, res.m) For (k, 1, a.m)
res.a[i][j] = (res.a[i][j] + 1ll * a.a[i][k] * b.a[k][j] % PYZ) % PYZ;
return res;
}
friend inline cyx operator ^ (cyx a, ll b) {
int i; cyx res = cyx(a.n, a.m); For (i, 1, res.n) res.a[i][i] = 1;
while (b) {
if (b & 1) res = res * a; a = a * a; b >>= 1;
}
return res;
}
} P, Y, Z;
int main() {
int i; cin >> n >> PYZ >> k >> r; P = cyx(k, k);
For (i, 0, k - 1) P.a[i + 1][i + 1]++, P.a[i + 1][(i + k - 1) % k + 1]++;
Y = cyx(1, k); Y.a[1][1] = 1; Z = Y * (P ^ 1ll * n * k);
cout << Z.a[1][r + 1] << endl; return 0;
}