zoukankan      html  css  js  c++  java
  • [SCOI2008]着色方案

    X.[SCOI2008]着色方案

    双倍经验,双倍快乐

    可以看出这题直接是上一题的无编号版,直接套上一题的板子,乘上逆元的倒数直接水过,还轻轻松松完虐正解(五维暴力DP)

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int mod=1e9+7;
    typedef long long ll;
    int n,m,num[310],dsu[310],f[2][310][310],res,fac[310];
    int ksm(int x,int y){
    	int z=1;
    	for(;y;x=(1ll*x*x)%mod,y>>=1)if(y&1)z=(1ll*x*z)%mod;
    	return z;
    }
    int main(){
    	scanf("%d",&m);
    	for(int i=1,x;i<=m;i++){
    		scanf("%d",&num[i]);
    		for(int j=1;j<=num[i];j++)dsu[++n]=i;
    	}
    	fac[0]=1;
    	for(int i=1;i<=n;i++)fac[i]=(1ll*fac[i-1]*i)%mod;
    	f[0][0][0]=1;
    	for(int i=1,cnt;i<=n;i++){
    		memset(f[i&1],0,sizeof(f[i&1]));
    		if(dsu[i]!=dsu[i-1]){
    			cnt=0;
    			for(int j=0;j<i;j++){
    				for(int k=0;k<=j;k++)f[i&1][j][0]=(1ll*f[!(i&1)][j-k][k]*(i-j)+f[i&1][j][0])%mod;//if we put it between two balls of different colours
    				for(int k=0;k<=j+1;k++)f[i&1][j][0]=(1ll*f[!(i&1)][j-k+1][k]*(j+1)+f[i&1][j][0])%mod;//if we put it between two balls of the same colours
    			}
    		}else{
    			for(int j=0;j<i;j++){
    				for(int k=1;k<=cnt;k++)f[i&1][j][k]=(1ll*f[!(i&1)][j][k-1]*(cnt*2-(k-1))+f[i&1][j][k])%mod;//if we put it next to a ball of the same colour
    				for(int k=0;k<=cnt;k++)f[i&1][j][k]=(1ll*f[!(i&1)][j+1][k]*(j+1)+f[i&1][j][k])%mod;//if we put it between two balls of the same colours
    				for(int k=0;k<=cnt;k++)f[i&1][j][k]=(1ll*f[!(i&1)][j][k]*(i-(cnt*2-k)-j)+f[i&1][j][k])%mod;//if we put it between two balls of different colours
    			}
    		}
    		cnt++;
    	}
    	res=f[n&1][0][0];
    	for(int i=1;i<=m;i++)res=(1ll*res*ksm(fac[num[i]],mod-2))%mod;
    	printf("%d\n",res);
    	return 0;
    }
    

    当然这么就水过一道题好像有点不好意思哈

    因此额外再介绍一种方法,复杂度最劣应该是\(O(n^3)\)的,其中\(n\)是木块数量。

    我们设\(num[i]\)表示第\(i\)种颜色有多少个,\(sum[i]\)为前\(i\)种颜色有多少个。

    因为这题无编号,我们可以考虑简化一维:设\(f[i][j]\)表示:

    \(i\)颜色(!!!)

    涂了前\(sum[i]\)块,

    并且有\(j\)对相邻同色对的方案数。

    我们采取刷表法进行DP。

    考虑由\(f[i][j]\)推出\(f[i+1][?]\)

    我们枚举一个\(k\in\Big[1,num[i+1]\Big]\),表示我们将\(num[i+1]\)分成\(k\)段连续的同色木块。

    我们再枚举一个\(l\in\Big[0,\min(j,k)\Big]\),表示我们从这\(k\)段木块中,抽出\(l\)段木块塞在两段颜色相同但相邻的木块中。

    首先,依据隔板法(小学奥数),共有\(\large C_{num[i+1]-1}^{k-1}\)中合法的分割方案;

    一共有\(sum[i]-j+1\)个颜色不同的相邻位置,因此这\(j-l\)段放入颜色相同的位置的木块共有\(\large C_{sum[i]-j+1}^{j-l}\)种放法。

    一共有\(j\)个颜色相同的相邻位置,共有\(C_j^l\)种放法。

    则总方案数为\(\large C_{num[i+1]-1}^{k-1}*C_{sum[i]-j+1}^{j-l}*C_j^l*f[i][j]\)

    等等,这一大坨是往哪里更新去的?

    是往\(f\Big[i+1\Big]\Big[j+num[i+1]-k-l\Big]\)更新的。原本应该增加\(num[i+1]-1\)段相邻的,现在聚合成了\(k\)段,减少了\(k-1\)段;有\(l\)段放偏了,又减少了\(l\)段。

    代码(压 行 带 师):

    #include<bits/stdc++.h>
    using namespace std;
    const int mod=1e9+7;
    int f[20][100],n,num[20],sum[20],C[100][100];
    int main(){
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)scanf("%d",&num[i]),sum[i]=sum[i-1]+num[i];
    	for(int i=0;i<=sum[n];i++)C[i][0]=1;
    	for(int i=1;i<=sum[n];i++)for(int j=1;j<=i;j++)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
    	f[1][num[1]-1]=1;
    	for(int i=1;i<n;i++)for(int j=0;j<sum[i];j++)for(int k=1;k<=num[i+1];k++)for(int l=0;l<=min(k,j);l++)f[i+1][j-k+num[i+1]-l]=(1ll*f[i][j]*C[num[i+1]-1][k-1]%mod*C[sum[i]-j+1][k-l]%mod*C[j][l]%mod+f[i+1][j-k+num[i+1]-l])%mod;
    	printf("%d\n",f[n][0]);
    	return 0;
    }
    

  • 相关阅读:
    [LeetCode] 273. Integer to English Words 整数转为英文单词
    What happens when you type an URL in the browser and press enter?
    HTTP Status Code
    What's binary search?
    [Http] Difference between POST and GET?
    [LeetCode] 53. Maximum Subarray 最大子数组
    [LeetCode] 621. Task Scheduler 任务调度
    C# web项目添加*.ashx文件后报错处理
    Web项目和Windows应用程序的配置文件
    C#中web项目使用log4net日志
  • 原文地址:https://www.cnblogs.com/Troverld/p/14596842.html
Copyright © 2011-2022 走看看