gym102893 D. Multiple Subject Lessons

题目链接:D. Multiple Subject Lessons

题意
给定 \(n\)\(k\),表示 \(n\) 个学生,每个学生有 \(k\) 种颜色的笔,每个学生需要在纸上写下若干个数字,使得数字和为 \(n\)。问共有多少不同种方案。

SOLUTION
\(dp[i][j]\) 表示用前 \(i\) 个数字,和为 \(j\) 的方案数,转移时枚举新数字 \(i\) 的个数,若有 \(t\),则该部分方案为
\(\begin{pmatrix}t+k-1 \\ t\end{pmatrix}\)

CODE
#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, k; cin >> n >> k;
	
	vector f(n + 1, vector<LL>(n + 1));
	
	auto C = [&] (int a, int b) {
		LL ans = 1;
		for(int i = b + 1; i <= a; i ++ ) ans *= i;
		for(int j = 1; j <= (a - b); j ++ ) ans /= j;
		return ans;
	};
	
	f[0][0] = 1;
	for(int i = 1; i <= n; i ++ ) {
		for(int j = 0; j <= n; j ++ ) {
			for(int t = 0; t <= j / i; t ++ ) {
				assert(i <= n && j <= n);
				f[i][j] += f[i - 1][j - t * i] * C(t + k - 1, t);
			}
		}
	}
	
	cout << f[n][n] << "\n";
	
	
  return 0;
}

posted @ 2022-03-11 16:24  ccz9729  阅读(67)  评论(0编辑  收藏  举报