首页 > 代码库 > UVA 11125 Arrange Some Marbles
UVA 11125 Arrange Some Marbles
dp[i][j][m][n][s]表示最初选择j个i号颜色大理石。当前选择n个m号颜色大理石。剩余大理石状态(8进制数状压表示)最开始没看出状压。。sad
#include <map>#include <set>#include <list>#include <cmath>#include <ctime>#include <deque>#include <stack>#include <queue>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <climits>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define PI 3.1415926535897932626using namespace std;int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}int base_8[]={1 ,8 ,64 ,512};int cnt[4],dp[4][4][4][4][5000];int N;int calcu(int hc, int hl, int pc, int pl, int sta){ if (dp[hc][hl][pc][pl][sta]!=-1) return dp[hc][hl][pc][pl][sta]; dp[hc][hl][pc][pl][sta]=0; if (sta == 0) { if (hc != pc && hl != pl) return dp[hc][hl][pc][pl][sta] = 1; else return dp[hc][hl][pc][pl][sta] = 0; } for (int i = 0; i < N; i++) for (int j = 1; j <= 3 && j <= cnt[i]; j++) { if (i != pc && j != pl) { cnt[i] -= j; dp[hc][hl][pc][pl][sta] += calcu(hc, hl, i, j, sta - base_8[i] * j); cnt[i] += j; } } return dp[hc][hl][pc][pl][sta];}int slove(){ int state=0,ans=0; for (int i = N - 1; i >= 0; i--) state = state * 8 + cnt[i]; for (int i = 0; i < N; i++) for (int j = 1; j <= 3 && j <= cnt[i]; j++) ans += calcu(i, j, i, j,state - j * base_8[i]); return ans;}int main(){ int T; memset(dp,-1,sizeof(dp)); scanf("%d",&T); while (T--) { scanf("%d",&N); for (int i = 0; i < N; i++) scanf("%d",&cnt[i]); if (cnt[0] == 0 && cnt[1] == 0 && cnt[2] == 0 && cnt[3] == 0) puts("1"); else printf("%d\n",slove()); } return 0;}
UVA 11125 Arrange Some Marbles
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。