链接:

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 -

  1.  You have to color each cell which is not blocked.
  2.  You cannot color a blocked cell.
  3.  You can choose exactly one color from K given colors to color a cell.
  4.  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的更多相关文章

  1. AtCoder Regular Contest 080 D - Grid Coloring

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_b 题目: D - Grid Coloring Time limit : 2sec / Memory ...

  2. 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 ...

  3. Gym - 101615J Grid Coloring DP 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

    题目传送门 题目大意: 给出n*m的网格,有红蓝两种颜色,每个格子都必须被染色,当一个格子被染成蓝色后,这个格子左上方的一块都必须被染成蓝色,问最后的方案数量. 思路: 按照题目条件,如果有一个格子被 ...

  4. 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, ...

  5. 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

    A. Odd Palindrome 所有回文子串长度都是奇数等价于不存在长度为$2$的偶回文子串,即相邻两个字符都不同. #include<cstdio> #include<cstr ...

  6. AtCoder Regular Contest 080 [CDEF]

    C - 4-adjacent Time limit : 2sec / Memory limit : 256MB Problem Statement We have a sequence of leng ...

  7. 【AtCoder】ARC080

    C - 4-adjacent 我们挑出来4的倍数和不是4的倍数而是2的倍数,和奇数 然后就是放一个奇数,放一个4,如果一个奇数之后无法放4,然后它又不是最后一个,那么就不合法 #include < ...

  8. AtCoder Beginner Contest 069 ABCD题

    题目链接:http://abc069.contest.atcoder.jp/assignments A - K-City Time limit : 2sec / Memory limit : 256M ...

  9. AStar A* A星 算法TypeScript版本

    一 演示效果 二  参考教程 <ActionScript3.0 高级动画教程> + 源码 http://download.csdn.net/download/zhengchengpeng/ ...

随机推荐

  1. C++静态库与动态库的区别

    在日常开发中,其实大部分时间我们都会和第三方库或系统库打交道.在 Android 开发音视频开发领域,一般会用到 FFmepg.OpenCV.OpenGL 等等开源库, 我们一般都会编译成动态库共我们 ...

  2. Service must be explitict android 5.0问题

    如果target到API 21,有一些注意的事项,以下是目前我发现的两个问题1. Service must be explitict,从Lollipop开始,service必须显性声明,解决方案:ht ...

  3. 使用adb命令对移动设备截图

    步骤: 1)   首先要进入CMD窗口,命令行模式,连接设备. 2)使用screencap 命令,对安卓设备的当前屏幕进行截屏,示例: adb shell screencap -p /sdcard/0 ...

  4. 唯一ID生成器--雪花算法

    在微服务架构,分布式系统中的操作会有一些全局性ID的需求,所以我们不能用数据库本身的自增功能来产生主键值,只能由程序来生成唯一的主键值.我们采用的是twitter的snokeflake(雪花)算法. ...

  5. 【leetcode-91 动态规划】 解码方法

    一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...

  6. IDEA设置虚拟机参数

    第一步:打开“Run->Edit Configurations”菜单 第二步:选择“VM Options”选项,输入你要设置的VM参数 第三步:点击“OK”.“Apply”后设置完成

  7. form表单提交数据给后台

    1.完整登录示例 1. form表单往后端提交数据注意三点 1.所有获取用户输入标签都应该放在form表单里面 2.action属性控制往哪儿提交,method一般都是设置成post 3.提交按钮必须 ...

  8. 【imx6ul应用开发】如何修改串口?

    4.1如何修改串口?答:开发板已经调好了串口驱动,调试串口,只需要修改dts文件即可,客户可以根据实际需要,确定硬件管脚具体用哪一个. 打开内核源代码/arch/arm/boot/dts/myb-y6 ...

  9. BFC 到底是什么?

    MDN 对 BFC 的描述: 块格式化上下文(Block Formatting Context,BFC) 是Web页面的可视化CSS渲染的一部分,是布局过程中生成块级盒子的区域,也是浮动元素与其他元素 ...

  10. php长连接应用

    php长连接和短连接 2012-12-05 17:25 3529人阅读 评论(0) 收藏 举报  分类: 我的收藏(8)  什么是长连接,如果你没听说过,可以往下看! 长连接到底有什么用?我想你应该见 ...