Question Link
A three-in-one pressure problem.
Description:
Tyvj's anniversary is coming. Sam wants to make a big cake for tyvj. The cake top view is a rectangle of n × m, which is divided into small square areas with n × m sides with a length of 1 × 1 (the cake can be regarded as a matrix of N rows and M columns ).
The cake is soon ready, but the bare cake is definitely not good!
Therefore, Sam needs to apply jam on the surface of the cake. There are three types of jams: Red jam, green jam, and blue jam,
The numbers of the three jams are 1, 2, and 3. To ensure the visual effect of the cake, admin issued the following command:
The same type of jam is prohibited in adjacent areas. But before Sam receives this command,
The contents of the cake line KKK have been coated and cannot be modified. Sam wants to know the number of jam-coated solutions that can satisfy the admin. Please output the solution number mod1e6. If no solution meets the conditions, output 0.
Input Format
Enter three rows in total.
First line: n, m;
Row 2: K;
Row 3: an integer of M, indicating the solution of row K.
For more information about the description of letters, see the example.
Output Format
Only one line is output, which is the total number of feasible solutions.
ExampleSample Input
2 2 1 2 3
Sample output
3
Solution:
The key to this question is to determine the legal situation. You can determine the k-th line.
1. Determine whether a three-digit number is adjacent to the same number. The binary shifts left and right are not allowed,
Because there are three numbers here, the Left shift and right shift will have a 0 effect.
2. Determine if different rows have the same numbers adjacent to each other. Simulate the binary format and click "OK.
Code:
# Include <bits/stdc ++. h> # define ll long # define R registerusing namespace STD; int n, m, K, MOD = 1e6, a [250], SK, num, top, ANS, f [10005] [250]; inline int KSM (r int X, r int p) {r int tot = 1; while (p) {If (P & 1) {tot = tot * X;} X = x * X; P >>=1;} return tot;} inline int check (r int X, r int y) {for (r int I = 1; I <= m; ++ I) {If (X % 3) = (Y % 3) return 0; x/= 3; y/= 3;} return 1;} inline int judge (r int X) {r int y =-1; for (R in T I = 1; I <= m; ++ I) {If (y = x % 3) return 0; y = x % 3; X/= 3 ;} return 1;} inline void Init () {for (r int I = 0; I <= 242; ++ I) {r int x = I, TOT = 0; while (x) {x/= 3; ++ tot;} If (TOT> = m + 1) break; If (Judge (I )) {A [++ num] = I; if (I = Sk) Top = num ;}} int main () {scanf ("% d ", & N, & M); scanf ("% d", & K); For (r int I = 1; I <= m; ++ I) {r int T; scanf ("% d", & T); SK + = (t-1) * KSM (3, I-1);} If (! Judge (SK) {printf ("0"); Return 0;} Init (); If (k = 1) f [1] [top] = 1; else For (r int I = 1; I <= num; ++ I) f [1] [I] = 1; for (r int I = 2; I <= N; ++ I) // current row number {if (I = k) {for (r int T = 1; t <= num; ++ t) if (check (A [Top], a [T]) f [I] [top] = (F [I] [top] + F [I-1] [T]) % MOD;} else {for (r Int J = 1; j <= num; ++ J) // current row status {If (I-1 = k) {If (check (A [J], a [Top]) f [I] [J] = (F [I] [J] + F [I-1] [top]) % MOD;} else {for (r int T = 1; T <= num; ++ t) // The status of the previous row if (check (A [J], a [T]) f [I] [J] = (F [I] [J] + F [I-1] [T]) % mod ;}}for (r int I = 1; I <= num; ++ I) ans = (ANS + F [N] [I]) % MOD; printf ("% d", ANS % mod ); return 0 ;}
The key to this question is to discard the judgment of illegal situations.
#10172. "yiben Tong 5.4 Exercise 1" smear jam question