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 ...
随机推荐
- JS数组 团里添加新成员(向数组增加一个新元素)只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素。myarray[5]=88;
团里添加新成员(向数组增加一个新元素) 上一节中,我们使用myarray变量存储了5个人的成绩,现在多出一个人的成绩,如何存储呢? 只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素. my ...
- 使用github作为远程仓库的常见git操作
[git上传本地代码到github新建仓库]一.建立git本地仓库 1.在本地目标文件夹(Code)中执行命令: git init //初始化本地仓库二.将上传到github的项目文件添加到本地仓库中 ...
- 【学术篇】NOIP2016 D1T3 luogu1850换教室
题目链接:点击这里献出你宝贵的时间(是用来做题不是捐赠Emmmm).. Emmmm我太弱了= = 做完这题我觉得我应该去打星际..这题怎么就有重边了呢.. 这题就是一道期望= =当时考场上好像完全不会 ...
- 0903NOIP模拟测试赛后总结
分-rank33.这次考试心态挂了. 拿到题目通读三道题,发现都十分恶心. 然后把时间押到了T1上.将近两个小时,打了个dfs,一直调调调. 最后没调出来,手模了个数据就把自己两个小时的思路hack了 ...
- IDEA Error:java: Compilation failed: internal java compiler error 解决方案
这是由于版本不一致导致的 file => settings => 搜索找到Java Compiler 把相应jdk版本改成1.8 ctrl+alt+s
- iPhone开发关于UDID和UUID的一些理解
一.UDID(Unique Device Identifier) UDID是Unique Device Identifier的缩写,中文意思是设备唯一标识. 在很多需要限制一台设备一个账号的应用中 ...
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- 深入浅出 Java Concurrency (26): 并发容器 part 11 Exchanger[转]
可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视为 Synchro ...
- opencv-阈值分割
关于自适应阈值,可参考:Wellner 自适应阈值二值化算法 一.大津法OTSU(最大类间方差法) 参考:非黑即白——图像分割入门篇之Otsu阈值 自适应阈值分割—大津法(OTSU算法)C++实现 灰 ...
- hdu 1754 I Hate It (线段树)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树的模板题,详细的都写在代码里了 //不知道为什么定义单个字符,用%c输入会超时,换成字符数 ...