11916 - Emoogle Grid
You have to color an M x N ( 1M, 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 -
- You have to color each cell which is not blocked.
- You cannot color a blocked cell.
- You can choose exactly one color from K given colors to color a cell.
- 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.
![\epsfbox{p11916.eps}](http://uva.onlinejudge.org/external/119/p11916.png)
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![$ \le$](http://uva.onlinejudge.org/external/119/11916img1.png)
Each test case starts with a line containing four integers N, K, B and R ( 0R < 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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-10
#define mod 100000007
using namespace std;
void egcd(LL a,LL b,LL &x,LL &y)
{
if (b==0) {
x=1;
y=0;
return ;
}
egcd(b,a%b,x,y);
LL t=x;
x=y;y=t-a/b*y;
}
LL qmod(LL x,LL y)
{
LL ans=1;
LL poo=x;
while (y)
{
if (y&1){
ans=(ans*poo)%mod;
}
poo=(poo*poo)%mod;
y/=2;
}
return ans;
}
set <LL> sett;
set <LL> ::iterator it;
map <LL , int> times;
int main()
{
LL n,k,r,cntm,fbl,lbl;//fbl代表需要用多少个k lbl代表需要用多上个k-1
int T,b,x,y,ca=0;
scanf("%d",&T);
while (T--)
{
scanf("%lld%lld%d%lld",&n,&k,&b,&r);
sett.clear();
times.clear();
cntm=1;
for (int i=1;i<=b;i++)
{
scanf("%d%d",&x,&y);
cntm=max(cntm,(LL)x);
sett.insert((LL)x*(n+1)+y);
}
printf("Case %d: ",++ca );
fbl=n;
lbl=n*(cntm-1);
for (it=sett.begin(); it!=sett.end();it++)
{
x=(*it)/(n+1);
y=(*it)%(n+1);
if (x==1) fbl--;
else lbl--;
if ((x!=cntm)&&(sett.find((x+1)*(n+1)+y)==sett.end())) {lbl--;fbl++;}
}
LL tmp=1;
tmp=(tmp*qmod((LL)k,fbl))%mod;
tmp=(tmp*qmod((LL)(k-1),lbl))%mod;
if (tmp==r) {
printf("%lld\n",cntm );
continue;
}
cntm++;
fbl=n;
lbl=n*(cntm-1);
for (it=sett.begin(); it!=sett.end();it++)
{
x=(*it)/(n+1);
y=(*it)%(n+1);
if (x==1) fbl--;
else lbl--;
if ((x!=cntm)&&(sett.find((x+1)*(n+1)+y)==sett.end())) {lbl--;fbl++;}
}
tmp=1;
tmp=(tmp*qmod((LL)k,fbl))%mod;
tmp=(tmp*qmod((LL)(k-1),lbl))%mod;
if (tmp==r) {
printf("%lld\n",cntm );
continue;
}
LL t=qmod((LL)k-1,n)%mod;
LL cur=1;
if (tmp==r) {
printf("%lld\n",cntm );
continue;
}
for (int i=1;i<10002;i++)
{
times[(cur*tmp)%mod]=i;
cur=(cur*t)%mod;
}
cur=(cur*t)%mod;
t=cur;
cur=1;
LL ans=mod+1;
for (int i=1;i<10002;i++)
{
LL xx,yy;
egcd(cur,mod,xx,yy);
xx=(xx+mod)%mod;
LL inve=(xx*(r))%mod;
if (times[inve]) {ans=min(ans,(LL)(times[inve])-1+(i-1)*10002);}
cur=(cur*t)%mod;
}
printf("%lld\n",ans+cntm);
}
return 0;
}