[uva11916] Emoogle Grid (Discrete logarithm)

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud

Emoogle Grid

You have A to-color an MxN ( 1M, n108) of the dimensional grid. You'll be provided K ( 2k108) different colors. You'll also be provided a list of b ( 0b) List of the 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 cells from the left of the x-th row from the top.

While coloring the grid, you have to follow these rules-

    1. You have the to color of each cell which are 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. Vertically adjacent cells can have the same color, i.e. cell (x, y) and cell (x + 1, y) cannot contain the same color.

Now the great problem setter smiled with emotion and thought so he would ask the contestants to find how many ways the B Oard can be colored. Since the number can be very large and he doesn ' t want the contestants of being 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 GI Veaway (easiest) problem.

But unfortunately he got married and forgot the problem completely. After some days he rediscovered he problem and became very excited. But after a while, he saw that, in the judge data, and 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 had the input file and the correct answer file. So, he asks your help to regenerate the data. Yes, you is 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 has used for this problem.

Input

Input starts with an integer t ( t), denoting the number of test cases.

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 would contains the integers x and y ( 1x M, 1yN), denoting the row and column number of a blocked cell. All the cells would be distinct.

Output

For each case, print the case number and the minimum possible value of M. You can assume this 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

Test instructions: has m row n column of the grid, to its color K, which has a B known position of the lattice can not be painted color, the requirements of the upper and lower two adjacent lattice color can not be the same, ask in the case of the number of scenarios mod100,000,007=r, how much m? (Ensure that the lattice in the known position must be in M row n column) to ensure that M has a solution

Analysis: All in the first row or above the lattice is not coated when the number of the color scheme is K, the remaining points of the coloring scheme number is K-1.

The maximum value of the row for all known locations is X, and the number of x-1 in the front row of the non-colored lattice adjacent to the first row is a, and the number of squares in the non-painted lattice is B, then the number of squares that can be coated with K is a+n-b, The number of squares that can only be painted in K-1 is x*n-(a+n-b); the coloring scheme for x lines is temp=k^ (a+n-b) * (K-1) ^ (x*n-(A+n-b)) and if temp=r, temp is the answer

Again consider the case of line x+1, if the X Act is not a color of the lattice, then the next row of its adjacent lattice coloring scheme is K, otherwise K-1, the number of K-1 can be painted C, then temp=temp*k^c* (K-1) ^ (n-c), if temp=r, then temp is the answer

The next number of new coloring schemes for each line is cnt= (K-1) ^n, which is the next cnt^ans*temp=r (mod100,000,007)

Cntans=temp-1*r (mod100,000,007)

Then we can find out the discrete logarithm

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <cmath>6#include <Set>7 #defineX First8 #defineY Second9#include <map>Ten using namespacestd; One  Atypedef pair<int,int>PII; -typedefLong Longll; - ll N,k,b,r; the Set<PII>s; -PII p[1010]; -ll maxx=0; - Const intMod=100000007; + ll Mul_mod (ll X,ll y) - { +     return(LL) x*y%MoD; A } atll Fast_mod (intm,ll t) - { -ll temp= (Long Long) m; -ll ret=1LL; -      while(t) -     { in         if(t&1) ret=Mul_mod (ret,temp); -temp=Mul_mod (temp,temp); toT/=2; +     } -     returnret; the } *ll EXT_GCD (ll a,ll t,ll &d,ll &x,ll &y) $ {Panax Notoginseng     if(!t) {d=a;x=1; y=0;} -     Else { theEXT_GCD (t,a%t,d,y,x); y-=x* (A/t); +     } A } the LL INV (ll a) + { - ll D,x,y; $ EXT_GCD (a,mod,d,x,y); $     returnD = =1? (x%mod+mod)%mod:-1; - } - ll Log_mod (ll A,ll b) the { -ll m,v,e=1, I;Wuyim= (LL) sqrt (mod+0.5); thev=Inv (Fast_mod (a,m)); -Map<ll, LL >x; Wu x.clear (); -x[1]=0; About      for(i=1; i<m;i++) $     { -E=Mul_mod (e,a); -         if(!x.count (e)) x[e]=i; -     } A      for(i=0; i<m;i++) +     { the         if(X.count (b))returni*m+X[b]; -b=Mul_mod (b,v); $     } the     return-1; the } the ll solve () the { -     inttemp=0; in      for(intI=0; i<b;i++) the         if(P[i]. X!=maxx&&!s.count (Make_pair (p[i). x+1, P[i]. Y)) temp++; thetemp+=N; About      for(intI=0; i<b;i++) the         if(P[i]. x==1) temp--; thell Ret=mul_mod (Fast_mod (k,temp), Fast_mod (k1,(Long Long) maxx*n-b-temp)); the     if(RET==R)returnMaxx; +temp=0; -      for(intI=0; i<b;i++)if(P[i]. X==maxx) temp++; themaxx++;Bayiret=Mul_mod (Ret,fast_mod (k,temp)); theRet=mul_mod (Ret,fast_mod (K-1, N-temp)); the     if(RET==R)returnMaxx; -     //( ret* (k-1) ^n) ^x)%mod=r -     //namely ((k-1) ^n) ^x=r* (ret^ ( -1))%mod the     returnLog_mod (Fast_mod (K-1, n), Mul_mod (R,INV (ret))) +Maxx; the } the  the intMain () - { theIos::sync_with_stdio (false); the     intT; the     //freopen ("in.in", "R", stdin);94Cin>>T; the     intcas=1; the      while(t--) the     {98maxx=1; About s.clear (); -Cin>>n>>k>>b>>R;101          for(intI=0; i<b;i++)102         {103Cin>>p[i]. X>>P[i]. Y;104             if(P[i]. X>maxx) maxx=P[i]. X; the S.insert (P[i]);106         }107cout<<" Case"<<cas++<<": "<<solve () <<Endl;108     }109     return 0; the}
code June

[uva11916] Emoogle Grid (Discrete logarithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.