【HDU - 6185】Covering(矩阵快速幂优化二维dp,高斯消元,轮廓线dp打表)
題干:
Bob's school has a big playground, boys and girls always play games here after school.?
To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.?
Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.?
He has infinite carpets with sizes of?1×21×2?and?2×12×1, and the size of the playground is?4×n4×n.?
Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping??
Input
There are no more than 5000 test cases.?
Each test case only contains one positive integer n in a line.?
1≤n≤10181≤n≤1018?
Output
For each test cases, output the answer mod 1000000007 in a line.?
Sample Input
1 2Sample Output
1 5題目大意:
給一個4*n的格子,你有兩種方塊(1*2),(2*1),放置的同時不能交叉重疊,詢問放滿的方案數。(n<=1e18)
解題報告:
考慮幾種可行的狀態:①0000,②1100,③0110,④0011,⑤1001,⑥1111。分別轉移即可。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const ll mod = 1e9 + 7; const int MAX = 9; struct Matrix {ll m[MAX][MAX]; } unix; Matrix Mul(Matrix a,Matrix b) {Matrix c;memset(c.m,0,sizeof c.m);for(int i = 1; i<=6; i++) {for(int j = 1; j<=6; j++) {for(int k = 1; k<=6; k++) {c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j])%mod;}}}return c; } Matrix qpow(Matrix a,ll k) {Matrix res = unix;while(k) {if(k&1) res = Mul(res,a);a = Mul(a,a);k>>=1;}return res; } int main() { ll n;for(int i = 1; i<=6; i++) {unix.m[i][i] = 1;}Matrix trans;memset(trans.m,0,sizeof trans.m);trans.m[1][6]=trans.m[2][4]=trans.m[2][6]=trans.m[3][5]=trans.m[3][6]=trans.m[4][2]=trans.m[4][6]=trans.m[5][3]=1;trans.m[6][1]=trans.m[6][2]=trans.m[6][3]=trans.m[6][4]=trans.m[6][6]=1;while(~scanf("%lld",&n)) {Matrix ans = qpow(trans,n+1);printf("%lld\n",ans.m[1][6]);}return 0 ; }?
另一個題解:
https://blog.csdn.net/qq_32570675/article/details/77816353
總結
以上是生活随笔為你收集整理的【HDU - 6185】Covering(矩阵快速幂优化二维dp,高斯消元,轮廓线dp打表)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wfxsnt40.exe - wfxsn
- 下一篇: WFXSVC.EXE - WFXSVC是