Codeforces Round #594 (Div. 1) Ivan the Fool and the Probability Theory
题意:给你一个NxM的图,让你求有多少符合 “一个格子最多只有一个同颜色邻居”的图?
题解:首先我们可以分析一维,很容易就可以知道这是一个斐波那契计数
因为dp[1][m]可以是dp[1][m-1]添加一个和结尾不同颜色的块,或者dp[1][m-2]加上两个和结尾不同颜色的块
为什么dp[1][m-1]不可以添加一个和结尾颜色相同的块呢?因为这样情况就和dp[1][m-2]重叠了。
接着我们再分析多维情况
假设我们有一个3x6的图
第一种情况:
第一行中有相邻同色块,例如 100101
那么很明显,第二行只能是 011010,第三行只能是100101,也就是第一行确定了这个图
第二种情况:
第一行中没有相邻同色块,例如101010
那么很明显,第二行可以选择 101010 或者 010101,第三行可以选择101010或者010101
如果我们一直取与上一行完全相反的,那么就是
101010
010101
101010
再结合第一种情况,那么对答案的贡献就是 dp[1][m]。
除此以外,那么如果我们取和上一行完全相同的情况还没有计入,这时我们可以把图旋转一下,变成6x3的图
同理,这样对答案的贡献就是 dp[1][n],但是由于6x3和3x6的完全黑白相间的图
101010 010101 101 010
010101 101010 010 101
101010 010101 101 010
010 101
101 010
010 101
这两种情况其实是相同的,所以最终答案需要-2 ,也就是 ans= dp[1][n]+dp[1][m]-2
//freopen("in.txt", "r", stdin);
#include <bits/stdc++.h>
#include<unordered_set>
using namespace std;
typedef double dou;
typedef long long LL;
typedef pair<int, int> pii;
typedef map<int, int> mii;
#define pai acos(-1.0)
#define M 200050
#define inf 0x3f3f3f3f
#define mod 1000000007
#define IN inline
#define W(a) while(a)
#define lowbit(a) a&(-a)
#define left k<<1
#define right k<<1|1
#define lson L, mid, left
#define rson mid + 1, R, right
#define ms(a,b) memset(a,b,sizeof(a))
#define Abs(a) (a ^ (a >> 31)) - (a >> 31)
#define random(a,b) (rand()%(b+1-a)+a)
#define false_stdio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
LL n, m;
LL ans[M];
int main() {
false_stdio;
cin >> n >> m;
ans[] = , ans[] = ;
for (int i = ; i <= max(n, m); i++) {
ans[i] = (ans[i - ] + ans[i - ]) % mod;
}
cout << ((ans[n] + ans[m]) % mod + mod - ) % mod << endl;
return ;
}
Codeforces Round #594 (Div. 1) Ivan the Fool and the Probability Theory的更多相关文章
- 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 stu ...
- Codeforces Round #594 (Div. 2)
传送门 C. Ivan the Fool and the Probability Theory 题意: 给出一个\(n*m\)的方格,现在要给方格中的元素黑白染色,要求任一颜色最多有一个颜色相同的格子 ...
- Codeforces Round #594 (Div. 1)
Preface 这场CF真是细节多的爆炸,B,C,F都是大细节题,每道题都写了好久的说 CSP前的打的最后一场比赛了吧,瞬间凉意满满 希望CSP可以狗住冬令营啊(再狗不住真没了) A. Ivan th ...
- Codeforces Round #594 (Div. 2) - C. Ivan the Fool and the Probability Theory(思维)
题意:给n*m的网格涂黑白两种颜色,保证每个格子上下左右的四个格子中最多只有一个格子与自己颜色相同,问有多少种涂法?结果$mod1000000007$ 思路:先只考虑一行有多少种涂法 $dp[i][0 ...
- Codeforces Round #594 (Div. 2) C. Ivan the Fool and the Probability Theory (思维,递推)
题意:给你一个\(n\)x\(m\)的矩阵,需要在这些矩阵中涂色,每个格子可以涂成黑色或者白色,一个格子四周最多只能有\(2\)个和它颜色相同的,问最多有多少种涂色方案. 题解:首先我们考虑一维的情况 ...
- Codeforces Round #594 (Div. 1) D. Catowice City 图论
D. Catowice City In the Catowice city next weekend the cat contest will be held. However, the jury m ...
- Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟
C. Queue in the Train There are
- Codeforces Round #594 (Div. 1) D2. The World Is Just a Programming Task (Hard Version) 括号序列 思维
D2. The World Is Just a Programming Task (Hard Version) This is a harder version of the problem. In ...
- Codeforces Round #594 (Div. 2) B. Grow The Tree 水题
B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...
随机推荐
- java文件的上传
文件的上传和下载在web应用中是非常常用,也是非常有用的功能. 例如:发送电子邮件时可以同过上传附件发送文件,OA系统中可以通过上传文件来提交公文,社交网站通过上传图片来自定义头像等等. 例如:下 ...
- 原生JS写表单验证提交功能
先上效果图: 表单的基础内容就是昵称判断.手机号判断.邮箱判断.身份证号码判断,这里是用到正则验证检验格式. 页面的表单写法就是一个form的提交.输入框用input来实现,输入内容用value来获取 ...
- java核心-多线程(4)-线程类基础知识
1.并发 <1>使用并发的一个重要原因是提高执行效率.由于I/O等情况阻塞,单个任务并不能充分利用CPU时间.所以在单处理器的机器上也应该使用并发. <2>为了实现并发,操作系 ...
- pom.xml报unknown error
1. 从https://start.spring.io/的spring initializr生成demo,使用默认的2.1.6.release(2019年7月10日) 2. 在eclipse加载后,p ...
- centos yum安装redis,nginx
先安装源 yum install epel-release 之后 分别安装redis,nginx
- 13.swoole学习笔记--DNS查询
<?php //执行DNS查询 swoole_async_dns_lookup("www.baidu.com",function($host,$ip){ echo $ip; ...
- JVM探秘:jstack查看Java线程状态
本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. jstack命令可以打印Java进程的各个线程堆栈跟踪信息,可以用来查看Java中各个 ...
- Spring配置数据源的三种方法
前言:今天接触新项目发现用的是JNDI配置数据源,用度娘倒腾了一会也没弄好,只好用平常用的方法,结果发现BasicDataSource和DriverManagerDataSource也是不同的,所以记 ...
- Django 异常处理
我们新建一个py文件 # 在restful中导入exception_handler from rest_framework.views import exception_handler from dj ...
- assert和hasattr,getattr,setattr
assert hasattr(self, 'initial_data'), ( 'Cannot call `.is_valid()` as no `data=` keyword argument wa ...