uva11916 - Emoogle Grid 网格涂色
2014-06-29 21:58
405 查看
Emoogle Grid |
M,
N
108) two dimensional grid. You will be provided
K (2
K
108)
different colors to do so. You will also be provided a list of
B (0
B
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 -
You have to color each cell which is not blocked.
You cannot color a blocked cell.
You can choose exactly one color from K given colors to color a cell.
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 (0
R < 100000007) which denotes the result for this case. Each
of the next B lines will contains two integers
x and y (1
x
M,
1
y
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 ofM. 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; }
相关文章推荐
- UVA 11916 Emoogle Grid(同余模)
- UVA 11916 Emoogle Grid(大步小步算法(解模方程对数) 快速幂 模的逆)
- UVA11916 Emoogle Grid
- [离散对数] uva 11916 Emoogle Grid
- UVA 11916 Emoogle Grid 数论
- UVa 11916 - Emoogle Grid (离散对数)
- uva11916 Emoogle Grid
- 【离散对数 && 逆元 && 概率论】UVA - 11916 Emoogle Grid
- UVA 11916 Emoogle Grid 离散对数 大步小步算法
- UVA11916 Emoogle Grid 网格涂色 大步小步算法(解模方程对数) 快速幂 模的逆 模的对数
- [离散对数] uva 11916 Emoogle Grid
- 大步小步算法(网格涂色,uva 11916)
- (UVA)11916 Emoogle Grid
- UVa 11916 (离散对数) Emoogle Grid
- UVA 11916 Emoogle Grid(离散对数、BSGS算法)
- uva 11916 Emoogle Grid (BSGS)
- UVA 11916 - Emoogle Grid(数论)
- [uva 11916]Emoogle Grid 数学 BSGS
- UVA - 11916 Emoogle Grid (离散对数取模)
- uva 11916 - Emoogle Grid(大步小步算法)