poj3420 Quad Tiling

Quad TilingTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 2443Accepted: 992

Description

Tired of the Tri Tiling game finally, Michael turns to a more challengeable game, Quad Tiling:

In how many ways can you tile a 4 × N (1 ≤ N ≤ 109) rectangle with 2 × 1 dominoes? For the answer would be very big, output the answer modulo M (0 < M ≤ 105).

Input

Input consists of several test cases followed by a line containing double 0. Each test case consists of two integers, N and M, respectively.

Output

For each test case, output the answer modules M.

Sample Input

1 10000

3 10000

5 100000

 0

Sample Output

1

11

95

Source

POJ Monthly--2007.10.06, Dagger

 

 

 

——————————————————————————

和poj2663 Tri Tiling一样。

——————————————————————————

  1 Program Stone;
  2 
  3 const m=4;
  4 
  5 type ar=array[0..15,0..15]of longint;
  6 
  7 var i,j,k,n,q,s:longint;
  8 
  9     a,qu,ti:ar;
 10 
 11  procedure check(x,y:longint);
 12 
 13  var i,k:longint;
 14 
 15   begin
 16 
 17     for i:=0 to m-2 do
 18 
 19      if ((y shr i)and 1=0)and((y shr (i+1))and 1=0) then
 20 
 21            begin
 22 
 23              k:=y+(1 shl i)+(1 shl (i+1));
 24 
 25              a[x,k]:=1;
 26 
 27              check(x,k);
 28 
 29            end;
 30 
 31   end;
 32 
 33  procedure time(a,b:ar);
 34 
 35  var i,j,k:longint;
 36 
 37   begin
 38 
 39     fillchar(ti,sizeof(ti),0);
 40 
 41     for i:=0 to 15 do
 42 
 43      for j:=0 to 15 do
 44 
 45       for k:=0 to 15 do
 46 
 47        ti[i,j]:=(ti[i,j]+a[i,k]*b[k,j] mod q)mod q;
 48 
 49   end;
 50 
 51  procedure quick(k:longint);
 52 
 53   begin
 54 
 55    if k=1 then begin qu:=a;exit;end;
 56 
 57    quick(k div 2);
 58 
 59    time(qu,qu);
 60 
 61    if k mod 2<>0 then time(ti,a);
 62 
 63    qu:=ti;
 64 
 65   end;
 66 
 67 Begin
 68 
 69  assign(input,'input.in');reset(input);
 70 
 71   s:=(1 shl m)-1;
 72 
 73   for i:=0 to s do       //建图
 74 
 75    begin
 76 
 77     k:=i xor s;
 78 
 79     a[i,k]:=1;
 80 
 81     check(i,k);
 82 
 83    end;
 84 
 85   readln(n,q);
 86 
 87   while n<>0 do
 88 
 89    begin
 90 
 91      quick(n+1);
 92 
 93      writeln(qu[0,s]);
 94 
 95      readln(n,q);
 96 
 97    end;
 98 
 99 end.
100 
101  

 

posted on 2016-03-02 20:11  Yesphet  阅读(262)  评论(0编辑  收藏  举报