C. Ivan the Fool and the Probability Theory------思维+dp

第二天叫醒我的不是闹钟,是梦想!

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 n rows and m 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 n and m (1≤n,m≤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
inputCopy
2 3
outputCopy
8
Note
The picture below shows all possible random pictures of size 2 by 3.

//题意:给你一个N*M的方格,有两种黑白色,每个方格相邻最多只有一种颜色与之相同求有多少中涂色方案数。

//解析:如果第一行出现相同的颜色,那么整个图就被确定了,下面一行页一定是确定的。
但是会出现两种情况就是,黑白黑白黑白和白黑白黑白黑。那他们的下一行存在两种情况,要么相等要么相反。但我们可以根据列来当做行去判断。
假设dp[i][0]:给第i个格子涂白色的方案数
dp[i][1]:给第i个格子涂黑色的方案数

在这里插入图片描述
在这里插入图片描述

那么就可以写成dp[i]=dp[i-1]+dp[i-2]。‘
最后要减去2,因为存在上面所说的那两种情况


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int MOD=1e9+7;
typedef long long ll;
ll f[N];
int main()
{
    int n,m;
    cin>>n>>m;
    f[1]=2;f[2]=4;
    for(int i=3;i<=N;i++)
      f[i]=(f[i-1]+f[i-2])%MOD;
    cout<<(f[n]+f[m]-2)%MOD<<endl;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值