zoukankan      html  css  js  c++  java
  • hiho41 : 骨牌覆盖问题·一

    原问题:骨牌覆盖问题
    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    骨牌,一种古老的玩具。今天我们要研究的是骨牌的覆盖问题:
    我们有一个2xN的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘。对于这个棋盘,一共有多少种不同的覆盖方法呢?
    举个例子,对于长度为1到3的棋盘,我们有下面几种覆盖方式:

    提示:骨牌覆盖

    提示:如何快速计算结果

    输入

    第1行:1个整数N。表示棋盘长度。1≤N≤100,000,000

    输出

    第1行:1个整数,表示覆盖方案数 MOD 19999997

    样例输入
    62247088
    样例输出
    17748018








     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 typedef long long ll;
     6 
     7 const int M = 19999997;
     8 struct Matrix
     9 {
    10     int m[2][2];
    11     Matrix operator*(Matrix& a)
    12     {
    13         Matrix res;
    14         res.m[0][0] = ((ll)m[0][0]*a.m[0][0]+(ll)m[0][1]*a.m[1][0])%M;  // (ll)防止数据溢出
    15         res.m[0][1] = ((ll)m[0][0]*a.m[0][1]+(ll)m[0][1]*a.m[1][1])%M;
    16         res.m[1][0] = ((ll)m[1][0]*a.m[0][0]+(ll)m[1][1]*a.m[1][0])%M;
    17         res.m[1][1] = ((ll)m[1][0]*a.m[0][1]+(ll)m[1][1]*a.m[1][1])%M;
    18         return res;
    19     }
    20 };
    21 
    22 
    23 Matrix pow(Matrix m, int n)
    24 {
    25     Matrix res;
    26     if(1==n)
    27         return m;
    28     res = pow(m, n/2);
    29     if(n%2==1)
    30         res = res*res*m;
    31     else
    32         res = res*res;
    33     return res;
    34 }
    35 
    36 
    37 int main()
    38 {
    39     int N;
    40     cin>>N;
    41 
    42     Matrix mat;
    43     mat.m[0][0]=0;
    44     mat.m[0][1]=1;
    45     mat.m[1][0]=1;
    46     mat.m[1][1]=1;
    47     mat = pow(mat, N);
    48 
    49     cout<<mat.m[1][1];
    50 
    51     return 0;
    52 }


  • 相关阅读:
    招银网络
    MYSQL基础
    http中get和post请求的作用和区别
    设计模式
    STL浅析
    云盾态势感知系统检测到您的服务器出现了紧急安全事件:挖矿木马
    SVN服务器搭建详解--权限划分
    MySQL主主复制,mysql主从复制,MySQL+keepalived故障转移。
    Redhat7.0系统利用amoeba对mysql数据进行读写分离的操作,MySQL数据库的主从配置
    源码安装zabbix LNMP源码安装
  • 原文地址:https://www.cnblogs.com/aituming/p/4452261.html
Copyright © 2011-2022 走看看