AtCoder AGC002F Leftmost Ball (DP、组合计数)

题目链接: https://atcoder.jp/contests/agc002/tasks/agc002_f

题解: 讲一下官方题解的做法: 就是求那个图(官方题解里的)的拓扑序个数,设(dp[i][j])表示有(i)个0色和(j)个非0色的图的拓扑序个数((i<j)),则转移一是加入一个0色球,二是加入一个非0色球(拓扑序以非0色球开始),这种情况下我们固定了开头所以还剩(((K-1)j+i-1))个位置放入((K-2))个球,(dp[i][j]=dp[i-1][j]+dp[i][j-1] imes {{(K-1)j+i-1}choose{K-2}}).

另外有一种反着的做法(我代码写的就是这个),但是并不懂(曾经懂过但是现在脑子又乱了)

启示: 这种计数很多都是考虑第一个,千万不要惯性思维无脑考虑最后一个!!!

代码

#include<cstdio>
#include<cstdlib>
#include<cstring>
#define llong long long
using namespace std;

const int N = 2000;
const int P = 1e9+7;
llong dp[N+3][N+3];
llong fact[5000003],finv[5000003];
int n,m;

llong quickpow(llong x,llong y)
{
	llong cur = x,ret = 1ll;
	for(int i=0; y; i++)
	{
		if(y&(1ll<<i)) {y-=(1ll<<i); ret = ret*cur%P;}
		cur = cur*cur%P;
	}
	return ret;
}

llong comb(llong x,llong y) {return x<0 || y<0 || x<y ? 0ll : fact[x]*finv[y]%P*finv[x-y]%P;}

int main()
{
	fact[0] = 1ll; for(int i=1; i<=5000000; i++) fact[i] = fact[i-1]*i%P;
	finv[5000000] = quickpow(fact[5000000],P-2); for(int i=5000000-1; i>=0; i--) finv[i] = finv[i+1]*(i+1)%P;
	scanf("%d%d",&n,&m);
	if(m==1) {printf("1"); return 0;}
	dp[0][0] = 1ll;
	for(int i=1; i<=n; i++)
	{
		dp[i][0] = 1ll;
		for(int j=1; j<=i; j++)
		{
			dp[i][j] = dp[i-1][j];
			dp[i][j] += comb((n-i)+(n-(j-1))*(m-1)-1,m-2)*dp[i][j-1]%P;
			dp[i][j] %= P;
		}
	}
	llong ans = dp[n][n]*fact[n]%P;
	printf("%lld
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/suncongbo/p/11144645.html