hdu6185 dp+矩阵乘法

来源:互联网 发布:签署淘宝图片空间协议 编辑:程序博客网 时间:2024/03/29 16:25

Covering

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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×2 and 2×1, and the size of the playground is 4×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.

1n1018
 

Output
For each test cases, output the answer mod 1000000007 in a line.
 

Sample Input
12
 

Sample Output
15
 

Source
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

老题新意

#include <string>#include <stdlib.h>#include <vector>#include <algorithm>#include <string.h>#include <stdio.h>#include <set>#include <iostream>#include <algorithm>#include <map>#include <queue>#include <math.h>#include<iostream>using namespace std;typedef long long LL ;const LL Mod = 1000000007 ;int  a[16][16] ;void  dfs(int n, int from, int to) {    if(n > 4)  return  ;    if(n == 4) {        a[to][from] += 1 ;        return  ;    }    dfs(n+2, (from<<2)+3, (to<<2)+3) ;    dfs(n+1, from<<1|1, (to<<1)) ;    dfs(n+1, from<<1, to<<1|1 ) ;}struct Mat {    LL  v[16][16] ;    Mat() {    };    Mat(int c[16][16]) {        for(int i = 0 ; i < 16 ; i++) {            for(int j = 0 ; j < 16 ; j++) {                v[i][j] = c[i][j] ;            }        }    }    Mat(int type) {        memset(v, 0, sizeof(v)) ;        if(type == 1) {            for(int i = 0 ; i < 16 ; i++) {                v[i][i] = 1 ;            }        }    }};Mat mult(Mat x, Mat y) {    Mat s =  Mat(0) ;    for(int k = 0 ; k < 16 ; k++) {        for(int i = 0 ; i < 16 ; i++) {            if(x.v[i][k] == 0) {                continue ;            }            for(int j = 0 ; j < 16 ; j++) {                s.v[i][j] += x.v[i][k] * y.v[k][j] ;                s.v[i][j] %= Mod ;            }        }    }    return s ;}Mat pow(Mat x, LL y) {    Mat s =  Mat(1) ;    for(; y > 0 ; y >>= 1) {        if((y&1) > 0) {            s = mult(s, x) ;        }        x = mult(x, x) ;    }    return s ;}int main() {    memset(a, 0, sizeof(a)) ;    dfs(0, 0, 0) ;    LL n ;    while(scanf("%I64d",&n) != EOF) {        Mat A = Mat(a) ;        A = pow(A, n) ;        printf("%I64d\n", A.v[15][15]) ;    }    return 0 ;}



原创粉丝点击