Codeforces Round #594 (Div. 1) A. Ivan the Fool and the Probability Theory 动态规划
A. Ivan the Fool and the Probability Theory
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area.
To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of 𝑛 rows and 𝑚 columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side.
Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo 109+7.
Input
The only line contains two integers 𝑛 and 𝑚 (1≤𝑛,𝑚≤100000), the number of rows and the number of columns of the field.
Output
Print one integer, the number of random pictures modulo 109+7.
Example
input
2 3
output
8
Note
The picture below shows all possible random pictures of size 2 by 3.
题意
给你一个n*m的矩阵,然后你进行黑白染色,你最多可以使得每个格子相邻一个格子的颜色相同,问你一共有多少种方案
题解
我们考虑如果第一行和第一列确定之后,我们就发现全部的位置都确定了,这时候我们采用dp去做第一行和第一列的方案数即可。
dp就比较简单了,dp[i]表示第i个格子为黑色的方案数,dp[i]=dp[i-1]+dp[i-2]即可。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int mod = 1e9+7;
long long dp[maxn];
int n,m;
int main(){
dp[0]=1,dp[1]=1;
for(int i=2;i<maxn;i++){
dp[i]=(dp[i-1]+dp[i-2])%mod;
}
cin>>n>>m;
cout<<(dp[n]+dp[m]-1+mod)*2LL%mod<<endl;
}