转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

 Emoogle Grid 

You have to color an MxN ( 1M, N108) two dimensional grid. You will be provided K ( 2K108) different colors to do so. You will also be provided a list of B ( 0B500) 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.

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 ( T150), 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 will contains two integers x and y ( 1xM, 1yN), 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个已知位置的格子不能涂颜色,要求上下两个相邻的格子的颜色不能相同,问在方案数mod100,000,007=R的情况下,M为多少?(保证已知位置的格子一定在M行N列内)保证M有解

分析:所有在第一行或者其上方的格子为不可涂时的格子的涂色方案数为K,其余点的涂色方案数为K-1.

设所有已知位置的格子的行的最大值为x,记在前x-1行内不可涂色的格子相邻的下方的可涂色格子的数目为a,不可涂色的格子中位于第一行的格子的数目为b,则最终可涂K种颜色的格子的数目为a+N-b,只能涂K-1中颜色的格子的数目为x*N-(a+N-b);则求x行的涂色方案为temp=K^(a+N-b)*(K-1)^(x*N-(a+N-b)),若temp=R,则temp即为答案

再考虑第x+1行的情况,若第x行为不可涂色的格子,则这一行的其下方相邻的格子的涂色方案为K,否则为K-1,记可涂K-1种的数目为c,则temp=temp*K^c*(K-1)^(N-c),若temp=R,则temp即为答案

则接下来的每行的新的涂色方案数都为cnt=(K-1)^N,即接下来求cnt^ans*temp=R(mod100,000,007)

cntans=temp-1*R(mod100,000,007)

然后求一下离散对数即可

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#define X first
#define Y second
#include <map>
using namespace std; typedef pair<int,int> PII;
typedef long long ll;
ll n,k,b,r;
set<PII> s;
PII p[];
ll maxx=;
const int mod=;
ll mul_mod(ll x,ll y)
{
return (ll)x*y%mod;
}
ll fast_mod(int m,ll t)
{
ll temp=(long long)m;
ll ret=1LL;
while(t)
{
if(t&)ret=mul_mod(ret,temp);
temp=mul_mod(temp,temp);
t/=;
}
return ret;
}
ll ext_gcd(ll a,ll t,ll &d,ll &x,ll &y)
{
if(!t){d=a;x=;y=;}
else {
ext_gcd(t,a%t,d,y,x);y-=x*(a/t);
}
}
ll inv(ll a)
{
ll d,x,y;
ext_gcd(a,mod,d,x,y);
return d == ? (x%mod+mod)%mod : -;
}
ll log_mod(ll a,ll b)
{
ll m,v,e=,i;
m=(ll)sqrt(mod+0.5);
v=inv(fast_mod(a,m));
map<ll ,ll >x;
x.clear();
x[]=;
for(i=;i<m;i++)
{
e=mul_mod(e,a);
if(!x.count(e))x[e]=i;
}
for(i=;i<m;i++)
{
if(x.count(b))return i*m+x[b];
b=mul_mod(b,v);
}
return -;
}
ll solve()
{
int temp=;
for(int i=;i<b;i++)
if(p[i].X!=maxx&&!s.count(make_pair(p[i].X+,p[i].Y)))temp++;
temp+=n;
for(int i=;i<b;i++)
if(p[i].X==)temp--;
ll ret=mul_mod(fast_mod(k,temp),fast_mod(k-,(long long)maxx*n-b-temp));
if(ret==r)return maxx;
temp=;
for(int i=;i<b;i++)if(p[i].X==maxx)temp++;
maxx++;
ret=mul_mod(ret,fast_mod(k,temp));
ret=mul_mod(ret,fast_mod(k-,n-temp));
if(ret==r)return maxx;
//求(ret*((k-1)^n)^x)%mod=r
//即((k-1)^n)^x=r*(ret^(-1))%mod
return log_mod(fast_mod(k-,n),mul_mod(r,inv(ret)))+maxx;
} int main()
{
ios::sync_with_stdio(false);
int t;
//freopen("in.in","r",stdin);
cin>>t;
int cas=;
while(t--)
{
maxx=;
s.clear();
cin>>n>>k>>b>>r;
for(int i=;i<b;i++)
{
cin>>p[i].X>>p[i].Y;
if(p[i].X>maxx)maxx=p[i].X;
s.insert(p[i]);
}
cout<<"Case "<<cas++<<": "<<solve()<<endl;
}
return ;
}

代码君

[uva11916] Emoogle Grid (离散对数)的更多相关文章

  1. UVA11916 Emoogle Grid

    Emoogle Grid You have to color an M × N (1 ≤ M, N ≤ 108 ) two dimensional grid. You will be provided ...

  2. UVA 11916 Emoogle Grid 离散对数 大步小步算法

    LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <mat ...

  3. uva11916 Emoogle Grid (BSGS)

    https://uva.onlinejudge.org/external/119/p11916.pdf 令m表示不能染色的格子的最大行号 设>m行时可以染k种颜色的格子数有ck个,恰好有m行时可 ...

  4. UVa 11916 (离散对数) Emoogle Grid

    因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色. 如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择. 反过来,如果一个格子位于第一列,或者上面相邻的格子 ...

  5. UVA - 11916 Emoogle Grid (组合计数+离散对数)

    假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...

  6. UVA 11916 Emoogle Grid(同余模)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. uva 11916 Emoogle Grid

    题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足 ...

  8. Uva_11916 Emoogle Grid

    题目链接 题意: 有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案. 1)每个可涂色的位置必须涂上一种颜色 2)不可涂色位置不能涂色 3)每个位置必须从K种颜色中选出一种 ...

  9. uva 11916 Emoogle Grid (BSGS)

    UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...

随机推荐

  1. hdu5672 尺取法

    StringTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  2. C/C++中define的使用

    代码: #include <iostream> using namespace std; #define a 10 void foo(); void bar(); void foo(){ ...

  3. C++中引用和指针详解

    先来分析指针这个东东: 从概念上讲,指针本质上就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变. 上面的图表示了程序运行时变量的值 ...

  4. tttt

    while(scanf("%d",&n)!=EOF) { res=-1; level(tmp,n,res,1); printf("%d/n",res); ...

  5. 16-js-缓冲运动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. scons构建自己的一个简单的程序

    我在我的D盘下,新建一个文件夹,命名为try.在这个文件夹下新建两个文件,一个文件是test.c .里面的程序很简单: #include<stdio.h>#include<stdli ...

  7. XJOI网上同步训练DAY1 T3

    思路:一开始看到这题的时候想DP,可是发现貌似不行..因为有前缀也有后缀,而且有的后缀会覆盖到现在的前缀,这就不满足无后效性了啊! 但是有个很巧妙的思路:如果我们知道a[i]的最大值,那么p的数量和q ...

  8. 使用ownCloud搭建你的个人云服务(ubuntu 14.04 server)(ownCloud对文件不切片,Seafile对文件切片),owncloud没有存储的功能 只能同步 本地删除了服务器也会删除

    ownCloud是什么 ownCloud是一个自由且开源的个人云存储解决方案(类似百度网盘或者Dropbox),包括两个部分:服务器和客户端. ownCloud在客户端可通过网页界面,或者安装专用的客 ...

  9. Qt工具知多少

    一级题目: Qt Designer — 所见即所得的界面设计工具, 可以用拖拽的方式将控件排布在界面上,支持layout, 支持signal/slot编辑. 生成的文件保存为ui格式, ui是xml格 ...

  10. 自定义JsonResult处理JSON序列化DateTime类型数据(Ext4.2+ASP.NET MVC 4)

    最近项目中前台页面使用Extjs4.2 ,在后台ASP.NET MVC4返回的DateTime类型的数据错返回的DateTime类型的JsonResult的结果中的值是“\/Date(13784461 ...