UVA11916 Emoogle Grid
Emoogle Grid
You have to color an M × N (1 ≤ M, N ≤ 108 ) two dimensional grid. You will be provided K (2 ≤ K ≤ 108 ) different colors to do so. You will also be provided a list of B (0 ≤ B ≤ 500) list of blocked cells of this grid. You cannot color those blocked cells. A cell can be described as (x, y), which points to the y-th cell from the left of the x-th 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) cannot contain the same color. Now the great problem setter smiled with emotion and thought that he would ask the contestants to find how many ways the board can be colored. Since the number can be very large and he doesn’t want the contestants to be in trouble dealing with big integers; he decided to ask them to find the result modulo 100,000,007. So he prepared the judge data for the problem using a random generator and saved this problem for a future contest as a giveaway (easiest) problem. But unfortunately he got married and forgot the problem completely. After some days he rediscovered his problem and became very excited. But after a while, he saw that, in the judge data, he forgot to add the integer which supposed to be the ‘number of rows’. He didn’t find the input generator and his codes, but luckily he has the input file and the correct answer file. So, he asks your help to regenerate the data. Yes, you are given the input file which contains all the information except the ‘number of rows’ and the answer file; you have to find the number of rows he might have used for this problem. Input Input starts with an integer T (T ≤ 150), denoting the number of test cases. Each test case starts with a line containing four integers N, K, B and R (0 ≤ R < 100000007) which denotes the result for this case. Each of the next B lines will contains two integers x and y (1 ≤ x ≤ M, 1 ≤ y ≤ N), denoting the row and column number of a blocked cell. All the cells will be distinct. Output For each case, print the case number and the minimum possible value of M. You can assume that solution exists for each case. Sample Input 4 3 3 0 1728 4 4 2 186624 3 1 3 3 2 5 2 20 1 2 2 2 2 3 0 989323 Sample Output Case 1: 3 Case 2: 3 Case 3: 2 Case 4: 20
这题先看已知部分和已知部分的下一行,不难统计出方案数cmt
每一加一行未知部分,会增加(k - 1)^m
解一个cnt * ((k - 1)^m)^p = r mod MOD
BSGS即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#include <utility>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
inline void swap(long long &a, long long &b)
{
long long tmp = a;a = b;b = tmp;
}
inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f;
const long long MAXB = + ;
const long long MOD = ;
long long t, n, m, k, b, r, x[MAXB], y[MAXB], ma, cnt;
long long pow(long long a, long long b, long long mod)
{
long long r = , base = a;
for(;b;b >>= )
{
if(b & ) r *= base, r %= mod;
base *= base, base %= mod;
}
return r;
}
long long ni(long long x, long long mod)
{
return pow(x, mod - , MOD);
}
std::map<std::pair<int, int>, int> mp;
std::map<int, int> mmp;
//求a^m = b % mod
long long BSGS(long long a, long long b, long long mod)
{
long long m = sqrt(mod), tmp = , ins = ni(pow(a, m,mod), mod);
mmp.clear();
for(register long long i = ;i < m;++ i)
{
if(!mmp.count(tmp)) mmp[tmp] = i;
tmp = tmp * a % MOD;
}
for(register long long i = ;i < m;++ i)
{
if(mmp.count(b)) return i * m + mmp[b];
b = (b * ins) % MOD;
}
return -;
} //计算可变部分方案数
long long count()
{
long long tmp = m;//有k种涂法的方案数
for(register long long i = ;i <= b;++ i)
{
if(x[i] != n && !mp.count(std::make_pair(x[i] + , y[i]))) ++ tmp;
if(x[i] == ) -- tmp;
}
return pow(k, tmp, MOD) * pow(k - , n * m - tmp - b, MOD) % MOD;
} long long solve()
{
long long cnt = count();
if(cnt == r) return n;
long long tmp = ;
for(register long long i = ;i <= b;++ i)
if(x[i] == n) ++ tmp;
cnt = cnt * pow(k, tmp, MOD) % MOD * pow(k - , m - tmp, MOD) % MOD;
++ n;
if(cnt == r) return n;
return (BSGS(pow(k - , m, MOD), r * ni(cnt, MOD) % MOD, MOD) + n)%MOD;
} int main()
{
read(t);
for(register long long v = ;v <= t;++ v)
{
read(m), read(k), read(b), read(r);
n = ;
mp.clear();
for(register long long i = ;i <= b;++ i)
{
read(x[i]), read(y[i]);
mp[std::make_pair(x[i], y[i])] = ;
n = max(n, x[i]);
}
printf("Case %lld: %lld\n", v, solve());
}
return ;
}
UVA11916
UVA11916 Emoogle Grid的更多相关文章
- [uva11916] Emoogle Grid (离散对数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Emoogle Grid You have to color an MxN ( ...
- uva11916 Emoogle Grid (BSGS)
https://uva.onlinejudge.org/external/119/p11916.pdf 令m表示不能染色的格子的最大行号 设>m行时可以染k种颜色的格子数有ck个,恰好有m行时可 ...
- UVA 11916 Emoogle Grid(同余模)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 11916 (离散对数) Emoogle Grid
因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色. 如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择. 反过来,如果一个格子位于第一列,或者上面相邻的格子 ...
- uva 11916 Emoogle Grid
题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足 ...
- UVA 11916 Emoogle Grid 离散对数 大步小步算法
LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <mat ...
- Uva_11916 Emoogle Grid
题目链接 题意: 有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案. 1)每个可涂色的位置必须涂上一种颜色 2)不可涂色位置不能涂色 3)每个位置必须从K种颜色中选出一种 ...
- UVA - 11916 Emoogle Grid (组合计数+离散对数)
假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...
- uva 11916 Emoogle Grid (BSGS)
UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...
随机推荐
- SpringBoot集成JPA根据实体类自动生成表
数据库是mysql,在application.properties中的写法如下: 原来配置这样的时候确实可以生产表的 #spring.jpa.hibernate.ddl-auto=update 多方查 ...
- nginx i.com.conf
server { listen 9090; server_name i.com; root /Users/chong/Documents/www; # Load configuration files ...
- 算法系列:Shell 排序
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- ie中报错---不能执行已释放 Script 的代码
我的原因:使用jquery.colorbox.js.在页面中使用弹框,对于父页面中的变量进行修改(弹框页面的js--window.parent.obj.arr= 数组;), 当弹框关闭之后,在父页面中 ...
- DRF 请求生命周期以及各模块解析
目录 rest_framework框架的封装特点 原生Django与DRF比较 APIView 的请求生命周期 请求模块(request) 解析模块(parser_classes) 异常模块(exce ...
- idea加载完文件报错:java:-source 1.7中不支持lambda表达式 解决方案
1.file - Project Structure ctrl+alt+shift+s 2.modules 中把7换成8
- PAT甲级——A1083 List Grades
Given a list of N student records with name, ID and grade. You are supposed to sort the records with ...
- 02_Hibernate持久化配置
一.hibernate对象持久化 Web开发的分层: 为了把数据访问细节和业务逻辑分开, 一般把数据访问作为单独的持久化层.DAO是数据访问对象,使用hibernate后,数据访问对象中操作的API将 ...
- 图像的K-L变换
1 问题的提出 2 K-L变换的原理 3 K-L变换的计算过程 4 K-L变换的性质 5 K-L变换的深入讨论 6 K-L变换的应用
- springboot整合rabbitMQ时遇到的消息无法入列问题
问题描述: 对列和交换器配置如下(绑定的正常交换器的key是“convert”): 管理平台上手动发是可以的: 而通过程序发消息不行,根本没有进入队列: 解决:显式指定交换器(备选交换器和死信交换器都 ...