• 【BZOJ3294/洛谷3158】[CQOI2011]放棋子(组合数+DP)


    题目:

    洛谷3158

    分析:

    某OIer兔崽子的此题代码中的三个函数名:dfs、ddfs、dddfs(充满毒瘤的气息

    显然,行与行之间、列与列之间是互相独立的。考虑背包,用(f[k][i][j])表示用前(k)种颜色占了(i)(j)列的方案数,(g[i][j])表示用颜色(k)占据(i)(j)列的方案数,(c[i])表示颜色为(i)的棋子数,就有如下方程:

    [f[k][i][j]=sum _{a=0}^i sum_{b=0}^j f[k-1][i-a][j-b] imes g[a][b] imes C_{n-(i-a)}^a imes C_{m-(j-b)}^b(abgeq c[i]) ]

    (g[i][j])在算的时候注意要减去有空行或空列的情况(枚举有多少行、列不是空的)。注意要(a=i)(b=j)的情况要跳过:

    [g[i][j]=C_{ij}^{c[k]}-sum_{a=0}^i sum_{b=0}^j g[a][b] imes C_i^a imes C_j^b(ijgeq c[k]) ]

    代码:

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
    namespace zyt
    {
    	template<typename T>
    	inline void read(T &x)
    	{
    		char c;
    		bool f = false;
    		x = 0;
    		do
    			c = getchar();
    		while (c != '-' && !isdigit(c));
    		if (c == '-')
    			f = true, c = getchar();
    		do
    			x = x * 10 + c - '0', c = getchar();
    		while (isdigit(c));
    		if (f)
    			x = -x;
    	}
    	template<typename T>
    	inline void write(T x)
    	{
    		static char buf[20];
    		char *pos = buf;
    		if (x < 0)
    			putchar('-'), x = -x;
    		do
    			*pos++ = x % 10 + '0';
    		while (x /= 10);
    		while (pos > buf)	
    			putchar(*--pos);
    	}
    	typedef long long ll;
    	const int N = 40, T = 20, p = 1e9 + 9;
    	int n, m, c, arr[T], C[N * N][N * N], f[T][N][N], g[N][N];
    	void init()
    	{
    		for (int i = 0; i < N * N; i++)
    		{
    			C[i][0] = 1;
    			for (int j = 1; j <= i; j++)
    				C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % p;
    		}
    	}
    	int work()
    	{
    		init();
    		read(n), read(m), read(c);
    		for (int i = 1; i <= c; i++)
    			read(arr[i]);
    		f[0][0][0] = 1;
    		for (int k = 1; k <= c; k++)
    		{
    			memset(g, 0, sizeof(g));
    			for (int i = 1; i <= n; i++)
    				for (int j = 1; j <= m; j++)
    				{
    					if (i * j >= arr[k])
    					{
    						g[i][j] = C[i * j][arr[k]];
    						for (int a = 0; a <= i; a++)
    							for (int b = 0; b <= j; b++)
    							{
    								if (a != i || b != j)
    									g[i][j] = (g[i][j] - (ll)g[a][b] * 
    										C[i][a] % p * C[j][b] % p + p) % p;
    							}
    					}
    				}
    			for (int i = 1; i <= n; i++)
    				for (int j = 1; j <= m; j++)
    					for (int a = 1; a <= i; a++)
    						for (int b = 1; b <= j; b++)
    							if (a * b >= arr[k])
    								f[k][i][j] = (f[k][i][j] + 
    									(ll)f[k - 1][i - a][j - b] * g[a][b] % p * 
    									C[n - (i - a)][a] % p * C[m - (j - b)][b]) % p;
    		}
    		int ans = 0;
    		for (int i = 1; i <= n; i++)
    			for (int j = 1; j <= m; j++)
    				ans = (ans + f[c][i][j]) % p;
    		write(ans);
    		return 0;
    	}
    }
    int main()
    {
    	return zyt::work();
    }
    
  • 相关阅读:
    Django 模型层
    Django 模板层
    就在明晚,3月10日19:30,#HMS Core Insights#直播来了!
    Unity平台 | 快速集成华为性能管理服务
    手把手教你集成华为机器学习服务(ML Kit)人脸检测功能
    在onBackPress中实现退出拦截时不生效
    在家也可以享受购物体验?AR+Scene 帮您实现商品3D展示和虚拟试戴
    web组件发消息给H5页面时报错
    借助近距离通信,实现手机作为游戏控制器掌控大屏游戏
    快应用审核常见问题
  • 原文地址:https://www.cnblogs.com/zyt1253679098/p/10116225.html
Copyright © 2020-2023  润新知