DP (I, J, 1) represents the first I, with J pairs is not legal, I and i-1 are together.
DP (i, j, 0) denotes the first I, there is J pair is not legal, I and i-1 not together.
Transfer we just need to consider is more than a pair of illegal or less a pair of illegal, or is unchanged, consider the current I and i-1,i-2 position of the influence on it.
DP (I, j, 1) = 2*DP (i-1, j-1, 0) + DP (i-1, J-1, 1) + DP (I-1, J, 1)
DP (i, j, 0) = (i-j-2) *DP (i-1, J, 0) + (j+1) *DP (i-1, j+1, 0) + (i-j-1) *DP (i-1,j,1) + J*DP (i-1, j+1, 1)
This problem seems to have a recursive type ....
---------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long Long ll;const int MAXN = 1009;const int MOD = 7777777;int dp[2][maxn][2], N;inline void upd (int &x, int t) {if ((x + = t) >= MOD)x-= MOD;}int main () {scanf ("%d", &n);int c = 0, p = 1;memset (Dp[c], 0, sizeof dp[c]);dp[c][0][0] = 1;for (int i = 2; I <= N; i++) {Swap (c, p);memset (Dp[c], 0, sizeof dp[c]);For (int j = 0; J < i; J + +) {dp[c][j][1] = dp[p][j][1];if (j) {upd (dp[c][j][1], dp[p][j-1][0]);upd (dp[c][j][1], dp[p][j-1][0]);upd (dp[c][j][1], dp[p][j-1][1]);}Dp[c][j][0] = (LL (dp[p][j + 1][0]) * (j + 1) + LL (j) * Dp[p][j + 1][1])% MOD;if (i >= j + 2)upd (Dp[c][j][0], (LL (dp[p][j][0) * (I-j-2) + LL (dp[p][j][1]) * (I-J-1)) (% MOD);}}printf ("%d\n", Dp[c][0][0]);return 0;}
---------------------------------------------------------------------------------
4321:queue2 time limit: ten Sec Memory Limit: MB
Submit: 104 Solved: 54
[Submit] [Status] [Discuss] Descriptionn A sand tea, is numbered 1~n. After the team, each satay hope that their two neighbors as long as no one's number and their own number difference is 1 (+1 or-1) on the line; now wonder how many schemes exist to meet the conditions of the sand tea so harsh.
Input has only one row and is a positive integer N separated by a space, where 100% of the data satisfies 1≤n≤1000;
Output a non-negative integer that represents the number of scenarios for modulo 7777777.
Sample Input4Sample Output2
Sample interpretation: There are two scenarios 2 4 1 3 and 3 1 4 2HINT
Source
Bzoj 4321:queue2 (DP)