[uva 11916]Emoogle Grid 数学 BSGS

来源:互联网 发布:python将字典传入函数 编辑:程序博客网 时间:2024/06/01 09:59

11916 - Emoogle Grid

Time limit: 4.000 seconds

You have to color an M x N ( 1$ \le$MN$ \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 (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.

\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 NKB 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$M1$ \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个方格不能涂色,且垂直相连的两个方格不能涂同一种颜色。问有多少种涂色方案。
现在问题改为已知N,K,B以及原问题的结果求最小的可能的M

解题思路
首先考虑没有不能涂色的方格的情况,那么第一行的每一个方格都可以任意涂色,而剩余的方格除了对应的上一行的颜色都可以随意填,ans=k^n*(k-1)^(n*m-n)
那么如果出现了不能填颜色的方格,如果它的下面有方格,这个方格就可以任意涂色了。这个时候判断一下多少个方格可以涂k种颜色,多少个方格可以涂k-1种颜色。
这个时候问题就可以转化为求x*(t)^L=k 求L的最小值。BSGS即可~
特判一下不需要多放一行和多放一行的情况即可
map比较慢,还是要掌握下hash的写法了

code:
#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 100000007using 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;}




1 0
原创粉丝点击