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. codevs 3026 恶心的扑克

    时间限制: 1 s  空间限制: 64000 KB  题目等级 : 白银 Silver 题目描述 Description 有一副恶心的扑克,从小到大依次是3 , 4 , 5 , 6 , 7 , 8 , ...

  2. shell批量转换iOS和Android图标

    icon_ios.sh #!/bin/sh convert icon-1024.png -resize 180x180 icon-180@3x.png convert icon-1024.png -r ...

  3. UVA - 12264 Risk (二分,网络流)

    题意比较坑,移动完以后的士兵不能再次移动,不然样例都过不了... 最小值最大满足决策单调性所以二分答案,跑网络流验证是否可行. 这种题重点在建图,为了保证只移动一次,拆点,一个入点一个出点,到了出点的 ...

  4. C-基础:atoi

    C语言库函数名: atoi 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); 函数说明: 参数np ...

  5. Java产生GUID

    /** * 产生GUID */public static final String generateGUID(){ UUID uuid = UUID.randomUUID(); return uuid ...

  6. $Codeforces\; Round\; 504\; (Div.2)$

    宾馆的\(\rm{wifi}\)也太不好了,蹭的\(ZZC\)的热点才打的比赛(感谢\(ZZC\)) 日常掉rating-- 我现在是个\(\color{green}{pupil}\)-- 因为我菜, ...

  7. 获得Java中System对应一些属性值

    public static void main(String[] args){ System.out.println("Java运行时环境版本:\n"+System.getProp ...

  8. ios之UIActivityIndicatorView

    UIActivityIndicatorView和UIProgressView都继承自UIView,所以他们可以附属在其他视图上.UIActivityIndicatorView是一个进度提示器,显示一个 ...

  9. CSS3中制作倒影box-reflect

    目前仅在Chrome.Safari和Opera浏览器下支持 box-reflect:none | <direction> <offset>? <mask-box-imag ...

  10. 【php】 布尔值判断

    当转换为 boolean 时,以下值被认为是 FALSE: 布尔值 FALSE 本身 整型值 0(零) 浮点型值 0.0(零) 空字符串,以及字符串 "0" 不包括任何元素的数组 ...