LightOJ - 1333 - Grid Coloring
链接:
https://vjudge.net/problem/LightOJ-1333
题意:
You have to color an M x N two dimensional grid. You will be provided K different colors for this. You will also be provided a list of B blocked cells of this grid. You cannot color these blocked cells.
A cell can be described as (x, y), which points to the yth cell from the left of the xth row from the top.
While coloring the grid, you have to follow these rules -
You have to color each cell which is not blocked.
You cannot color a blocked cell.
You can choose exactly one color from K given colors to color a cell.
No two vertically adjacent cells can have the same color, i.e. cell (x, y) and cell (x + 1, y) shouldn't contain the same color.
You have to calculate the number of ways you can color this grid obeying all the rules provided.
思路:
把不能填的位置记录排序,然后枚举每个不能填的位置,同时维护上一个能填的位置,计算之间的种类,乘起来
代码:
// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9;
const int MAXN = 1e6+10;
struct Node
{
int x, y;
bool operator < (const Node& rhs) const
{
if (this->y != rhs.y)
return this->y < rhs.y;
return this->x < rhs.x;
}
}node[510];
int n, m, k, b;
LL PowMod(LL a, LL b, LL p)
{
LL res = 1;
while(b)
{
if (b&1)
res = res*a%p;
a = a*a%p;
b >>= 1;
}
return res;
}
LL GetVal(LL len)
{
if (len <= 0)
return 1;
if (len == 1)
return k;
return k*PowMod(k-1, len-1, MOD)%MOD;
}
int main()
{
// freopen("test.in", "r", stdin);
int t, cas = 0;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d%d", &n, &m, &k, &b);
printf("Case %d:", ++cas);
for (int i = 1;i <= b;i++)
scanf("%d%d", &node[i].x, &node[i].y);
sort(node+1, node+1+b);
int x = 1, y = 1;
LL ans = 1;
if (b == 0)
{
ans = k*PowMod(k-1, n-1, MOD)%MOD;
ans = PowMod(ans, m, MOD);
}
for (int i = 1;i <= b;i++)
{
if (node[i].y == y)
{
LL tmp = GetVal(node[i].x-x);
ans = ans*tmp%MOD;
x = node[i].x+1;
if (x > n)
x = 1, y++;
}
else
{
LL tmp = GetVal(n-x+1);
ans = ans*tmp%MOD;
tmp = GetVal(n);
ans = ans*PowMod(tmp, node[i].y-y-1, MOD)%MOD;
x = 1, y = node[i].y;
i--;
}
// cout << ans << ' ' << x << ' ' << y << endl;
}
if (y <= m && b != 0)
{
LL tmp = GetVal(n-x+1);
ans = ans*tmp%MOD;
tmp = GetVal(n);
ans = ans*PowMod(tmp, m-y, MOD)%MOD;
}
printf(" %lld\n", ans);
}
return 0;
}
LightOJ - 1333 - Grid Coloring的更多相关文章
- AtCoder Regular Contest 080 D - Grid Coloring
地址:http://arc080.contest.atcoder.jp/tasks/arc080_b 题目: D - Grid Coloring Time limit : 2sec / Memory ...
- Atcoder ABC 069 C - 4-adjacent D - Grid Coloring
C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement We have ...
- Gym - 101615J Grid Coloring DP 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)
题目传送门 题目大意: 给出n*m的网格,有红蓝两种颜色,每个格子都必须被染色,当一个格子被染成蓝色后,这个格子左上方的一块都必须被染成蓝色,问最后的方案数量. 思路: 按照题目条件,如果有一个格子被 ...
- AtCoder Beginner Contest 069【A,水,B,水,C,数学,D,暴力】
A - K-City Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement In K-city, ...
- 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)
A. Odd Palindrome 所有回文子串长度都是奇数等价于不存在长度为$2$的偶回文子串,即相邻两个字符都不同. #include<cstdio> #include<cstr ...
- AtCoder Regular Contest 080 [CDEF]
C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Problem Statement We have a sequence of leng ...
- 【AtCoder】ARC080
C - 4-adjacent 我们挑出来4的倍数和不是4的倍数而是2的倍数,和奇数 然后就是放一个奇数,放一个4,如果一个奇数之后无法放4,然后它又不是最后一个,那么就不合法 #include < ...
- AtCoder Beginner Contest 069 ABCD题
题目链接:http://abc069.contest.atcoder.jp/assignments A - K-City Time limit : 2sec / Memory limit : 256M ...
- AStar A* A星 算法TypeScript版本
一 演示效果 二 参考教程 <ActionScript3.0 高级动画教程> + 源码 http://download.csdn.net/download/zhengchengpeng/ ...
随机推荐
- Word章 节标题自动编号并由此自动生成目录 -- 含视频教程(1)
1. 写论文之前事先调好 写论文的时候,难免要用到章节,如何实现下图的效果呢? 其实结合用好两个功能就行了: >> 视频教程链接:B站,速度快,清晰 事先调好,可以给后续工作带来很多便利, ...
- golang开发:环境篇(六) Go运行监控Supervisord的使用
为什么要使用Supervisord 17年第一次写Go项目的时候,用Go开发项目倒没没费多大劲,很快就开发完成了.到了在测试环境部署的时候,由于不知道有 Supervisord 这个软件,着实花了些功 ...
- 2. 运行Spark Streaming
2.1 IDEA编写程序 Pom.xml加入以下依赖: <dependency> <groupId>org.apache.spark</groupId> <a ...
- Shell编程学习(七)
if 条件语句的知识与实践 if 条件语句 if条件语句的语法 单分支结构 第一种 if <条件测试表达式> then 指令 fi 第二种 if <条件测试表达式>; then ...
- JDK提供的并发工具类
1.CountDownLatch await(),进入等待的状态 countDown(),计数器减一 应用场景:启动三个线程计算,需要对结果进行累加. /** * * CountDownLatch D ...
- AOP & 拦截器
https://www.cnblogs.com/boywwj/p/7502185.html spring aop中@after-returning和@after,@afterThrowing,@Aro ...
- C# 多线程与高并发处理并且具备暂停、继续、停止功能
--近期有一个需要运用多线程的项目,会有并发概率,所以写了一份代码,可能有写地方还不完善,后续有需求在改 1 /// <summary> /// 并发对象 /// </summary ...
- Golang/Go goroutine调度器原理/实现【原】
Go语言在2016年再次拿下TIBOE年度编程语言称号,这充分证明了Go语言这几年在全世界范围内的受欢迎程度.如果要对世界范围内的gopher发起一次“你究竟喜欢Go的哪一点”的调查,我相信很多Gop ...
- EF CodeFirst Dome学习
创建ConsoleDome控制台应用程序 从NuGet包管理器安装EntityFramework 创建DbContextDome类并继承DbContext public class DbContext ...
- test aria2 on windows platform
import 'dart:io'; import 'dart:convert'; import 'package:path/path.dart'; import 'package:web_socket ...