zoukankan      html  css  js  c++  java
  • HDU-6185-Covering(推递推式+矩阵快速幂)

    Covering

    Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3078    Accepted Submission(s): 1117


    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
    1 2
     
    Sample Output
    1 5

     题意:4xn的地面,用1x2或者2x1的地毯自由组合铺满,有几种方案(答案mod 1e9+7)

    解题思路:(草稿纸冲冲冲)

                    

    有了递推式,矩阵快速幂就好了

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 typedef unsigned long long ull;
     5 #define INF 0X3f3f3f3f
     6 const ll MAXN = 4;
     7 const ll mod = 1e9 + 7;
     8 //矩阵的大小 模数
     9 ll n;
    10 struct MAT
    11 {
    12     ll mat[MAXN][MAXN];
    13     MAT operator*(const MAT &a) const
    14     {
    15         //重载矩阵乘法
    16         MAT b;
    17         memset(b.mat, 0, sizeof(b.mat));
    18         for (int i = 0; i < MAXN; i++)
    19         {
    20             for (int j = 0; j < MAXN; j++)
    21             {
    22                 for (int k = 0; k < MAXN; k++)
    23                     b.mat[i][j] = (b.mat[i][j] + mat[i][k] * a.mat[k][j]);
    24                 b.mat[i][j] += mod;
    25                 b.mat[i][j] %= mod;
    26             }
    27         }
    28         return b;
    29     }
    30 } start, ans;
    31 MAT Mqpow(MAT base, ll b)
    32 {
    33     MAT r;
    34     memset(r.mat, 0, sizeof(r.mat));
    35     r.mat[0][0] = 1, r.mat[1][0] = 5, r.mat[2][0] = 11, r.mat[3][0] = 36;
    36     //初始状态
    37     while (b)
    38     {
    39         if (b & 1)
    40             r = base * r;
    41         base = base * base;
    42         b >>= 1;
    43     }
    44     return r;
    45 }
    46 int main()
    47 {
    48 
    49     start.mat[0][0] = 0, start.mat[0][1] = 1, start.mat[0][2] = 0, start.mat[0][3] = 0;
    50     start.mat[1][0] = 0, start.mat[1][1] = 0, start.mat[1][2] = 1, start.mat[1][3] = 0;
    51     start.mat[2][0] = 0, start.mat[2][1] = 0, start.mat[2][2] = 0, start.mat[2][3] = 1;
    52     start.mat[3][0] = -1, start.mat[3][1] = 1, start.mat[3][2] = 5, start.mat[3][3] = 1;
    53     //建立转移矩阵
    54     ll f[5] = {0, 1, 5, 11, 36};
    55     while (~scanf("%lld", &n))
    56     {
    57         if (n <= 4)
    58             printf("%lld
    ", f[n] % mod);
    59         else
    60             printf("%lld
    ", Mqpow(start, n - 4).mat[3][0]);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    python day01学习
    标准化体系建设(下):如何建立基础架构标准化及服务化体系?
    用EL表达式与Java代码的共享数据
    String和StringBuilder的相互转化
    博客园的一个bug_修改文章标签
    int const *p和int *const的区别
    C++ assert()的用法
    java中,为什么char类型数组可以直接用数组名打印,而int型数组打印结果是地址值!
    传说中的栈溢出
    Trello
  • 原文地址:https://www.cnblogs.com/graytido/p/10885103.html
Copyright © 2011-2022 走看看