链接:

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. docker的容器和镜像的清理

    Docker用户会在使用docker一段时间后发现宿主机的磁盘很容易就快被占满,并且手动docker rmi [imgName]似乎并不能释放磁盘,貌似想删掉的镜像依然在宿主机中,下面针对这一问题提出 ...

  2. [洛谷P4385][COCI2009]Dvapravca(咕咕咕)

    题目大意:很早以前做的题 题解: 卡点: C++ Code: #pragma GCC optimize("Ofast") #pragma GCC optimize("un ...

  3. AutoCAD ObjectARX 二次开发(2020版)--3,执行ARX文件--

    上一节中我们在initApp()函数中,将helloWorld()函数注册给了CAD主程序,注册指令的字符串为“Hello”. void initApp() { acedRegCmds->add ...

  4. ELK学习笔记之配置logstash消费kafka多个topic并分别生成索引

    0x00 filebeat配置多个topic filebeat.prospectors: - input_type: log encoding: GB2312 # fields_under_root: ...

  5. x.append()增加不同维度的区别

    b=np.array([[7,2],[2,4],[3,6],[7,8],[9,10]])print(b)print(type(b)) # 结果显示为nunmpy 数组a=[]for i in rang ...

  6. js事件(十二)

    一.事件三要素1.事件目标[谁触发的该事件(引起该事件触发的源头:target)]2.事件处理程序[处理相应事件的函数]3.事件对象[触发事件产生的携带事件信息的对象] 二.事件流[从页面中接受事件的 ...

  7. ABAP-JCO服务错误

    1.错误如下图,需SM59调整Unicode,保持JCO服务启动时与SAP-SM59设置Unicode一致.

  8. 网络监听工具 嗅探器 SpyNet

    配置网卡 注册 监听配置 开始捕获

  9. Mac音频播放

    Mac音频播放 audioqueue播放pcm数据 http://msching.github.io/blog/2014/08/02/audio-in-ios-5/ audiounit播放pcm数据  ...

  10. Objective-C 知识点拾遗

    -- :: UIWebView的使用总结 UIWebView全解