kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

插头 DP

陈丹琦的大作 http://www.docin.com/p-46797997.html

http://acm.hdu.edu.cn/showproblem.php?pid=1964

http://acm.timus.ru/problem.aspx?space=1&num=1519

http://poj.org/problem?id=1739

http://acm.hdu.edu.cn/showproblem.php?pid=3377

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3256

http://acm.hdu.edu.cn/showproblem.php?pid=1693

http://acm.fzu.edu.cn/problem.php?pid=1977

http://poj.org/problem?id=3133

// timus_1519 有效状态间转移

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<stdio.h>

long long dp[60000],pre[60000];
int all,r[60000],e[1600000];
int p[60000][13];

class DP {

public:
	int n,m,can[33][33],last;
	int val[33],LL,UU,sta,next;
	int a[33],b[33],d[33],c[33];

	void input()
	{
		int i,j;
		char ch[15];
		last = 0;
		for(i=1;i<=n;i++)
		{
			scanf("%s",ch);
			for(j=1;j<=m;j++)
			{
				can[i][j] = ch[j-1]=='.'?(++last):-1;
			}
		}
	}

	int ok()
	{
		int i,l;
		l = 0;
		for(i=1;i<=m+1;i++)
		{
			b[i] = d[i] = -1;
			if(a[i] == 1) c[++l] = i;
			else
			if(a[i] == 2)
			{
				if(l == 0) return 0;
				b[c[l]] = i; d[i] = c[l];
				l--;
			}
		}
		if(l != 0) return 0;
		return 1;
	}

	void init()
	{
		int i,j,k;

		val[1] = 1;
		for(i=2;i<=m+2;i++) val[i] = val[i-1]*3;;

		for(i=0;i<=m+2;i++) a[i] = 0;
		a[1] = -1;

		all = 0;

		for(i=0;i<=val[m+2];i++)
		{
			a[1]++;
			k = 1; while(k <= m+1 && a[k]>2) { a[k]%=3; a[k+1]++; k++; }

			e[i] = -1;

			if(ok() == 0) continue;

			r[all] = i; e[i] = all;

			for(j=1;j<=m+1;j++) {
				p[all][j] = -1;
				if(b[j] != -1) p[all][j] = b[j];
				if(d[j] != -1) p[all][j] = d[j];
			}

			all++;
		}
	}

	void abc()
	{
		int i,j,k,l;

		input();
		init();

		for(i=0;i<all;i++) dp[i] = 0;

		dp[r[0]] = 1;

		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				if(j == 1)
				{
					for(k=all-1;k>=0;k--)
					{
						l = r[k]; l = l/3; l = e[l];

						if(l != -1)
						{
							dp[k] = dp[l];
							if(r[k]%3 != 0) dp[k] = 0;
						}
						else
							dp[k] = 0;
					}
				}

				for(k=0;k<all;k++)
				{
					pre[k] = dp[k]; dp[k] = 0;
				}

				for(k=0;k<all;k++)
				if(pre[k] > 0)
				{
					LL = r[k]/val[j]%3;
					UU = r[k]/val[j+1]%3;

					if(can[i][j] == -1)
					{
						if(LL == 0 && UU == 0)
						{
							dp[k] += pre[k];
						}
						continue;
					}

					if(UU == 0 && LL == 0)
					{
						sta = r[k]+val[j]+val[j+1]+val[j+1];

						dp[e[sta]] += pre[k];
					}
					else
					if(LL == 0)
					{
						sta = r[k];
						dp[e[sta]] += pre[k];

						sta = r[k]+r[k]/val[j+1]%3*(val[j]-val[j+1]);
						dp[e[sta]] += pre[k];
					}
					else
					if(UU == 0)
					{
						sta = r[k];
						dp[e[sta]] += pre[k];

						sta = r[k]+r[k]/val[j]%3*(-val[j]+val[j+1]);
						dp[e[sta]] += pre[k];
					}
					else
					if(LL == 2 && UU == 1)
					{
						sta = r[k]-val[j]-val[j]-val[j+1];
						dp[e[sta]] += pre[k];
					}
					else
					if(LL == 1 && UU == 1)
					{
						if(p[k][j+1] > 0 && p[k][j+1] <= m+1)
						{
							sta = r[k]-val[j]-val[j+1]-val[p[k][j+1]];
							dp[e[sta]] += pre[k];
						}
					}
					else
					if(LL == 2 && UU == 2)
					{
						if(p[k][j] > 0)
						{
							sta = r[k]-val[j]-val[j]-val[j+1]-val[j+1]+val[p[k][j]];
							dp[e[sta]] += pre[k];
						}
					}
					else
					if(LL == 1 && UU == 2)
					{
						if(can[i][j] == last)
						{
							sta = r[k]-val[j]-val[j+1]-val[j+1];
							dp[e[sta]] += pre[k];
						}
					}
				}
			}
		printf("%lld\n",dp[r[0]]);
	}

	void solve()
	{
		while(scanf("%d %d",&n,&m) != EOF)
		{
			abc();
		}
	}
};

int main() {
	DP dp;
	dp.solve();
	return 0;
}

algorithm, dp

« 快速傅里叶变换计算大整数乘法 code 插头 DP code1-2 »