CF293B. Distinct Paths
2 seconds
256 megabytes
standard input
standard output
You have a rectangular n × m-cell board. Some cells are already painted some of k colors. You need to paint each uncolored cell one of the k colors so that any path from the upper left square to the lower right one doesn't contain any two cells of the same color. The path can go only along side-adjacent cells and can only go down or right.
Print the number of possible paintings modulo 1000000007 (109 + 7).
The first line contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10). The next n lines contain mintegers each — the board. The first of them contains m uppermost cells of the board from the left to the right and the second one containsm cells from the second uppermost row and so on. If a number in a line equals 0, then the corresponding cell isn't painted. Otherwise, this number represents the initial color of the board cell — an integer from 1 to k.
Consider all colors numbered from 1 to k in some manner.
Print the number of possible paintings modulo 1000000007 (109 + 7).
- 2 2 4
0 0
0 0
- 48
- 2 2 4
1 2
2 1
- 0
- 5 6 10
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
- 3628800
- 2 6 10
1 2 3 4 5 6
0 0 0 0 0 0
- 4096
暴搜即可,有一个强力剪枝,对于当前所有未出现的颜色,接下来的状态是一致的,于是只用算一次就行了。
- #include <bits/stdc++.h>
- using namespace std;
- namespace my_useful_tools {
- #define rep(_i, _k, _j) for(int _i = _k; _i <= _j;++_i)
- #define foreach(_i, _s) for(typeof(_s.begin()) _i = _s.begin(); _i != _s.end();++_i)
- #define pb push_back
- #define mp make_pair
- #define ipir pair<int, int>
- #define ivec vector<int>
- #define clr(t) memset(t,,sizeof t)
- #define pse(t, v) memset(t, v,sizeof t)
- #define brl puts("")
- #define file(x) freopen(#x".in","r", stdin), freopen(#x".out","w", stdout)
- const int INF = 0x3f3f3f3f;
- typedef long long LL;
- typedef double DB;
- inline void pc(char c) { putchar(c); }
- template<class T> inline T gcd(T a, T b) { return b == ? a : gcd(b, a % b); }
- template<class T> inline void W(T p) { if(p < ) pc('-'), p = -p; if(p / != ) W(p / ); pc('0' + p % ); } // warning!! slower than printf
- template<class T> inline void Wn(T p) { W(p), brl; } template<class T> inline void W(T a, T b) { W(a), pc(' '), W(b); }
- template<class T> inline void Wn(T a, T b) { W(a), pc(' '), Wn(b); }
- template<class T> inline void W(T a, T b, T c) { W(a), pc(' '), W(b), pc(' '), W(c); }
- inline char gchar() { char ret = getchar(); for(; ret == '\n' || ret == '\r' || ret == ' '; ret = getchar()); return ret; }
- template<class T> inline void fr(T&ret) { char c = ' '; int flag = ; for(c = getchar(); c != '-' && !('0' <= c && c <= '9'); c = getchar());
- if(c == '-') flag = -, ret = ; else ret = c - '0'; for(c = getchar(); '0' <= c && c <= '9'; c = getchar()) ret = ret * + c - '0';
- ret = ret * flag;
- }
- inline int fr() { int x; fr(x); return x; }
- template<class T> inline void fr(T&a, T&b) { fr(a), fr(b); } template<class T> inline void fr(T&a, T&b, T&c) { fr(a), fr(b), fr(c); }
- template<class T> inline T fast_pow(T base, T index, T mod = , T ret = ) {
- for(; index; index >>= , base = base * base % mod) if(index & ) ret = ret * base % mod;
- return ret;
- }
- const int maxv = , maxe = ;
- struct Edge {
- int edge, head[maxv], to[maxe], next[maxe];
- Edge() { edge = ; memset(head, -, sizeof head); }
- void addedge(int u, int v) {
- to[edge] = v, next[edge] = head[u];
- head[u] = edge++;
- }
- };
- };
- using namespace my_useful_tools;
- class DistinctPaths {
- static const int mod = 1e9 + ;
- static const int maxn = + ;
- static const int maxs = << ;
- int n, m, k;
- int a[maxn][maxn], ans, log2[maxs], f[maxn][maxn];
- int cnt[maxn];
- int dfs(int x, int y) {
- if(y == m + ) {
- y = ;
- ++x;
- }
- if(x == n + ) {
- return ;
- }
- int s = f[x - ][y] | f[x][y - ];
- int ret = ;
- int calced = -;
- for(int t = (~s) & (( << k) - ); t; t -= t & (-t)) {
- int p = log2[t & -t] + ;
- if(a[x][y] == || a[x][y] == p) {
- ++cnt[p];
- f[x][y] = s | (t & -t);
- if(cnt[p] == ) {
- if(calced == -) calced = dfs(x, y + );
- ret += calced;
- } else if(cnt[p]) {
- ret += dfs(x, y + );
- }
- --cnt[p];
- if(ret >= mod) ret -= mod;
- }
- }
- return ret;
- }
- int solve() {
- fr(n, m, k);
- if(k < n + m - )
- return ;
- rep(i, , n) rep(j, , m)
- fr(a[i][j]), ++cnt[a[i][j]];
- rep(i, , k) log2[ << i] = i;
- return dfs(, );
- }
- public:
- void run() {
- Wn(solve());
- }
- } solver;
- int main() {
- solver.run();
- return ;
- }
CF293B. Distinct Paths的更多相关文章
- CF293B Distinct Paths题解
CF293B Distinct Paths 题意 给定一个\(n\times m\)的矩形色板,有kk种不同的颜料,有些格子已经填上了某种颜色,现在需要将其他格子也填上颜色,使得从左上角到右下角的任意 ...
- CF293B Distinct Paths 搜索
传送门 首先数据范围很假 当\(N + M - 1 > K\)的时候就无解 所以对于所有要计算的情况,\(N + M \leq 11\) 超级小是吧,考虑搜索 对于每一个格子试填一个数 对于任意 ...
- [codeforces 293]B. Distinct Paths
[codeforces 293]B. Distinct Paths 试题描述 You have a rectangular n × m-cell board. Some cells are alrea ...
- [CF293B]Distinct Paths_搜索_剪枝
Distinct Paths 题目链接:http://codeforces.com/problemset/problem/293/B 数据范围:略. 题解: 带搜索的剪枝.... 想不到吧..... ...
- Codeforces 293B Distinct Paths DFS+剪枝+状压
目录 题面 题目链接 题意翻译 输入输出样例 输入样例#1 输出样例#1 输入样例#2 输出样例#2 输入样例#3 输出样例#3 输入样例#4 输出样例#4 说明 思路 AC代码 总结 题面 题目链接 ...
- 考前停课集训 Day1 废
[友情链接] Day1 今天模拟赛倒数…… 感觉自己菜到爆炸…… 被一个以前初一的倒数爆踩…… 感觉自己白学了. 满分400,自己只有100.真的是倒数第一…… 做了一个T2,其他暴力分全部没有拿到… ...
- uva 10564
Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seco ...
- Codeforce 水题报告
最近做了好多CF的题的说,很多cf的题都很有启发性觉得很有必要总结一下,再加上上次写题解因为太简单被老师骂了,所以这次决定总结一下,也发表一下停课一星期的感想= = Codeforces 261E M ...
- 关于PJ 10.27
题1 : Orchestra 题意: 给你一个 n*m 的矩阵,其中有一些点是被标记过的. 现在让你求标记个数大于 k 个的二维区间个数. n.m .k 最大是 10 . 分析: part 1: 10 ...
随机推荐
- PDF文本内容批量提取到Excel
QQ:231469242,版权所有 sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269 ...
- Hadoop生态圈-Hive快速入门篇之HQL的基础语法
Hadoop生态圈-Hive快速入门篇之HQL的基础语法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客的重点是介绍Hive中常见的数据类型,DDL数据定义,DML数据操作 ...
- bzoj千题计划119:bzoj1029: [JSOI2007]建筑抢修
http://www.lydsy.com/JudgeOnline/problem.php?id=1029 把任务按截止时间从小到大排序 如果当前时间+当前任务耗时<=当前任务截止时间,把这个任务 ...
- noi题库(noi.openjudge.cn) 1.13编程基础之综合应用 T12 分数求和
12:分数求和 描述 输入n个分数并对他们求和,并用最简形式表示.所谓最简形式是指:分子分母的最大公约数为1:若最终结果的分母为1,则直接用整数表示. 如:5/6.10/3均是最简形式,而3/6需要化 ...
- HDU 4135 容斥
问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...
- easy-animation | Animation for Sass
最近因为项目缘故,勾搭上了Sass. 其实在折腾Sass之前,也有简单用过一下Less.但碍于Less提供的一些API实在让人觉得有点多余,用着就是不顺手,最后就不了了之啦. Sass之所以用起来舒服 ...
- 从咖啡馆的经营看 Web 应用的扩展
我经营着一家咖啡馆.经营成本同所用的资源成正比. 我的咖啡馆店面大概有一百平方英尺(约九平方米),雇佣了一个咖啡师,一台咖啡机. 营业能力: 每次能够服务一个顾客,用三分钟泡制一杯咖啡,算下来服务一个 ...
- 三个你不知道的CSS技巧
各种浏览器之间的竞争的白热化意味着越来越多的人现在开始使用那些支持最新.最先进的W3C Web标准的设备,以一种更具交互性的方式来访问互联网.这意味着我们终于能够利用更强大更灵活的CSS来创造更简洁, ...
- asp.net WebForm程序删除.designer.cs文件之后的故事
1.介绍 正常情况下添加一个WebForm程序结构如下(命名为:myWebForm.aspx) 文件说明:.aspx文件:书写html代码部分,以及javascript,css等代码书写及引用 .as ...
- 【CodeForces】961 G. Partitions 斯特林数
[题目]G. Partitions [题意]n个数$w_i$,每个非空子集S的价值是$W(S)=|S|\sum_{i\in S}w_i$,一种划分方案的价值是所有非空子集的价值和,求所有划分成k个非空 ...