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. Redis Cluste部署

    一.原生搭建篇Cluster了解cluster的架构 Redis-cluster是使用的是一致性哈希算法来切分数据存储,总计16383个槽,分成16383/N(redis节点)个分区,存取时将key转 ...

  2. 开发环境、测试环境、生产环境、UAT环境、仿真环境详解

    版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/WYX15011474269/article ...

  3. MapReduce应用程序执行过程

  4. 阿里云香港ECS搭建Shadowscoks

    注(转https://yijingping.github.io/2016/11/29/fanqiang.html) 1 为什么FQ 作为一个技术人员, 最常用的就是Google.StackOverfl ...

  5. uoj33 树上GCD

    题意:给你一棵树,根节点为1,每条边长度为1.定义f(u,v)=gcd(u-lca(u,v),lca(u,v)-v),求有多少个无序点对f(u,v)=i.对每个i输出答案. n<=20W. 标程 ...

  6. Python3基础笔记_字符串类型

    # 1.Python转义字符 a = "sqwerdf" # 2.Python字符串运算符 ''' + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符 ...

  7. Spring Cloud Alibaba发布第二个版本,Spring 发来贺电

    还是熟悉的面孔,还是熟悉的味道,不同的是,这次的配方升级了. 今年10月底,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cl ...

  8. 0810NOIP模拟测试赛后总结

    明日之后将是什么. 悲哀, 还是希望? 60分我没脸了…… 所以T1好不容易想到了正解结果实现打挂w0了…… 贪心想的还是相当完美的. 不知道我咋想的开了1e6个栈然后dfs模拟结果MLE原地自爆…… ...

  9. 基于MFC的实时可视化项目中视图刷新消息的严谨使用

    在实时可视项目中,视图的实时刷新显示对软件的体验感来说非常重要,当算法的效率达到实时,比如一秒40帧,如果实时显示帧率更不上,则体验感将大大折扣,让用户感觉你的算法并没有40帧,当然最关键的是解决显示 ...

  10. light oj 1095 组合数学

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...