POJ3420 Quad Tiling(模板+矩阵快速幂)
Quad Tiling
| Time Limit:?1000MS | Memory Limit:?65536K |
| Total Submissions:?4107 | Accepted:?1878 |
Description
Tired of(厭煩) the?Tri Tiling?gamefinally, Michael turns to(轉(zhuǎn)向) a morechallengeable game, Quad Tiling:
In how many ways can you tile(鋪瓷磚) a 4 ×?N?(1 ≤?N?≤109) rectangle(矩形) with 2 × 1dominoes(多米諾骨牌)? For the answerwould be very big, output the answer modulo?M?(0 <?M?≤105).
Input
Input consists of several test casesfollowed by a line containing double 0. Each test case consists of twointegers,?N?and?M, respectively(分別的).
Output
For each test case, output the answermodules?M.
Sample Input
1 10000
3 10000
5 10000
0 0
Sample Output
1
11
95
Source
POJMonthly--2007.10.06, Dagger
題意:在一個4*N的矩陣中用2*1的多米諾骨牌鋪滿,問有多少種鋪法?
模板:
遞推式:a[i]=a[i-1]+5*a[i-2]+a[i-3]-a[i-4];
由于N高達10^9,所以要用矩陣進行優(yōu)化。?
|0 1 0 0|?
|0 0 1 0|?
|0 0 0 1|?
|-1 1 5 1|?
與?
|a[i-3]|?
|a[i-2]|?
|a[i-1]|?
|a[i]|?
相乘
總結(jié)
以上是生活随笔為你收集整理的POJ3420 Quad Tiling(模板+矩阵快速幂)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ndoe.js实战之开发微博第一讲之工具
- 下一篇: 2016杭州ccpc