Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维)

time limit per test:1 second
memory limit per test:512 megabytes
input:standard input
output:standard output

描述

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 nnn rows and mmm 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+710^9+7109+7.

Input

The only line contains two integers nnn and m(1≤n,m≤100000)m (1≤n,m≤100000)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+710^9+7109+7.

Example

input

2 3

output

8

Note

The picture below shows all possible random pictures of size 2 by 3.

题解

题读到一半,联想起状压 dpdpdp ,但是一看到 100000100\ 000100 000 果断放弃。╮(╯▽╰)╭

然后想起普通 dpdpdp ,再一看 nnn 和 mmm 都是 100000100\ 000100 000 果断放弃。╮(╯▽╰)╭

数一下0,嗯,有 555 个,说明 O(nlogn)O(n\ log\ n)O(n log n) 能过,可是又想不到什么用得上的算法,最后决定还是得用 dpdpdp 做。

仔细一想不难发现,当第一行存在两个相同的颜色连在一起的情况时剩下所有的方块只有一种排列方式,也就是说只要第一排确定了且第一排存在两个相同的连在一起的方块,这种情况就只有一种排列方式。形如这样:

当宽度为 mmm 时,定义 dp[i]dp[i]dp[i] 是宽度为 i+1i+1i+1 的(从0开始数)方格的方法数。
那么 dp[0]=2,dp[1]=4dp[0]=2,dp[1]=4dp[0]=2,dp[1]=4

且 dp[i]=dp[i−2]+dp[i−1]dp[i]=dp[i-2]+dp[i-1]dp[i]=dp[i−2]+dp[i−1]

证明如下:


设 dp[i][2]dp[i][2]dp[i][2]

dp[i][0]dp[i][0]dp[i][0] 表示宽度为 i+1i+1i+1 (从0开始数)且下标为i的位置放置白方块的方格的方法数。
那么相应的 dp[i][1]dp[i][1]dp[i][1] 表示宽度为 i+1i+1i+1 (从0开始数)且下标为i的位置放置黑方块的方格的方法数。

所以有 dp[0][0]=1dp[0][0]=1dp[0][0]=1(只有一个格而且只能放白方块,所以只有一种情况);
dp[0][1]=1dp[0][1]=1dp[0][1]=1(只有一个格而且只能放黑方块,所以只有一种情况);
dp[1][0]=2dp[1][0]=2dp[1][0]=2(不论第一个位置是什么颜色,这里都能放白色,故有两种情况)
dp[1][1]=2dp[1][1]=2dp[1][1]=2(不论第一个位置是什么颜色,这里都能放黑色,故有两种情况)


由图可知:dp[i][1]=dp[i−2][0]+dp[i−1][0]dp[i][1]=dp[i-2][0]+dp[i-1][0]dp[i][1]=dp[i−2][0]+dp[i−1][0]


由图可知:dp[i][0]=dp[i−2][1]+dp[i−1][1]dp[i][0]=dp[i-2][1]+dp[i-1][1]dp[i][0]=dp[i−2][1]+dp[i−1][1]

把两式相加会发现 dp[i][0]+dp[i][1]=dp[i−1][0]+dp[i−1][1]+dp[i−2][0]+dp[i−2][1]dp[i][0]+dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-2][0]+dp[i-2][1]dp[i][0]+dp[i][1]=dp[i−1][0]+dp[i−1][1]+dp[i−2][0]+dp[i−2][1]

把 dp[i][0]+dp[i][1]dp[i][0]+dp[i][1]dp[i][0]+dp[i][1] 看成一个整体,方程可以写成 dp[i]=dp[i−2]+dp[i−1]dp[i]=dp[i-2]+dp[i-1]dp[i]=dp[i−2]+dp[i−1]

即长度为 i+1i+1i+1 的一行格子有 dp[i]dp[i]dp[i] 种排列方法,宽度为 mmm 的第一行就有 dp[m−1]dp[m-1]dp[m−1] 种排列方法
而一定包含至少一对相同的块相连的情况是 dp[m−1]−2dp[m - 1]-2dp[m−1]−2 (除去黑白交替的两种情况)

证毕


而剩下的就是第一排所有相同的的方块都不连的情况了。如图:

这个时候会发现, nnn 行方块一共只有两行不一样的状态(先放黑块和先放白块,然后交替出现),把其中一行看作一个白方块,把另一行看作黑方块,问题就变成了在 nnn 行里排列黑白方块,且最多只有一对颜色相同的块互相连接。那么答案显然就是 dp[n−1]dp[n-1]dp[n−1] (证明如上)。

即答案就是 dp[m−1]+dp[n−1]−2dp[m-1]+dp[n-1]-2dp[m−1]+dp[n−1]−2

代码

#include <iostream>
#include <algorithm>
#include <vector>
#define maxn 100005
#define _for(i, a) for(LL i = 0; i < (a); i++)
#define _rep(i, a, b) for(LL i = (a); i <= (b); i++)
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;LL dp[maxn]; //0:do  1:not doint main() {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//freopen("in.txt", "r", stdin);int n, m;cin >> n >> m;dp[0] = 2, dp[1] = 4;if (n < m) swap(n, m);_rep(i, 2, n - 1) {dp[i] = dp[i - 2] + dp[i - 1];dp[i] %= mod;}cout << (dp[n - 1] - 2 + dp[m - 1]) % mod << "\n";return 0;
}

Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维)相关推荐

  1. Codeforces Round #594 (Div. 2) C. Ivan the Fool and the Probability Theory 思维 + dp

    文章目录 题意: 思路 题意: 思路 一开始找规律,表都打好了,没找出来.. 找规律还是适合让队友来. 先考虑第一行,我们先计算第一行的方案数,设f[i][j]f[i][j]f[i][j]表示到了ii ...

  2. A. Ivan the Fool and the Probability Theory

    A. Ivan the Fool and the Probability Theory Problem - 1239A - Codeforces 题意:给定一个n*m的矩阵,每个格子可以涂白色与黑色两 ...

  3. C. Ivan the Fool and the Probability Theory

    C. Ivan the Fool and the Probability Theory 题意: 给n行m列,让构造黑白块,要求每个块周围最多一个和自己颜色相同的块,问最多有多少种构造方法? 思考 : ...

  4. Codeforces Round 700 (Div. 2) B题 英雄杀怪兽

    Codeforces Round 700 (Div. 2) B题 链接: https://codeforces.com/contest/1480/problem/B 大致意思: n组数据,每组数据的第 ...

  5. Codeforces Round #587 (Div. 3) C. White Sheet 思维

    传送门 文章目录 题意: 思路: 题意: 给你一个白色的矩形和俩个黑色的矩形,问白色被黑色覆盖后还能不能看到. 思路: 经典被简单题卡. 一开始写了个自我感觉很对的做法,结果wa41wa41wa41, ...

  6. Codeforces Round #636 (Div. 3) E. Weights Distributing 思维 + bfs

    传送门 文章目录 题意: 思路: 题意: n≤2e5,m≤2e5n\le2e5,m\le2e5n≤2e5,m≤2e5 思路: 怎么感觉每场div3div3div3都有一个巧妙的图论题. 首先如果只有两 ...

  7. Codeforces Round #617 (Div. 3) F. Berland Beauty 思维

    传送门 文章目录 题意: 思路: 题意: 给定一棵树,再给定若干两点最短路之间边权的最小值,让你给树的边权赋值,使得满足给定的条件,如果不存在输出−1-1−1. 思路: 观察一个性质,加入经过这条边的 ...

  8. Codeforces Round #774 (Div. 2)E题题解

    Codeforces Round #774 (Div. 2) E. Power Board 题目陈述 有一个n×m(1≤n,m≤106)n\times m(1\le n,m\le10^6)n×m(1≤ ...

  9. Codeforces Round #594 (Div. 2) Ivan the Fool and the Probability Theory(DP)

    题目链接:https://codeforces.com/contest/1248/problem/C 题目大意:n*m的长方形中,一共有两种颜色,问每一个块相邻最多有一个与之相同颜色的块的染色方案数 ...

最新文章

  1. 后端返回html标签到前端,后端返回的HTML代码字符串怎么能自动渲染到前端页面...
  2. 用python学编程_用Python学编程
  3. java nextintln_Java对正则表达式的支持(二)
  4. 26.中继器数据的添加与删除
  5. 2.2. 3.调度算法的评价指标
  6. 链接服务器访问接口返回了消息没有活动事务,因为链接服务器 SQLEHR 的 OLE DB 访问接口 SQLNCLI10 无法启动分布式事务。...
  7. 各种搜索算法比较--2015年7月26日16:42:45V1.0版
  8. [转]jQuery ListBox Plugin(ListBox插件)
  9. Nginx源码分析 - HTTP模块篇 - HTTP模块的初始化(20)
  10. 使用AdapterTypeRender对不同类型的item数据到UI的渲染
  11. 单片机跑马灯源代码+仿真
  12. 盲盒商城app开发需要具备哪些核心功能,盲盒商城app源码成品分享
  13. WSL下Kali 2021 启用mysql服务并通过主机连接
  14. 企业研发人员配备比例_高新技术企业对研发技术人员占企业总职工人数的比例为多少?...
  15. html超链接访问前颜色,html超链接颜色设置
  16. Git入门——tortoisegit使用问题:git不显示图标?
  17. python图像分析_python数字图像处理(一)图像的常见操作
  18. AI算法面试难度升级,该如何应对?
  19. 智慧文娱,阿里巴巴文娱技术探索之路
  20. Ubuntu 16.04如何安装Cinnamon 3.0

热门文章

  1. A股-指标-boll线全面解析
  2. yii2 adminlte, yii2整合adminlte3
  3. 计算机管理格式化硬盘,解决方案:如何一步一步对硬盘进行分区和格式化
  4. java毕业设计动漫网站Mybatis+系统+数据库+调试部署
  5. 向mycat中插入数据的时候,一定要将字段写完整
  6. WZOI-256奇怪的数列
  7. CSAP_ORD_BOM_MAINTAIN 销售订单BOM
  8. python画图plt函数学习
  9. PageOffice 在线编辑 office文件,回调父页面
  10. 对话CTO | 和优信CTO邱慧聊二手车产业链中的技术价值