uva11916 - Emoogle Grid 网格涂色

 Emoogle Grid 

You have to color an M x N (1$ \le$M, N$ \le$108) two dimensional grid. You will be provided K (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 and R (0$ \le$R < 100000007) which denotes the result for this case. Each of the next B lines will contains two integers x 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 

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

  给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 100000007
typedef 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值