Emoogle Grid 

You have to color an M x N ( 1MN108) two dimensional grid. You will be provided K ( 2K108) different colors to do so. You will also be provided a list of B( 0B500) list of blocked cells of this grid. You cannot color those blocked cells. A cell can be described as (xy), 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 (xy) 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 (T150), denoting the number of test cases.

Each test case starts with a line containing four integers NKB and R ( 0R < 100000007) which denotes the result for this case. Each of the next B lines will contains two integers x and y ( 1xM, 1yN), 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

题目大意:已知N,K,R和B个格子的位置求最小可能的M。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
using namespace std; typedef long long LL;
const int MOD=;
const int Max=;
int N,M,B,K,R,x[Max],y[Max];
set<pair<int,int> > bset; LL mult_mod(LL a,LL b)
{
LL t=;
a%=MOD;
while(b)
{
if(b&) t=(t+a)%MOD;
b>>=;
a=(a<<)%MOD;
}
return t;
} LL pow_mod(LL a,LL b)
{
LL t=;
a%=MOD;
while(b)
{
if(b&) t=mult_mod(t,a);
b>>=;
a=mult_mod(a,a);
}
return t;
} int Extended_Euclid(int a,int b,int &x,int &y)
{
int d,t;
if(b==)
{
x=;y=;return a;
}
d=Extended_Euclid(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return d;
} int inv(int a)
{
int x,y,d;
d=Extended_Euclid(a,MOD,x,y);
x=(x%MOD+MOD)%MOD;
return x;
} int log_mod(int a,int b)
{
int c,v,e=,i;
c=(int)sqrt(MOD+0.5);
v=inv(pow_mod(a,c));
map<int,int> xx;
xx[]=;
for(i=;i<c;i++)//计算e[i]
{
e=mult_mod(e,a);
if(!xx.count(e)) xx[e]=i;
}
for(i=;i<c;i++)
{
if(xx.count(b)) return (i*c+xx[b]);
b=mult_mod(b,v);
}
return -;
} int Count()
{
int cnt;//涂色种数
int c=;//能涂k种的个数
int i;
for(i=;i<B;i++)
//上面是不能涂色的下面是能涂色的情况,排除不能涂色的相邻的情况
if(x[i]!=M && !bset.count(make_pair(x[i]+,y[i])) ) c++;
c+=N;//第一行的都能涂k种色
for(i=;i<B;i++)//减去第一行不能涂色的
if(x[i]==) c--;
//ans=k^c * (k-1)^(m*n-c-b) mod MOD
cnt=mult_mod(pow_mod(K,c),pow_mod(K-,(LL)M*N-B-c));
return cnt;
} int Deal()
{
int i,cnt=Count();
if(cnt==R) return M;
int c=,m;
for(i=;i<B;i++)//不变部分最后一行的下一行能涂k种的个数
if(x[i]==M) c++;
M++;
cnt=mult_mod(cnt,pow_mod(K,c));
cnt=mult_mod(cnt,pow_mod(K-,N-c));
if(cnt==R) return M;
//模方程求解 a^x=b (mod n),用log_mod(a,b,n)函数求解
m=log_mod(pow_mod(K-,N),mult_mod(R,inv(cnt)))+M;
return m;
} int main()
{
int t,i,Case=;
scanf("%d",&t);
while(t--)
{
bset.clear();
Case++;
scanf("%d %d %d %d",&N,&K,&B,&R);
M=;
for(i=;i<B;i++)
{
scanf("%d %d",x+i,y+i);
bset.insert(make_pair(x[i],y[i]));//插入一个座标
if(M<x[i]) M=x[i];
}
printf("Case %d: %d\n",Case,Deal());
}
return ;
}

uva 11916 解模方程a^x=b (mod n)的更多相关文章

  1. uva 11916 Emoogle Grid (BSGS)

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

  2. UVA 11916 Emoogle Grid(同余模)

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

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

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

  4. uva 11916 Emoogle Grid

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

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

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

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

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

  7. 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法

    先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝  扩展Baby Step Gian ...

  8. UVA 1426 - Discrete Square Roots(数论)

    UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...

  9. UVA 11754 (暴力+中国剩余定理)

    题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...

随机推荐

  1. HDinsight 系列-使用证书登陆中国区Azure

    使用azure explorer 插件的时候,登陆默认是globle的azure网站,中国区的azure不能直接使用 可以使用auth文件认证 auth 文件生成 az cloud show -o j ...

  2. 4个Linux服务器监控工具

    下面是我想呈现给你的4个强大的监控工具. htop – 交互式进程查看器 你可能知道在机器上查看实时进程的标准工具top.如果不知道,请运行$ top看看,运行$ man top阅读帮助手册. hto ...

  3. 访问修饰符(C# 参考)

    第一篇 就抄写了一下下MSDN上面的东西练练手吧!!! 访问修饰符是一些关键字,用于指定声明的成员或类型的可访问性.             本节介绍四个访问修饰符: public protected ...

  4. Spring中的事务传播行为与隔离级别

    事务传播行为 事务传播行为(为了解决业务层方法之间互相调用的事务问题): 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己 ...

  5. 在 webpack 中使用 ECharts

    http://echarts.baidu.com/tutorial.html#%E5%9C%A8%20webpack%20%E4%B8%AD%E4%BD%BF%E7%94%A8%20ECharts W ...

  6. RLock(递归锁)

    import threading, time def run1(): print("grab the first part data") lock.acquire()#进入大门后的 ...

  7. Bootstrap 表格2

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. java第十次作业:oop的第6张图片到第11张图片

  9. 有趣的this以及apply,call,bind方法

    看this指向谁,要看执行时而非定义时(箭头函数除外).函数没有绑定在对象上调用,非'strict'模式下,this指向window,否则为undefined 改变this指向的方法 1. apply ...

  10. MySql中引擎

    1. InnoDB 引擎 MySQL 5.5 及以后版本中的默认存储引擎,它的优点如下:灾难恢复性好,支持事务,使用行级锁,支持外键关联,支持热备份. InnoDB引擎中的表,其数据的物理组织形式是簇 ...