题目1 : 骨牌覆盖问题·一
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
骨牌,一种古老的玩具。今天我们要研究的是骨牌的覆盖问题:
我们有一个2xN的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘。对于这个棋盘,一共有多少种不同的覆盖方法呢?
举个例子,对于长度为1到3的棋盘,我们有下面几种覆盖方式:
week41_1.PNG
提示:骨牌覆盖
提示:如何快速计算结果
输入
第1行:1个整数N。表示棋盘长度。1≤N≤100,000,000
输出
第1行:1个整数,表示覆盖方案数 MOD 19999997
样例输入
62247088
样例输出
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
骨牌,一种古老的玩具。今天我们要研究的是骨牌的覆盖问题:
我们有一个2xN的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘。对于这个棋盘,一共有多少种不同的覆盖方法呢?
举个例子,对于长度为1到3的棋盘,我们有下面几种覆盖方式:
week41_1.PNG
提示:骨牌覆盖
提示:如何快速计算结果
输入
第1行:1个整数N。表示棋盘长度。1≤N≤100,000,000
输出
第1行:1个整数,表示覆盖方案数 MOD 19999997
样例输入
62247088
样例输出
17748018
#include<iostream>
int c = 19999997;
using namespace std;
long long int a[2][2] = { 0, 1, 1, 1 };
long long int PowerMod(int b){
int ans[2] = { 0, 1 };
while (b > 0){
if (b % 2 == 1){
long long int temp[2];
temp[0] = ans[0], temp[1] = ans[1];
ans[0] = (temp[0] * a[0][0] + temp[1] * a[1][0])%c;
ans[1] = (temp[0] * a[0][1] + temp[1] * a[1][1])%c;
}
long long int temp[2][2] = { a[0][0], a[0][1], a[1][0], a[1][1] };
a[0][0] = (temp[0][0] * temp[0][0] + temp[0][1] * temp[1][0])%c;
a[0][1] = (temp[0][0] * temp[0][1] + temp[0][1] * temp[1][1])%c;
a[1][0] = (temp[1][0] * temp[0][0] + temp[1][1] * temp[0][1])%c;
a[1][1] = (temp[1][0] * temp[0][1] + temp[1][1] * temp[1][1])%c;
b = b / 2;
}
return ans[1];
}
int main(){
int n;
while (cin >> n){
cout << PowerMod(n) << endl;
a[0][0] = 0;
a[1][0] = 1;
a[0][1] = 1;
a[1][1] = 1;
}
}