1. 程式人生 > >hdu 6185 Covering

hdu 6185 Covering

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.

1≤n≤1018


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


Sample Input
1
2


Sample Output
1

5

沒有想到有一樣的題:poj 3420。

然後就是自己有點難想通的地方是:

a[n] = a[n - 1] + b[n - 1] + c[n - 1] + d[n - 1]
b[n] = a[n - 1]//這裡不是 b[n] = a[n-1]+b[n-1]的原因就是 b[n]表示的是放完n行後n+1行的狀態,b[n-1]已經算放完n行了所以不能再放 ,從而不能推出b[n].所以b[n-1]的狀態只能推到a[n]

c[n] = a[n - 1] + e[n - 1]
d[n] = a[n - 1] * 2 + d[n - 1]
e[n] = c[n - 1]

自己的程式碼:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec>mat;

const ll Mod = 1000000007;

ll n;
mat mul(mat &A,mat &B)
{
    mat C(A.size(),vec(B[0].size()));
    for(int i=0,l=A.size(); i<l; i++)
    {
        for(int k=0,l1=B.size(); k<l1; k++)
        {
            for(int j=0,l2=B[0].size(); j<l2; j++)
            {
                C[i][j] = (C[i][j] + A[i][k]*B[k][j])%Mod;
            }
        }
    }
    return C;
}
mat matpow(mat A,ll n)
{
    mat B(A.size(),vec(A.size()));
    for(int i=0,l=A.size(); i<l; i++)
    {
        B[i][i] = 1;
    }
    while(n>0)
    {
        if(n&1) B=mul(B,A);
        A = mul(A,A);
        n>>=1;
    }
    return B;
}
void solve()
{
    mat A(5,vec(5));
    mat C(5,vec(1));
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++) A[i][j] = 0,C[i][0]=0;
    A[0][0] = A[0][1] = A[0][2] = A[0][3] =  1;
    A[1][0] = 1, A[2][0] = 1, A[2][4] = 1, A[3][0] = 2, A[3][3] = 1, A[4][2] = 1;

    A = matpow(A,n), C[0][0] = 1;
    C = mul(A,C);

    printf("%I64d\n",C[0][0]%Mod);
}
int main()
{
    while(~scanf("%I64d",&n))
    {
        solve();
    }
    return 0;
}


相關推薦

hdu 6185 Covering

Problem Description Bob's school has a big playground, boys and girls always play games here after school. To protect boys and girls from

hdu 6185 Covering(矩陣快速冪)

Covering Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 493    Accepted Submi

【2017廣西邀請賽】hdu 6185 Covering 矩陣快速冪

Problem Description Bob's school has a big playground, boys and girls always play games here after school. To protect boys and girls f

HDU 6185 Covering (2017 廣西邀請賽重現賽)(矩陣快速冪)

題意:用1x2或者2x1的地毯去鋪滿4xN的地面。給定N,問方案數.          (偶見DFS暴力搜尋前10個結果,學習中,稍後貼上) 解法:推匯出公式後,轉換成矩陣快速冪求解即可. /*dfs

hdu 6185 Covering 求遞推式的板子

CS Course Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0 Accepted

HDU - 6185Covering(矩陣乘法&狀態壓縮)

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 happi

HDU 6172 and HDU 6185 【線性遞推 + 思維 + 板子】

這兩道題都是給的線性遞推式(輸入只有一個未知數n), 那麼我寫這個部落格的目的就是儲存一個超強模板, 可以解決任何線性遞推式. 這個板子是我從百度之星複賽上”偷”的杜教的板子. 所以我們現在要做的是用絕對正確的方法求出遞推式的前幾項. 然後扔進這個板子就可以了.

6185 Covering (高斯消元 矩陣快速冪)

CoveringTime Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1774    Accepted Submissio

6185 Covering (高斯消元 矩陣快速冪)

本文參考自: 原文地址 Covering Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1774    Ac

Covering HDU

題目 題意 4*n的方格塊 要用1*2和2*1的矩形填滿,問填發。 解題思路 : n最大為1e18,。 考慮矩陣快速冪。 先用狀壓DP打出前面幾項,然後for迴圈一個一個試求出遞推式係數。。。。

hdu 4719 Oh My Holy FFF(dp線段樹優化)

origin end should adding href ast left code padding Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535

HDU 1087 Super Jumping! Jumping! Jumping!

blog col som oss score ++ all you finall Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU.

HDU 1114 Piggy-Bank(完全背包)

ask style span ica ace eno eterm ++ empty 題目: Before ACM can do anything, a budget must be prepared and the necessary financial support o

Hdu-1072

std bits -1 簡單 pty one int 個數字 一行 題目描述: 首先輸入一個N;代表測試數據的個數; 然後每個測試數據的開頭第一行輸入一個n和一個命令(FIFO或FILO<就是先進先出或先進後出>) 然後是該測試數據的n行,每行包括“IN”加一個

hdu 1284 錢幣兌換問題

print class mat 題目 能夠 ims 遞推 detail acm 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1284 思路:僅僅有3個硬幣,範圍是32768。能夠一個一個枚舉硬幣。假設僅僅放價值為1的硬幣

HDU 2648(搜索題,哈希表)

cin ostream pac cstring using mem == ring scan #include<iostream> #include<map> #include<string> #include<cstring&

hdu 5078

fine title tle only asi art it is text 現場 Osu! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T

HDU 2112 HDU Today(STL MAP + Djistra)

stl eof dji int ble mes tin std n) 題目鏈接:HDU Today 立即集訓要開始,抓緊時間練練手,最短路的基礎題,第一次用STL的map 題目非常水,可是錯了N遍。手賤了。本題不優點理的就是把地名轉化為數字 #include

Super Jumping! Jumping! Jumping! HDU - 1087

std idt scribe cas abs roc assume led cor Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU

最大連續子序列 HDU - 1231

bsp 最大 程序 結構 %d col 最小 元素 class 給定K個整數的序列{ N1, N2, ..., NK },其任意連續子序列可表示為{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大連續子序列是所有連續子