N个箱子放入K个小球的方案数
作者:互联网
https://zhidao.baidu.com/question/367173891541492052.html
结果为C(N+K-1,K)
思想为上面的挨个放入。
或者
将每个箱子都先放入一个球,即N个箱子,放入N+K个小球,箱子非空,然后再使用隔板法,得到C(N+K-1,N-1)。
例题:
https://atcoder.jp/contests/abc266/tasks/abc266_g
代码:
#include<bits/stdc++.h> #include<unordered_set> #define fore(x,y,z) for(LL x=(y);x<=(z);x++) #define forn(x,y,z) for(LL x=(y);x<(z);x++) #define rofe(x,y,z) for(LL x=(y);x>=(z);x--) #define rofn(x,y,z) for(LL x=(y);x>(z);x--) #define all(x) (x).begin(),(x).end() #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; LL MOD = 998244353; LL fac[3000010]; LL inv[3000010]; LL QPow(LL base, LL exp) { LL res = 1; while (exp) { if (exp & 1) { res = res * base%MOD; } base *= base; base %= MOD; exp >>= 1; } return res; } int main() { LL a, b, c; LL k; cin >> a >> b >> c >> k; a -= k; b -= k; fac[0] = fac[1] = 1; for (int i = 2; i <= 3000000; i++) { fac[i] = fac[i - 1] * i%MOD; } inv[3000000] = QPow(fac[3000000], MOD - 2); for (int i = 2999999; i >= 0; i--) { inv[i] = inv[i + 1] * (i + 1) % MOD; } LL res = 1; res *= fac[a + c + k] * inv[a]%MOD * inv[c]%MOD * inv[k]%MOD; res %= MOD; res *= fac[b + c + k] * inv[b] % MOD *inv[c + k] % MOD; res %= MOD; cout << res << endl; return 0; }View Code
标签:箱子,res,LL,小球,inv,fac,define,放入,MOD 来源: https://www.cnblogs.com/ydUESTC/p/16649029.html