链接:

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. Linux crontab 查看所有用户的crontab任务

    查看所有用户的crontab任务 - mlzhu007的专栏 - CSDN博客 https://blog.csdn.net/mlzhu007/article/details/81662091 以roo ...

  2. Kubernetes集群中Jmeter对公司演示的压力测试

    6分钟阅读 背景 压力测试是评估Web应用程序性能的有效方法.此外,越来越多的Web应用程序被分解为几个微服务,每个微服务的性能可能会有所不同,因为有些是计算密集型的,而有些是IO密集型的. 基于微服 ...

  3. Mybatis自动生成代码工具

    项目结构如下 一:在POM中添加mybatis-generator-maven-plugin 插件 <plugins> <plugin> <groupId>org. ...

  4. Outlook 邮件助手

    Outlook 邮件助手 1 Overview 2 C# 编程 3 Outlook 设置 3.1 Outlook 2013 3.2 Outlook 2010 1 Overview 本章将示例如何开发一 ...

  5. MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  6. centos8安装chromium浏览器

    1/yum install epel* [root@localhost framework]# yum list epl* Last metadata expiration check: 0:57:4 ...

  7. Bean named 'XXX' is expected to be of type [XXX] but was actually of type [com.sun.proxy.$Proxy7

    AOP原理 <aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面. <aop:aspectj-aut ...

  8. 2019-07-23 php魔术方法

    本文对一些php中的魔术方法进行总结,魔术方法顾名思义就是具备神奇功能的方法(function).魔术方法通常在某些特定情况下自动触发,不能用实例化变量名->方法名()来主动触发.不同的魔术方法 ...

  9. Linux负载模拟

    转载:https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79063392 CPU 下面命令会创建 CPU 负荷,方法是通过压缩随机数据并将结果发送 ...

  10. Maven的SNAPSHOT版本找不到

    有时一个SNAPSHOT版本的包,明明打包部署到私服了,却还是报错找不到,比如: [WARNING] The POM for com.foo:bar:jar:0.4.0-20130404.093655 ...