UVA11916 Emoogle Grid

Emoogle Grid

 

You have to color an M × 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 – 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 (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

 

这题先看已知部分和已知部分的下一行,不难统计出方案数cmt

每一加一行未知部分,会增加(k - 1)^m

解一个cnt * ((k - 1)^m)^p = r mod MOD

BSGS即可

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <map>
  8 #include <cmath>
  9 #include <utility>
 10 #include <vector>
 11 #define min(a, b) ((a) < (b) ? (a) : (b))
 12 #define max(a, b) ((a) > (b) ? (a) : (b))
 13 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
 14 inline void swap(long long &a, long long &b)
 15 {
 16     long long tmp = a;a = b;b = tmp;
 17 }
 18 inline void read(long long &x)
 19 {
 20     x = 0;char ch = getchar(), c = ch;
 21     while(ch < '0' || ch > '9') c = ch, ch = getchar();
 22     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
 23     if(c == '-') x = -x;
 24 }
 25 const long long INF = 0x3f3f3f3f;
 26 const long long MAXB = 500 + 10;
 27 const long long MOD = 100000007; 
 28 long long t, n, m, k, b, r, x[MAXB], y[MAXB], ma, cnt;
 29 long long pow(long long a, long long b, long long mod)
 30 {
 31     long long r = 1, base = a;
 32     for(;b;b >>= 1)
 33     {
 34         if(b & 1) r *= base, r %= mod;
 35         base *= base, base %= mod;
 36     }
 37     return r;
 38 }
 39 long long ni(long long x, long long mod)
 40 {
 41     return pow(x, mod - 2, MOD);
 42 }
 43 std::map<std::pair<int, int>, int> mp;
 44 std::map<int, int> mmp;
 45 //求a^m = b % mod 
 46 long long BSGS(long long a, long long b, long long mod)
 47 {
 48     long long m = sqrt(mod), tmp = 1, ins = ni(pow(a, m,mod), mod);
 49     mmp.clear();
 50     for(register long long i = 0;i < m;++ i)
 51     {
 52         if(!mmp.count(tmp)) mmp[tmp] = i;
 53         tmp = tmp * a % MOD;
 54     }
 55     for(register long long i = 0;i < m;++ i)
 56     {
 57         if(mmp.count(b)) return i * m + mmp[b];
 58         b = (b * ins) % MOD;
 59     }
 60     return -1;
 61 }
 62 
 63 //计算可变部分方案数 
 64 long long count()
 65 {
 66     long long tmp = m;//有k种涂法的方案数
 67     for(register long long i = 1;i <= b;++ i)
 68     {
 69         if(x[i] != n && !mp.count(std::make_pair(x[i] + 1, y[i]))) ++ tmp;
 70         if(x[i] == 1) -- tmp;
 71     }
 72     return pow(k, tmp, MOD) * pow(k - 1, n * m - tmp - b, MOD) % MOD;
 73 }
 74 
 75 long long solve()
 76 {
 77     long long cnt = count();
 78     if(cnt == r) return n;
 79     long long tmp = 0;
 80     for(register long long i = 1;i <= b;++ i)
 81         if(x[i] == n) ++ tmp;
 82     cnt = cnt * pow(k, tmp, MOD) % MOD * pow(k - 1, m - tmp, MOD) % MOD;
 83     ++ n;
 84     if(cnt == r) return n;
 85     return (BSGS(pow(k - 1, m, MOD), r * ni(cnt, MOD) % MOD, MOD) + n)%MOD;
 86 }
 87 
 88 int main()
 89 {
 90     read(t);
 91     for(register long long v = 1;v <= t;++ v)
 92     {
 93         read(m), read(k), read(b), read(r);
 94         n = 1;
 95         mp.clear();
 96         for(register long long i = 1;i <= b;++ i)
 97         {
 98             read(x[i]), read(y[i]);
 99             mp[std::make_pair(x[i], y[i])] = 1;
100             n = max(n, x[i]);
101         }
102         printf("Case %lld: %lld\n", v, solve());
103     }
104     return 0;
105 }
UVA11916

 

posted @ 2018-01-16 15:55  嘒彼小星  阅读(336)  评论(0编辑  收藏  举报