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;
    }
  • 相关阅读:
    洛谷P1170 兔八哥与猎人 欧拉函数的应用
    洛谷P1056 排座椅
    洛谷P1177 【模板】快速排序
    洛谷1059 明明的随机数
    洛谷P1372 又是毕业季I
    洛谷P1042 乒乓球
    洛谷P1086 花生采摘
    洛谷P1031 均分纸牌
    洛谷P1068 分数线划定
    洛谷P1781 宇宙总统
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9817183.html
Copyright © 2011-2022 走看看