It is also a question of Baidu star.
When I first read the question and thought it was a search question, I came up with the idea of finding a question. There was no fighting spirit at the beginning, and of course this question would not be made.
However, after reading the question, I suddenly realized that it was originally DP, and it was a very simple DP.
Another failure indicates that it is not a good habit to look at the problem. I want to change it! I want to change it !!
In fact, the basic idea is to separate the two-dimensional movement into one-dimensional movement, and then add the combination number.
The following is the code. Although it was typed by myself, it is still plagiarized ....
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int mod = 9999991;const int maxn = 1010;#define LL long longLL sum[2][maxn], dp[2][maxn][maxn];LL C[1010][1010];void init(){ memset(sum, 0, sizeof(sum)); memset(dp, 0, sizeof(dp)); for(int i = 0; i < maxn; i++) C[i][i] = 1, C[i][0] = 1; for(int i = 2; i < maxn; i++) for(int j = 1; j < i; j++) C[i][j] =(C[i-1][j] + C[i-1][j-1]) % mod;}void solve(int n, int k, int t, int p){ dp[p][0][t] = 1; sum[p][0] = 1; for(int i = 1; i <= k; i++) { for(int j = 1; j <= n; j++) { if(j - 1 > 0) dp[p][i][j] += dp[p][i-1][j-1]; if(j - 2 > 0) dp[p][i][j] += dp[p][i-1][j-2]; if(j + 1 <= n) dp[p][i][j] += dp[p][i-1][j+1]; if(j + 2 <= n) dp[p][i][j] += dp[p][i-1][j+2]; dp[p][i][j] %= mod; sum[p][i] = (sum[p][i] + dp[p][i][j]) % mod; } } return ;}LL getans(int k){ LL ans = 0; for(int i = 0; i <= k; i++) ans = ans + (((C[k][i] * sum[1][i]) % mod) * sum[0][k - i]) % mod; return ans % mod;}int main(){ int t, n, m, k, x, y; cin >> t; for(int Case = 1; Case <= t; Case++) { scanf("%d%d%d%d%d", &n, &m, &k, &x, &y); init(); solve(n,k,x,0); solve(m,k,y,1); printf("Case #%d:\n%I64d\n", Case, getans(k)); }}View code