Codeforces 1239A. Ivan the Fool and the Probability Theory
注意到连续两个格子如果有相同颜色那么一路过去的都可以确定
比如一开始染了这两个位置:
然后发现后面整片过去都可以确定:
对于横着的情况也是一样,然后就会发现不可能出现横着两个和竖着两个同时都有的情况,因为这样一定会冲突,就一定不合法了
(自己画一下就知道了)
那么现在只要对行列分别计算即可,直接设 $f[i][0/1][0/1]$ 表示前 $i$ 个位置,当前位置为 $0/1$ 上一个位置为 $0/1$ 时的方案数
那么转移十分显然,然后答案就是行任意放的方案 $\sum_{i=0}^{1}\sum_{j=0}^{1}f[n][i][j]$ 加上列任意放的方案 $\sum_{i=0}^{1}\sum_{j=0}^{1}f[m][i][j]$ 减 $2$
减 $2$ 是因为黑白染色情况下的方案会被行和列都算到,要减去多算的次数
然后就可以过了,但是事实上如果直接设 $g[i]=\sum_{i=0}^{1}\sum_{j=0}^{1}f[n][i][j]$ ,然后分析原本 $f$ 的方程,那么你会发现 $g[i]$ 恰好等于 $g[i-1]+g[i-2]$
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e5+,mo=1e9+;
inline int fk(int x) { return x>=mo ? x-mo : x; }
int n,m,f[N][][];
int main()
{
n=read(),m=read(); int mx=max(n,m);
if(n==&&m==) { printf("2\n"); return ; }
f[][][]=f[][][]=f[][][]=f[][][]=;
for(int i=;i<=mx;i++)
{
f[i][][]=f[i-][][];
f[i][][]=fk(f[i-][][]+f[i-][][]);
f[i][][]=fk(f[i-][][]+f[i-][][]);
f[i][][]=f[i-][][];
}
if(n>m) swap(n,m);
if(n==) { printf("%d\n", fk(fk(f[m][][]+f[m][][]) + fk(f[m][][]+f[m][][])) ); return ; }
int ans=fk(fk(f[m][][]+f[m][][]) + fk(f[m][][]+f[m][][]));
ans=fk(ans-+mo);
ans=fk(ans+ fk(fk(f[n][][]+f[n][][]) + fk(f[n][][]+f[n][][])));
printf("%d\n",ans);
}
Codeforces 1239A. Ivan the Fool and the Probability Theory的更多相关文章
- Codeforces 1248C Ivan the Fool and the Probability Theory(推公式)
题意 一个n*m的网格图,每个格子可以染黑色.白色,问你每个格子最多有一个相邻颜色相同的方案数 n,m<=1e5 思路 我们先处理\(1 \times m\)的情况 设\(f[i][j]\)为前 ...
- 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的网格涂黑白两种颜色,保证每个格子上下左右的四个格子中最多只有一个格子与自己颜色相同,问有多少种涂法?结果$mod1000000007$ 思路:先只考虑一行有多少种涂法 $dp[i][0 ...
- Codeforces Round #594 (Div. 1) Ivan the Fool and the Probability Theory
题意:给你一个NxM的图,让你求有多少符合 “一个格子最多只有一个同颜色邻居”的图? 题解:首先我们可以分析一维,很容易就可以知道这是一个斐波那契计数 因为dp[1][m]可以是dp[1][m-1]添 ...
- Codeforces Round #594 (Div. 2) C. Ivan the Fool and the Probability Theory (思维,递推)
题意:给你一个\(n\)x\(m\)的矩阵,需要在这些矩阵中涂色,每个格子可以涂成黑色或者白色,一个格子四周最多只能有\(2\)个和它颜色相同的,问最多有多少种涂色方案. 题解:首先我们考虑一维的情况 ...
- CF1239A Ivan the Fool and the Probability Theory
思路: 可以转化为“strip”(http://tech-queries.blogspot.com/2011/07/fit-12-dominos-in-2n-strip.html)问题.参考了http ...
- CF C.Ivan the Fool and the Probability Theory【思维·构造】
题目传送门 题目大意: 一个$n*m$的网格图,每个格子可以染黑色.白色,问每个格子最多有一个相邻格子颜色相同的涂色方案数$n,m<=1e5$ 分析: 首先,考虑到如果有两个相邻的格子颜色相同, ...
- C - Ivan the Fool and the Probability Theory---div2
题目连接:https://codeforces.com/contest/1248/problem/C 思路: 注意上下两排的关系,如果说上面那一排有两个方格连续,那么他相邻的两排必定和他相反,如果说当 ...
- CodeForces 1100F Ivan and Burgers
CodeForces题面 Time limit 3000 ms Memory limit 262144 kB Source Codeforces Round #532 (Div. 2) Tags da ...
随机推荐
- sql到python正则
import urllib.requestimport re,timeresult=[]for i in range(100): urls ="http://xxx.com/-1%20 ...
- Java第00周作业
---恢复内容开始--- 1.你对专业的认识和期望是什么? 徐老师说过,C语言是很多语言的基础,学好C语言,将来接触的不论是面向对象还是面向过程的语言都能沉下心,能轻松解决面临的问题,处理棘手的麻烦, ...
- 在linux上使用impdp命令时提示ORA-12154: TNS:could not resolve the connect identifier specified的问题
今天在一台linux服务器上用impdp命令导入dmp文件时出现了错误: ORA: TNS:could not resolve the connect identifier specified 我使用 ...
- postgresql获取表最后更新时间(通过触发器将时间写入另外一张表)
通过触发器方式获取表最后更新时间,并将时间信息写入到另外一张表 一.创建测试表和表记录更新时间表 CREATE TABLE weather( city varchar(80), temp_lo int ...
- 检查并解决CentOS 7 安装Tomcat是否成功
参考网址 https://blog.csdn.net/Blue_Sky_rain/article/details/91348791
- element-ui el-upload http-request自定义上传方法
html <el-form-item class="upload-bg register-bg" prop="ad_url"> <div cl ...
- 6and7.Pod控制器应用进阶
Pod控制器应用进阶:imagepullpolicy: 镜像获取策略 Always,Never,IfNoPresent 暴露端口: portslabels 标签可以后期添加修改. ========== ...
- HmacSha1加密-java
package com.test; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache ...
- AIX系统命令
当/oracle达到一定使用量时需清除*.trc文件find . -name "*.trc"|xargs rm -rf 1.查看是否开启归档日志切换用户:su - oracle使用 ...
- React Native面试题记录
一.ES6中增加了那些新的功能? 1> for-of循环: 使用 for-of 的时候,循环的是数组内部的元素且不会出现 for-in 中将附加属性也遍历的情况,其次,循环变量的类型和其在数组中 ...