LA 6801 Sequence (DP)
Question:
Given the initial status of n switches (0/1), perform k operations. You can select any one of them and reverse the status (0-1, 1-0 ). Question: The number of methods that make the final state all 0% 1000000007.
Solution:
Dynamic Planning. Dp [I] [j] indicates the number of j 1 solutions after the I operation.
State transition equation: dp [I] [j + 1] = dp [I-1] [j] * (n-j)
Dp [I] [J-1] = dp [I-1] [j] * j
Reference code:
// Author: Yuan Zhu#include
#include
#include
#define ll long long#define mod 1000000007using namespace std;int t, n, k;int a[1100];ll dp[1100][1100];int main() { scanf("%d", &t); for (int ca = 1; ca <= t; ca++) { memset(dp, 0, sizeof dp); scanf("%d%d", &n, &k); int one = 0; for (int i = 1; i <= n; i++) { scanf("%d", a + i); if(a[i] == 1) one++; } dp[0][one] = 1; for (int i = 0; i < k; i++) { for (int j = 0; j <= n; j++) { if (j) dp[i + 1][j - 1] = (dp[i + 1][j - 1] + dp[i][j] * j) % mod; if (j < n) dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (n - j)) % mod; } } printf("Case #%d: %lld\n", ca, (dp[k][0] % mod + mod) % mod); } return 0;}