B - Chessboard
B - Chessboard
分析:
阅读理解题,组合数学,神仙思维
" the shortest distance between any two blocks would remain unchanged after they were painted"
主要是这句话,不能让任意两个访问过的格子的最短距离发生改变,想一下可以发现以下几条规则:
首先,从始至终都是在同一个连通块,然后不断向外扩展
到最后,画满的情况下,任意两点的最短路都是曼哈顿距离
最后,结束的地方一定是四角之一
为了满足上述几个规则,在画的过程中,在同一列(同一行)出现未被访问的格子夹在已访问的格子中间的情况是绝对不允许的
为了避免这种情况的发生,就要按照矩形的方式去扩展(一行一行,一列一列)
刚开始,选一个点,即1×11\times 11×1的矩形,那么接下来,就要变成2×12\times12×1 or 1×21\times21×2的矩形
若变成r×cr\times cr×c,接下来扩展行((r+1)×c(r+1)\times c(r+1)×c) 扩展列(r×(c+1)r\times(c+1)r×(c+1))
到最终变成n×mn\times mn×m,总共是扩展(n+m−2)(n+m-2)(n+m−2)次,行扩展(n−1)(n-1)(n−1)次,贡献为(n−1n+m−2)(^{n+m-2}_{n-1})(n−1n+m−2)
再根据终点不同,逆推回去,扩展方式都是唯一的,方案数再乘4即可
注意:特判n=1 or m=1 的情况
#include <bits/stdc++.h>
#define int long long
using namespace std;const int N=2e6+5, mo=1e9+7;
int ksm(int a,int b,int p=mo)
{int ans=1;while(b){if(b&1) ans=ans*a%p;b>>=1; a=a*a%p;}return ans;
}
int fac[N], inv[N];
void init(int n)
{fac[0]=inv[0]=1;for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mo; inv[n]=ksm(fac[n],mo-2);for(int i=n-1;i>=1;i--) inv[i]=inv[i+1]*(i+1)%mo;
}
int C(int n,int m) // 组合数
{return fac[n]*inv[m]%mo*inv[n-m]%mo;
}
int f[N];
void solve()
{int n,m;scanf("%lld%lld",&n,&m);if(n==1 && m==1) cout<<"1\n";else if(n==1 || m==1) cout<<"2\n";else cout<<C(n+m-2,n-1)*4%mo<<"\n";
}
signed main()
{init(N-2);int t=1;cin>>t;while(t--) solve();
}
B - Chessboard相关推荐
- UESTC 1851 Kings on a Chessboard
状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...
- Numbers on the Chessboard
You are given a chessboard of size n×nn×n. It is filled with numbers from 11 to n2n2 in the followin ...
- [poj2446]Chessboard
Description 给定一个m×n的棋盘,上面有k个洞,求是否能在不重复覆盖且不覆盖到洞的情况下,用2×1的卡片完全覆盖棋盘. Input 第一行有三个整数n,m,k(0<m,n<=3 ...
- 【CodeForces - 1027B 】Numbers on the Chessboard (没有营养的找规律题,无聊题)
题干: You are given a chessboard of size n×nn×n. It is filled with numbers from 11 to n2n2 in the foll ...
- Numbers on the Chessboard(CF-1027B)
Problem Description You are given a chessboard of size n×n. It is filled with numbers from 1 to n^2 ...
- Codewars-The wheat/rice and chessboard problem(棋盘放谷粒)
棋盘放谷粒 科学作家大卫·布拉特纳说,从前,有一个骗子为高端客户制作棋盘--在这个例子中,他是一个国王. 工匠很好;他的棋盘不仅仅是漂亮.他知道,国王喜欢下棋.于是他想出一个计谋,骗国王交出一大笔财产 ...
- LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...
- UVA10284 POJ2512 Chessboard in FEN【国际象棋】
In the FEN (Forsyth-Edwards Notation), a chessboard is described as follows: • The Board-Content is ...
- UVA10751 Chessboard【数学水题】
A king wishes to go for a walk of a square chessboard with the following conditions: • Each two cons ...
- POJ1657 Bailian1657 Distance on Chessboard【基础】
Distance on Chessboard 描述 国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间.如下图所示: 王.后.车.象的走子规则如下: 王:横.直.斜都可以走,但每步限走一格. ...
最新文章
- Nature:iHMP之“微生物组与炎症性肠病”
- SQLite复杂表的更新方式
- python 枚举相等判断_python中is和==的区别
- Virtualbox 无缝整合linux和windows双系统
- sjf调度算法_如何通过静态方法预测SJF调度中未来过程的突发时间?
- Python 进程池 multiprocessing.Pool - Python零基础入门教程
- Storm-源码分析-Topology Submit-Client
- 2.1 Mini-batch 梯度下降法
- A - 敌兵布阵 - hdu 1166
- 电力电子转战数字IC20220610day21——杂七杂八
- pe系统服务器怎么做双盘备份,韩博士教你如何使用pe备份系统
- 伍斯特理工学院计算机专业,伍斯特理工学院计算机科学专业
- 部署项目启动提示找不到locahost:8080.....地址问题
- ParticleDesigner 粒子编辑器使用
- 【信号与系统】笔记(4-1)拉普拉斯变换
- QQ邮箱导出的通讯录出现乱码怎么办?
- apple 密码 seeion has time out 无法再本机上重设密码
- 三分的多种写法及对应的精度 三分套三分原理
- deny all后如何优雅的处理403
- 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)
热门文章
- 【笔记】文献阅读[SORT]-SIMPLE ONLINE AND REALTIME TRACKING
- php like,关于where like
- python 替换文本 通配符_python替换word中的关键文字(使用通配符)
- python使用socket进行多线程收发与路径计算模拟路由器网络
- 绘图操作时报错“无法从带有索引像素格式的图像创建 Graphics 对象”
- python写出表白_简单python 表白代码
- 如何做个让人喜欢的程序员
- 学习笔记_latex
- js制作秒表计时器代码怎么写
- HTML img标签水平垂直居中