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

最新文章

  1. IIS最大连接数优化
  2. [Linux] - CentOS中文乱码解决办法
  3. CmRegisterCallback使用方法
  4. Android的init过程(二):初始化语言(init.rc)解析【转】
  5. oracle PL/SQL基础编程
  6. SPOJ 78 Marbles 组合数学
  7. RequireJS 2.0 正式发布(转)
  8. 设置符合条件的DataGridView的行的颜色的两种方法
  9. Mego(04) - NET简单实现EXCEL导入导出
  10. Gronwall型不等式
  11. python基础篇_001_初识Python
  12. Ionic项目的建立
  13. 在IIS上启用Gzip压缩(HTTP压缩)
  14. Python之字典的应用
  15. 01-go语言开始-HelloWorld
  16. cocos2d-js 3.0 rc2 自定义UI控件组件 例子:能播放动画的MenuItem。MenuItemSprite的bug
  17. Saltstack实战之无master和多master
  18. oracle 临时表的使用
  19. windows下运用批处理实现一键自动开启多个应用
  20. 【leetcode 简单】 第六十五题 2的幂

热门文章

  1. pip的使用方法简介
  2. Python3中面向对象 OOP
  3. 面试系列18 rediscluster 原理
  4. Java Annotation试用
  5. web.xml中多个Servlet执行顺序的问题!
  6. 使用subprocessm模块管理进程
  7. resful风格
  8. 基于知识图谱的APT组织追踪治理
  9. opencv-阈值分割
  10. 2019 西电ACM校赛网络赛 题解