Drawing Pictures
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission (s): 467 Accepted Submission (s): 207
Problem Description
Dr. Skywind is drawing a picture, using his favorite six colors, namely red, orange, yellow, green, blue, and violet.
The paper has N grids in a line. each time he will fill a grid with one of the six colors. all grids needs to be filled. to make his drawing more beautiful, Skywind decided to draw into rically. moreover, as he hates sorting, Skywind will never come up with the situation where all colors are in their original order. so he won't draw red-orange-yellow-green-blue-violet let in a continuous way. and to make his drawing clearer, he won't paint the same color in adjacent grids.
Given N, you are asked to calculate the number of ways that Skywind can complete his drawing. As the answer might be very large, just output the number MOD 112233.
Input
There are multiple test cases ended with an EOF. Each test case will be a line containing one positive integer N. (N <= 10 ^ 9)
Output
For each test case, output the answer MOD 112233 in a single line.
Sample Input
2
5
Sample Output
0
150
6 colors are used for n grids. The adjacent colors cannot be the same. They cannot be 1, 2, 3, 4, 5, or 6 colors. They must be symmetric before and after. How many possibilities are there?
/* Analysis: assume that dp [I] [0] indicates the total valid number of I; dp [I] [5] indicates the valid number ending with 12345 (convenient dp [I] [0] minus 654321xxx, for example, 123456xxx) dp [I] [4] indicates the effective number ending with 1234 for the length of I (for convenience, dp [I] [5]) dp [I] [3] indicates the effective number ending with 123 for the length of I (for convenience, dp [I] [4]) dp [I] [2] indicates the effective number ending with 12 for the length of I (to facilitate obtaining dp [I]) [3] dp [I] [1] indicates the effective number ending with 1 For I (to facilitate obtaining dp [I] [2]). Then: dp [I] [0] = 5 * dp [I-1] [0]-2 * dp [I] [5]; // subtract 654321,123456 such dp [I] [5] = dp [I-1] [4]; dp [I] [4] = dp [I-1] [3]; dp [I] [3] = dp [I-1] [2] dp [I] [2] = dp [I-1] [1]; dp [I] [1] = dp [I] [0]-dp [I-1] [1]-dp [I] [5]; // subtract 11xxx, for 123456xxx, use the Matrix to quickly calculate the power of dp [n] [0] */# include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <map> # include <iomanip> # define INF 99999999 using namespace std; const int MAX = 6; const int mod = 112233; _ int64 array [MAX] [MAX], sum [MAX] [MAX]; void MatrixMult (_ int64 a [MAX] [MAX] ,__ int64 B [MAX] [MAX ]) {_ Int64 c [MAX] [MAX] = {0}; for (int I = 0; I <MAX; ++ I) {for (int j = 0; j <MAX; ++ j) {for (int k = 0; k <MAX; ++ k) {c [I] [j] + = a [I] [k] * B [k] [j] ;}} for (int I = 0; I <MAX; ++ I) {for (int j = 0; j <MAX; ++ j) a [I] [j] = c [I] [j] % mod ;}} _ int64 Matrix (int k) {memset (array, 0, sizeof array); array [0] [0] = 5, array [0] [1] =-2; array [1] [2] = array [2] [3] = array [3] [4] = array [4] [5] = array [5] [0] = 1; array [5] [1] = array [5] [5] =-1; for (int I = 0; I <MAX; ++ I) {// Initialize sum to make sum * a = a for (int j = 0; j <MAX; ++ j) sum [I] [j] = (I = j);} while (k) {if (k & 1) MatrixMult (sum, array); MatrixMult (array, array); k> = 1;} return (sum [0] [0] * 6 + sum [0] [5]) % mod + mod) % mod; // dp [1] [0] = 6, dp [1] [1] = 1, dp [1] [I] = 0 (I! = 1 & I! = 0)} int main () {int n; while (scanf ("% d", & n )! = EOF) {if (n % 2 = 0) printf ("0 \ n "); // The even number does not match else {n = n/2 + 1 because the two symmetric centers must be equal; printf ("% I64d \ n", Matrix (n-1 )); // dp starts from the length of 1, so only the result of multiplying the matrix by N-1 is the length of n} return 0 ;}