uva11916 - Emoogle Grid 网格涂色

来源:互联网 发布:绝地求生知乎 编辑:程序博客网 时间:2024/06/05 06:40

 Emoogle Grid 

You have to color an M x N (1$ \le$M,N$ \le$108) two dimensional grid. You will be providedK (2$ \le$K$ \le$108) different colors to do so. You will also be provided a list of B (0$ \le$B$ \le$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.
\epsfbox{p11916.eps}

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$150), denoting the number of test cases.

Each test case starts with a line containing four integers N, K, B andR (0$ \le$R < 100000007) which denotes the result for this case. Each of the next B lines will contains two integersx and y (1$ \le$x$ \le$M,1$ \le$y$ \le$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 

43 3 0 17284 4 2 1866243 13 32 5 2 201 22 22 3 0 989323

Sample Output 

Case 1: 3Case 2: 3Case 3: 2Case 4: 20

  给M行N列的网格涂上K种颜色,有B个格子不用涂色,其他每个格子涂一种颜色,同一列上下相邻两个各自不能涂相同颜色。给出涂色方案数取模后的结果R,求M。

  设最下一行有不用涂色各自的行为m,如果不存在不用涂色的格子,设m=1。先算完m以上部分cnt,m以下第一行以后每多一行,涂色方案乘以(K-1)^N,也就是cnt*P^M=R,移项得P^M=R*cnt^-1(cnt的逆元),用Shank大步小步算法算出M。

#include<iostream>#include<queue>#include<cstring>#include<cstdio>#include<cmath>#include<set>#include<map>#include<vector>#include<stack>#include<algorithm>#define eps 1e-9#define MAXN 510#define MAXM 110#define MOD 100000007typedef long long LL;using namespace std;LL T,N,K,B,R,m,x[MAXN],y[MAXN];set<pair<LL,LL> >s;void gcd(LL a,LL b,LL& d,LL& x,LL& y){    if(!b){        d=a;        x=1;        y=0;    }    else{        gcd(b,a%b,d,y,x);        y-=x*(a/b);    }}LL inv(LL a,LL n){    LL d,x,y;    gcd(a,n,d,x,y);    return d==1?(x+n)%n:-1;}LL bigpow(LL x,LL n,LL M){    LL ret=1,t=x%M;    while(n){        if(n&1) ret=ret*t%M;        t=t*t%M;        n>>=1;    }    return ret;}LL log_mod(LL a,LL b,LL n){    LL m,v,e=1,i;    m=(LL)sqrt(n+0.5);    v=inv(bigpow(a,m,n),n);    map<LL,LL> x;    x[1]=0;    for(LL i=1;i<m;i++){        e=e*a%n;        if(!x.count(e)) x[e]=i;    }    for(LL i=0;i<m;i++){        if(x.count(b)) return i*m+x[b];        b=b*v%n;    }    return -1;}LL count(){    LL c=0;    for(LL i=0;i<B;i++) if(x[i]!=m&&!s.count(make_pair(x[i]+1,y[i]))) c++;    c+=N;    for(LL i=0;i<B;i++) if(x[i]==1) c--;    return bigpow(K,c,MOD)*bigpow(K-1,(LL)m*N-B-c,MOD)%MOD;}LL solve(){    LL cnt=count();    if(cnt==R) return m;    LL c=0;    for(LL i=0;i<B;i++) if(x[i]==m) c++;    m++;    cnt=cnt*bigpow(K,c,MOD)%MOD*bigpow(K-1,N-c,MOD)%MOD;    if(cnt==R) return m;    return log_mod(bigpow(K-1,N,MOD),R*inv(cnt,MOD)%MOD,MOD)+m;}int main(){    freopen("in.txt","r",stdin);    LL cas=0;    scanf("%lld",&T);    while(T--){        scanf("%lld%lld%lld%lld",&N,&K,&B,&R);        s.clear();        m=1;        for(LL i=0;i<B;i++){            scanf("%lld%lld",&x[i],&y[i]);            m=max(m,x[i]);            s.insert(make_pair(x[i],y[i]));        }        printf("Case %lld: %lld\n",++cas,solve());    }    return 0;}



0 0