Codeforces 233 D - Table

时间:2021-08-30 19:13:19

D - Table

思路:dp

首先,第i列的个数肯定和第i - n列个数一样,假设[i - n + 1, i - 1] 之间的个数之和为x,那么第i列和第i-n列的个数应该是n - x

那么我们可以用dp求方案数

状态:dp[i][j] 表是到第 i 列为止 填了 j 个的方案数

初始状态: dp[0][0] = 1

状态转移: dp[i][j](1 <= i <= n, 0 <= j <= k) = ∑(dp[i-1][j - l](l <= n && j >= l) * C(n, l) ^ cnt)

其中,cnt = n/m 或者 n/m + 1,C(n, l)^cnt 可以预处理来降低复杂度

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int MOD = 1e9 + ;
const int N = ;
LL dp[N][N*N];
LL qp[N][N][];
LL q_pow(LL n, LL k) {
LL ans = ;
while(k) {
if(k&) ans = (ans * n) % MOD;
n = (n * n) % MOD;
k >>= ;
}
return ans;
}
int main() {
int n, k;
LL m;
scanf("%d %lld %d", &n, &m, &k);
LL cnt = m/n;
LL C = ;
for (int i = ; i <= n; i++) {
qp[n][i][] = q_pow(C, cnt);
qp[n][i][] = q_pow(C, cnt+);
C = (C * (n-i)) % MOD;
C = (C * q_pow(i+, MOD-)) % MOD;
}
dp[][] = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= k; j++) {
for (int l = ; l <= n && j >= l; l++) {
if(i <= m%n) dp[i][j] = (dp[i][j] + dp[i-][j-l] * qp[n][l][]) % MOD;
else dp[i][j] = (dp[i][j] + dp[i-][j-l] * qp[n][l][]) % MOD;
}
}
}
printf("%lld\n", dp[n][k]);
return ;
}