Http://acm.hit.edu.cn/hoj/problem/view? Id = 2930
Perfect fill II
|
Source: Acmgroup |
|
Time Limit: 1 sec |
|
Memory limit: 64 m |
Submitted: 69,Accepted: 50
Here comes the perfect fill problem again!
You have tow types of blocks, one is made up of two 1*1 blocks, another is made up of three 1*1 blocks. you can see the shape of two kinds of blocks in the picture below:
Now here comes the problem: How many ways can you fully fill a 2 * n block with the two kinds of blocks? Here is one way to fully fill a 2*11 block:
Input specifications
On each line of input, there will be one positive integer N (1 <= n <= 1000000000). input is end of file.
Output specifications
On each single line, output a number M mod 2010, M is the number of ways to fully fill a 2 * n block.
Sample Input
12532
Sample output
1224596
# Include <set> # include <map> # include <cmath> # include <queue> # include <stack> # include <string> # include <vector> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; typedef long ll; # define debug puts ("here") # define rep (I, n) for (INT I = 0; I <n; I ++) # define foreach (I, VEC) for (unsigned I = 0; I <Vec. size (); I ++) # define Pb push_backconst int mod = 2010; const int x = 5; Class matrix {public: int n, m; int A [x] [X]; matrix () {memset (A, 0, sizeof ());} matrix (INT _ n, int _ m): n (_ n), m (_ m) {memset (A, 0, sizeof ());} matrix Operator * (matrix P) {int q = P. m; matrix C (n, q); For (INT I = 0; I <n; I ++) for (Int J = 0; j <q; j ++) for (int K = 0; k <m; k ++) C. A [I] [J] = (C. A [I] [J] + A [I] [k] * P. A [k] [J]) % MOD; return C;} void Gete () {memset (A, 0, sizeof (a); For (INT I = 0; I <n; I ++) A [I] [I] = 1;} matrix bi N (INT exp) {matrix temp (n, n); temp. gete (); matrix cur = * This; while (exp> 0) {If (exp & 1) temp = temp * cur; cur = cur * cur; exp = exp> 1;} return temp;} void di () {for (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) cout <A [I] [J] <""; cout <Endl ;}cout <Endl ;}}; int main () {# ifndef online_judgefreopen ("sum. in "," r ", stdin); # endif int N; matrix P = matrix (3, 1); p. A [0] [0] = 1; p. A [1] [0] = 2; p. A [2] [0] = 5; whi Le (~ Scanf ("% d", & N) {If (n <= 3) {printf ("% d \ n", p. A [n-1] [0]); continue;} matrix ans = matrix (3, 3); ans. A [0] [1] = ans. A [1] [2] = 1; ans. A [2] [0] = 1; ans. A [2] [1] = 0; ans. A [2] [2] = 2; ans = ans. bin (n-3); ans = ans * P; printf ("% d \ n", ans. A [2] [0]);} return 0 ;}