您的位置:首页 > 其它

UVa 11916 - Emoogle Grid (离散对数)

2014-08-19 21:05 483 查看



Emoogle Grid

You have to color an M x N ( 1

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 (T

150),
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 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个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个格子不能涂相同的颜色。给出M,N,K和B个格子的位置,求出涂色方案的种数取余100000007的结果R。

本题的任务刚好和这个相反:已知NKR和B个格子的位置,求最小可能的M。

稍微一看就可以得出

每一列涂色是独立的,同一列的只有第一行的和该格子上面不涂色的有K种涂色方案,其他都是K-1种方案。

那么最后的就可以得到 K^C * (K-1)^(n*m-C-B) = R (mod MOD)

其中C为可涂k种颜色的格子数

现在已知nkrb,要求m 用离散对数可以求出

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int MOD = 100000007;
const int maxn = 1000;
const int maxb = 500 + 20;
int n, K, B, R, m;
set< pair<int,int> > bset;
int x[maxb], y[maxb];

LL mul_mod(LL a, LL b, LL c) {
return a * b % c;
}

int pow_mod(LL a, LL p, LL c) {
if(p == 0) return 1;
LL ans = pow_mod(a, p/2, c);
ans = ans * ans % c;
if(p&1) ans = ans * a % c;
return ans;
}

int inv(int a) {
return pow_mod(a, MOD-2, MOD);
}

int log_mod(int a, int b) {
int m, v, e = 1, i;
m = (int)sqrt(MOD);
v = inv(pow_mod(a, m, MOD));
map <int,int> x;
x[1] = 0;
for(i = 1; i < m; i++){ e = mul_mod(e, a, MOD); if (!x.count(e)) x[e] = i; }
for(i = 0; i < m; i++){
if(x.count(b)) return i*m + x[b];
b = mul_mod(b, v, MOD);
}
return -1;
}

int count() {
int c = 0;
for(int i=0; i<B; i++) {
if(x[i] != m && !bset.count(make_pair(x[i]+1, y[i]))) c++;
}
c += n;
for(int i=0; i<B; i++) if(x[i] == 1) c--;
LL t1 = pow_mod(K, c, MOD);
LL t2 = pow_mod(K-1, (LL)n*m-c-B, MOD);
return mul_mod(t1, t2, MOD);
}

int solve() {
int cnt = count();
if(cnt == R) return m;
int c = 0;
for(int i=0; i<B; i++) if(x[i] == m) c++;
m++;
int tcnt= mul_mod(pow_mod(K, c, MOD), pow_mod(K-1, n-c, MOD), MOD);
cnt = mul_mod(cnt, tcnt, MOD);
if(cnt == R) return m;
// (k-1)^n * cnt = R (mod MOD)
LL t1 = pow_mod(K-1, n, MOD);
LL t2 = mul_mod(R, inv(cnt), MOD);
return log_mod(t1, t2)+ m;
}

int main() {
int T;

scanf("%d", &T);
for(int kase=1; kase<=T; kase++) {
m = 1;
bset.clear();
scanf("%d%d%d%d", &n, &K, &B, &R);
for(int i=0; i<B; i++) {
scanf("%d%d", &x[i], &y[i]);
m = max(x[i], m);
bset.insert(make_pair(x[i], y[i]));
}
int ans = solve();
printf("Case %d: %d\n", kase, ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: