首页 > 代码库 > UVA - 11916 Emoogle Grid (离散对数取模)
UVA - 11916 Emoogle Grid (离散对数取模)
You have to color an M x N (1M,N108) two dimensional grid. You will be providedK (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 -
- 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 andR (0R < 100000007) which denotes the result for this case. Each of the next B lines will contains two integersx 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个不用染色,其他每个格子涂一种颜色,同一列上下两个格子不能染相同的颜色,给出M,N,K,和B个格子的位置,求出涂色的方案%100000007的结果是R,现在给出N,K,R和B个格子位置,让你求最小的M思路:首先我们可以比较容易想到的是M最起码要和B个格子中X坐标最大的一样,然后我们尝试着一列一列的染色,每列从上到下染,如果一个格子是在第一行或者是在B个格子下面的话。那么就有K种可能,否则就是K-1种可能,现在我们将网格分成两部分,上面的部分是有B个坐标的不变的部分,下面是可变的部分,那么我们先计算出上面不变的部分和可变部分的第一行的和,然后每次添加一行的话就是增加(K-1)^N种可能。现在我们可以推出方程cnt*P^M = R,cnt是前面说的可以计算出来的,M是待增加的,在进行%处理后得到R,那么这就是离散对数取模方程的求解过程了。#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <map> #include <set> #include <cmath> typedef long long ll; using namespace std; const int maxn = 510; const ll mod = 100000007; int n, m, k, b, r, x[maxn], y[maxn]; set<pair<int, int> > best; ll mul_mod(ll a, ll b) { return a * b % mod; } ll pow_mod(ll a, ll p) { ll tmp = 1; while (p) { if (p & 1) tmp = tmp * a % mod; p >>= 1; a = a * a % mod; } return tmp; } 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 d, x, y; gcd(a, mod, d, x, y); return d == 1 ? (x+mod) % mod : -1; } int log_mod(int a, int b) { int m, v, e = 1, i; m = (int)sqrt(mod+0.5); v = inv(pow_mod(a, m)); map<int, int> x; x[1] = 0; for (int i = 1; i < m; i++) { e = mul_mod(e, a); if (!x.count(e)) x[e] = i; } for (int i = 0; i < m; i++) { if (x.count(b)) return i*m + x[b]; b = mul_mod(b, v); } return -1; } int count() { int c = 0; for (int i = 0; i < b; i++) if (x[i] != m && !best.count(make_pair(x[i]+1, y[i]))) c++; c += n; for (int i = 0; i < b; i++) if (x[i] == 1) c--; return mul_mod(pow_mod(k, c), pow_mod(k-1, (ll)m*n-b-c)); } int doit() { int cnt = count(); if (cnt == r) return m; int c = 0; for (int i = 0; i < b; i++) if (x[i] == m) c++; m++; cnt = mul_mod(cnt, pow_mod(k, c)); cnt = mul_mod(cnt, pow_mod(k-1, n-c)); if (cnt == r) return m; return log_mod(pow_mod(k-1, n), mul_mod(r, inv(cnt))) + m; } int main() { int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%d%d%d%d", &n, &k, &b, &r); best.clear(); m = 1; for (int i = 0; i < b; i++) { scanf("%d%d", &x[i], &y[i]); if (x[i] > m) m = x[i]; best.insert(make_pair(x[i], y[i])); } printf("Case %d: %d\n", cas++, doit()); } return 0; }
UVA - 11916 Emoogle Grid (离散对数取模)