E - 着色方案 HYSBZ - 1079 (计数DP)

题目链接:https://cn.vjudge.net/contest/281963#problem/E

题目大意:中文题目

 具体思路:这个题脑洞有点大,因为ci的数据量非常小,所以我们可以根据这个来进行操作,我们要找的答案=使用1次的颜色方案数+使用2次的颜色的方案数+使用3次的颜色的方案数+.....。

具体的解释看代码:

 1 #include<iostream>
 2 #include<stack>
 3 #include<cmath>
 4 #include<stdio.h>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 # define ll long long
 9 const int mod = 1e9+7;
10 const int maxn = 1000+100;
11 ll vis[10];
12 ll dp[16][16][16][16][16][16];
13 ll dfs(ll t1,ll t2,ll t3,ll t4,ll t5,ll pre)
14 {
15     if(t1+t2+t3+t4+t5==0)
16         return dp[t1][t2][t3][t4][t5][pre]=1;
17     if(dp[t1][t2][t3][t4][t5][pre])
18         return dp[t1][t2][t3][t4][t5][pre];
19     ll ans=0;
20     if(t1)
21         ans+=dfs(t1-1,t2,t3,t4,t5,1)*(t1-(pre==2)),ans%=mod;// 减去为了返回原来的状态而多加的1
22     if(t2)
23         ans+=dfs(t1+1,t2-1,t3,t4,t5,2)*(t2-(pre==3)),ans%=mod;//t1+1的原因是:当t1存在的时候递归形式t1-1,为了恢复原来的状态,所以是t1+1,。
24     if(t3)
25         ans+=dfs(t1,t2+1,t3-1,t4,t5,3)*(t3-(pre==4)),ans%=mod;
26     if(t4)
27         ans+=dfs(t1,t2,t3+1,t4-1,t5,4)*(t4-(pre==5)),ans%=mod;
28     if(t5)
29         ans+=dfs(t1,t2,t3,t4+1,t5-1,5)*t5,ans%=mod;
30     return dp[t1][t2][t3][t4][t5][pre]=ans;
31 }
32 int main()
33 {
34     int k;
35     scanf("%d",&k);
36     for(int i=1; i<=k; i++)
37     {
38         int tmp;
39         scanf("%d",&tmp);
40         vis[tmp]++;
41     }
42     ll ans=dfs(vis[1],vis[2],vis[3],vis[4],vis[5],0);
43     printf("%lld\n",ans);
44     return 0;
45 }

猜你喜欢

转载自www.cnblogs.com/letlifestop/p/10370975.html