zoukankan      html  css  js  c++  java
  • HDU

    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×1×2 and 2×2×1 , and the size of the playground is 4×4×n .

    Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?

    InputThere are no more than 5000 test cases.

    Each test case only contains one positive integer n in a line.

    1n10 18  1≤n≤1018
    OutputFor each test cases, output the answer mod 1000000007 in a line.
    Sample Input

    1
    2

    Sample Output

    1
    5

    题意:给定4*N的矩阵,用1*2或者2*1的地毯去覆盖,问多少种方案。

    思路:开始想的状态压缩求出矩阵,但是矩阵是16*16的,T了。然后发现,16割状态里有用的(最后可以到达15这个状态)只有5个:0,3,6,9,15。 所以矩阵的大小为5。就可以过了。

    (至于他们的公式:f(n)=f(n-1)+5*f(n-2)+f(n-3)-f(n-4)。在下是不会推的。

    (状态压缩可以参考此题:https://www.cnblogs.com/hua-dong/p/7967023.html

    #include<bits/stdc++.h>
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define ll long long
    using namespace std;
    const int Mod=1e9+7;struct mat
    {
        ll mp[6][6];
        mat(){memset(mp,0,sizeof(mp)); }
        mat friend operator *(mat a,mat b)
        {
            mat res;
            rep(k,0,5)
             rep(i,0,5)
              rep(j,0,5)
                res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j]%Mod))%Mod;
            return res;
        }
        mat friend operator ^(mat a,ll x)
        {
            mat res;
            rep(i,0,5) res.mp[i][i]=1;
            while(x){
                if(x&1) res=res*a;  a=a*a;  x>>=1;
            }   return res;
        }
    };
    int main()
    {
        ll N; mat base;
        base.mp[0][5]=base.mp[1][3]=base.mp[1][5]=1;
        base.mp[2][5]=base.mp[3][1]=base.mp[3][5]=1;
        base.mp[5][0]=base.mp[5][1]=base.mp[5][2]=1;
        base.mp[5][3]=base.mp[5][5]=base.mp[2][4]=base.mp[4][2]=1;
        while(~scanf("%lld",&N)){
            mat ans; ans.mp[0][5]=1;
            ans=ans*(base^N);
            printf("%d
    ",ans.mp[0][5]);
        }
        return 0;
    }
  • 相关阅读:
    Tomcat:基础安装和使用教程
    java部署
    tomcat 配置访问路径 server.xml配置去掉项目名称 .
    linuxACL控制
    Your PHP installation appears to be missing the MySQL
    ssh报错
    502 Bad Gateway
    单点登录SSO
    tomcat详细介绍
    详解redis5.x版本
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9817183.html
Copyright © 2011-2022 走看看