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的更多相关文章

  1. [uva11916] Emoogle Grid (离散对数)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Emoogle Grid  You have to color an MxN ( ...

  2. uva11916 Emoogle Grid (BSGS)

    https://uva.onlinejudge.org/external/119/p11916.pdf 令m表示不能染色的格子的最大行号 设>m行时可以染k种颜色的格子数有ck个,恰好有m行时可 ...

  3. UVA 11916 Emoogle Grid(同余模)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVa 11916 (离散对数) Emoogle Grid

    因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色. 如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择. 反过来,如果一个格子位于第一列,或者上面相邻的格子 ...

  5. uva 11916 Emoogle Grid

    题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足 ...

  6. UVA 11916 Emoogle Grid 离散对数 大步小步算法

    LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <mat ...

  7. Uva_11916 Emoogle Grid

    题目链接 题意: 有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案. 1)每个可涂色的位置必须涂上一种颜色 2)不可涂色位置不能涂色 3)每个位置必须从K种颜色中选出一种 ...

  8. UVA - 11916 Emoogle Grid (组合计数+离散对数)

    假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...

  9. uva 11916 Emoogle Grid (BSGS)

    UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...

随机推荐

  1. 16-1-es5

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Java之实现多线程

    保证同步的几种方法: (1) 同步方法,synchronized 关键字修饰方法.由于Java中的每个对象都有一个内置锁,当用该关键词修饰时,内置锁会保护整个方法.在调用该方法前,需要获得内置锁,否则 ...

  3. 2019-8-31-C#-如何给-ValueTuple-返回值添加注释

    title author date CreateTime categories C# 如何给 ValueTuple 返回值添加注释 lindexi 2019-08-31 16:55:58 +0800 ...

  4. [NOIP2019模拟赛]数数(gcd)

    题目大意: 求l~r中有多少数与x互质,带单点修改 分析: 两个30的部分分很好打: ·n<=1000暴力O(nq)就好了 ·$a_i<=100$用树状数组维护每个x的前缀和就好了 100 ...

  5. 跟我一起使用create-react-app脚手架搭建vw-layout解决方案

    之前也是看过大漠的vw适配Vue-cli,我自己写H5,还有使用vue做项目的时候,会搭建大漠博客中的那一套. 现在在github上面,看见了一位博主使用create-react-app也是用vw适配 ...

  6. jmeter参数化遇到的问题

    遇到的问题是点击运行后,察看结果树没有任何结果,且右上角的警告日志是: meter.threads.JMeterThread: Test failed! java.lang.IllegalArgume ...

  7. 【10.6NOIP普及模拟】MATH——枚举法

    [10.6NOIP普及模拟]MATH 题目简化 一个数列任意删k个数,是得数列中最大的差+最小的差最小 思路 程序1--时超40 暴搜+剪枝. 用类似排列组合的方式,暴搜删或不删 剪枝就是看看剩下的数 ...

  8. Ubuntu GitHub操作——分支、合并与标签

    分支 分支是用来将特性开发绝缘开来的.在你创建仓库的时候,master 是"默认的"分支.在其他分支上进行开发,完成后再将它们合并到主分支上. 创建一个叫做"featur ...

  9. ConcurrentHashMap线程安全的具体实现方式/底层具体实现

    1. jdk1.7以及之前 ConcurrentHashMap 是由 Segment 数组结构和 HashEntry 数组结构组成. 通俗的话讲:就是首先将数据分为一段一段的存储,然后给每一段数据配一 ...

  10. 为WCF增加UDP绑定(实践篇)

    这两天忙着系统其它功能的开发,没顾上写日志.本篇所述皆围绕为WCF增加UDP绑定(储备篇)中讲到的微软示例,该示例我已上传到网盘. 上篇说道,绑定是由若干绑定元素有序组成,为WCF增加UDP绑定其实就 ...