Chess
Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 509 accepted submission (s): 198
Problem description xiaodu and xiaoliang have recently become addicted to playing chess. There are n rows and M columns in the checker. We can set the lattice in the upper left corner as () and the lattice in the lower right corner as (n, m ). In their rules, the "King" on the board follows the cross route. That is to say, if the "King" is currently at (x, y) point, the degree in the next step can be moved to (x + 1, Y), (x-1, Y), (X, Y + 1), (X, Y-1), (X + 2, Y), (X-2, Y), (X, Y + 2), (X, y-2) any of these eight points.
Figure 1 the yellow part is the range controlled by the pawn
He thinks that every time he wins, he is boring. To make it difficult for Mr. xiaoliang, he came up with the following question: if the "King" was at (x0, y0), Mr. xiaoliang was moving exactly K steps to the "King, how many different mobile solutions can there be? The two schemes are the same, and only when K moves are the same. That is to say, moving to the left and then to the right is considered a different solution.
Xiaoliang was defeated. Can you write a program to solve this problem?
The input includes multiple groups of data. The first line of the input data is an integer T (T ≤ 10), indicating the number of test data groups.
Each group of test data contains only one row, which is an integer of n, m, K, x0, and Y0. (1 ≤ n, m, K ≤ 1000,1 ≤ x0 ≤ n, 1 ≤ y0 ≤ m)
For data in the K group, the first row outputs case # K:, and the second row outputs the number of solutions. Because the answer may be very large, you only need to output the result to the value after the modulo of 9999991.
Sample Input
22 2 1 1 12 2 2 1
Sample output
Case #1: 2 case #2: 4
Source 2014 Baidu STAR Program Design Competition-Preliminary Round (second round)
The most direct DP Method for all the solutions for moving K steps on the board is nmk, but the computing scale is relatively large. Because the rows and columns are independent, you can calculate the rows first, calculate the column and combine the statistics.
Code:
/*************************************** * ******** Author: rabbitcreated time: 13: 55: 51 file name: 111. CPP *************************************** * ********/# pragma comment (linker, "/Stack: 102400000,102400000") # include <stdio. h> # include <iostream> # include <algorithm> # include <sstream> # include <stdlib. h> # include <string. h> # include <limits. h> # include <string> # include <time. h> # include <math. h> # Include <queue> # include <stack> # include <set> # include <map> using namespace STD; # define INF 0x3f3f3f3f # define EPS 1e-8 # define PI ACOs (-1.0) typedef long ll; const ll mod = 9999991; ll DPX [1010] [1010], dpy [1010] [1010], sumx [1010], Sumy [1010], c [1010] [1010]; int main () {// freopen ("data. in "," r ", stdin); // freopen (" data. out "," W ", stdout); int t; memset (C, 0, sizeof (c); For (INT I = 1; I <= 1000; I ++) {C [I] [0] = C [I] [I] = 1; for (Int J = 1; j <I; j ++) c [I] [J] = (C [I-1] [J-1] + C [I-1] [J]) % MOD;} CIN> T; for (int t = 1; t <= T; t ++) {int n, m, K, X, Y; cin> N> m> K> x> Y; memset (DPX, 0, sizeof (DPX); memset (dpy, 0, sizeof (dpy); memset (sumx, 0, sizeof (sumx); memset (Sumy, 0, sizeof (Sumy); DPX [0] [x] = 1; dpy [0] [Y] = 1; for (INT I = 1; I <= K; I ++) for (Int J = 1; j <= N; j ++) for (int K =-2; k <= 2; k ++) {If (k = 0) continue; int T = J + K; if (T <1 | T> N) continue; DPX [I] [T] = (d Px [I] [T] + DPX [I-1] [J]) % MOD;} For (INT I = 1; I <= K; I ++) for (Int J = 1; j <= m; j ++) for (int K =-2; k <= 2; k ++) {If (! K) continue; int T = J + k; If (T <1 | T> m) continue; dpy [I] [T] = (dpy [I] [T] + dpy [I-1] [J]) % MOD;} For (INT I = 0; I <= K; I ++) for (Int J = 1; j <= N; j ++) sumx [I] = (DPX [I] [J] + sumx [I]) % MOD; For (INT I = 0; I <= K; I ++) for (Int J = 1; j <= m; j ++) Sumy [I] = (Sumy [I] + dpy [I] [J]) % MOD; ll ans = 0; For (INT I = 0; I <= K; I ++) {ll TT = 1; TT = (TT * C [k] [I]) % MOD; TT = (TT * sumx [I]) % MOD; TT = (TT * Sumy [k-I]) % MOD; ans = (ANS + TT) % MOD;} printf ("case # % d: \ n", T); cout <ans <Endl;} return 0 ;}