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/ ...
随机推荐
- 【C语言】学不会的指针
指针 前言: 指针是C语言程序的核心,刚开始学指针,嗯....这样呀,貌似不难呀:之后开始用指针,&p,p,*p,**p,这些指针在用的时候,额.....什么东东?每次都要想半天,特别是遇到双 ...
- Linux下C++酒店管理系统
功能要求: 相关源码:码云:传送门,GitHub:传送门 相关图片: 拆分版 make编译 ./hotel运行 输入2,进入开房模块 相关源码: class.cpp #include ...
- 复杂sql语句之单字段分类count计数和多字段count计数
SELECT AF_DEPARTMENT, dept.FULLNAME, SUM(CASE AF_CLASSIFY WHEN THEN ELSE END) AS 'o_standard', (COUN ...
- MVC学习笔记(一)—用EF创建数据库
1.新建MVC项目 2.在Models文件夹下添加名为User的新类 2.1 Users类中的代码为: public class Users { /// <summary> / ...
- 【转载】C#中float.TryParse方法和float.Parse方法的异同之处
在C#编程过程中,float.TryParse方法和float.Parse方法都可以将字符串string转换为单精度浮点类型float,但两者还是有区别,最重要的区别在于float.TryParse方 ...
- springCloud学习5(Spring-Cloud-Stream事件驱动)
springcloud 总集:https://www.tapme.top/blog/detail/2019-02-28-11-33 代码见文章结尾 想想平常生活中做饭的场景,在用电饭锅做饭的同时, ...
- FFMPEG_avi转码到mp4(aac+h264)源码
#include <ffmpeg/avcodec.h>#include <ffmpeg/avformat.h>#include <stdio.h>#include ...
- mysql模糊查询1,11,111用逗号(其他符号)拼接的相似字符串
mysql进行模糊查询时,基本都是LIKE "%sss%",有时候这种查询时准确的,但是有种情况这种查询会出现很大问题. 看一下下面这张表 如果想查询字段test包含1的数据,一般 ...
- Alpha_4
一. 站立式会议照片 二. 工作进展 (1) 昨天已完成的工作 a. 我的·主界面设计 b. 番茄钟的页面及音乐选择弹窗页面设计 c. 实现自定义习惯和设置新习惯的功能页面,并可预览 d.已实现番茄钟 ...
- 《Spring in Action 4》阅读札记
重要思路 Spring通过面向POJO编程.依赖注入.AOP和模板技术来降低Java开发的复杂性. 依赖注入能够让互相协作的软件组件保持松耦合,模块直接的耦合性是必要的,否则没法完成工作,但是耦合性需 ...