UVA - 11916 Emoogle Grid (离散对数取模)

You have to color an M x N (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个不用染色,其他每个格子涂一种颜色,同一列上下两个格子不能染相同的颜色,给出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;
}

时间: 2024-08-11 23:31:31

UVA - 11916 Emoogle Grid (离散对数取模)的相关文章

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

LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <math.h> #include <set> #include <map> #include <queue> #include <algorithm> #include <string.h> #include <string> using

UVA 11916 - Emoogle Grid(数论)

UVA 11916 - Emoogle Grid 题目链接 题意:一个N列的网格,有B个格子可以不涂色,其他格子各涂一种颜色,现在一共有k种颜色,要求同一列格子颜色不能相同,问总方案数 MOD 100000007答案等于R时最小的M是多少. 思路:先把格子分为两部分,有不涂色的一部分,没有的一部分,然后计算出有的情况数,之后如果每多一行,每个格子上能涂颜色必然是k - 1种,也就是每多一行多(k - 1)^n总方案,所以也就是求 前一部分情况 * ((k - 1)^n)^x % MOD = R时

uva 11916 - Emoogle Grid(大步小步算法)

题目连接:uva 11916 - Emoogle Grid 题目大意:有一问题,在M行N列的网格上涂K种颜色,其中有B个格子不用涂色,其它每个格子涂一种颜色,同一列的上下两个相邻的格子不能涂相同的颜色.给出M,N,K和B个格子的位置,求出总方案数模掉1e8+7的结果R.现在已知R,求最小的M. 解题思路:有确定不用涂色格子的区域作为不变部分,总数通过计算为tmp,外加可变部分的第一行,方案数为cnt,可变部分除第一行外,每加一行都将总数乘以(K?1)N,既有 cnt?PM=Rmod(1e8+7)

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

假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1e8+7的结果R. 本题的任务和这个相反:已知N,K,R和B个格子的位置,求最小可能的M. 蓝书(大白)上的例题,设xm为不能涂色的格子的最大x值,则分三种情况讨论:M=xm,M=xm+1,M>xm+1.前两种用组合公式直接算,第三种可设前xm+1行的格子涂色方法有n种,由于每增加一行,总涂色方案数

uva 11916 Emoogle Grid

题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足题意. 思路:分成两个部分.设给出的B个不能涂的格子的最大行坐标为m. 首先,我们能计算出前m行的方案数cnt,若cnt=r,则m就是答案.每增加一行,就会增加(K-1)^m种方法,接着令p=(K-1)^M,我们能计算出前m+1行的方案数cnt,若cnt=r 则答案为 m+1.否则,设下面还需要t行

[uva11916] Emoogle Grid (离散对数)

转载请注明出处: 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 blo

UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

题目链接:Uva 11582 [vjudge] 题意 输入两个非负整数a.b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值,当中f(0) = 0,f(1) =  1.且对随意非负整数i.f(i+2)= f(i+1)+f(i). 分析 全部的计算都是对n取模.设F(i) =f(i)mod n, 非常easy发现,F(x)是具有周期性的,由于对N取模的值最多也就N个,当二元组(F(i-1),F(i))反复的时候.整个序列也就反复了.周期i

Uva 11609 - Team ( 组合数学 + 二项式性质 + 快速幂取模 )

Uva 11609 - Team ( 组合数学 + 二项式性质 + 快速幂取模 ) 题意: 有N个人,选一个或多个人参加比赛,其中一名当队长,有多少种方案? (如果参赛者完全相同但是队长不同,也算是一种情况) [ 1<=n <= 10^9 ] 分析: 这题要用到组合式公式的性质 转化之后快速幂取模轻松搞定之 代码: //Uva 11609 - Team /* 组合数公式 + 二项式系数性质 + 快速幂 手动自己推 -> F[n] = C(n,1)*1 + C(n,2)*2 + C(n,n

UVA 11582 - Colossal Fibonacci Numbers!(数论)(分治法幂取模)

巨大的斐波那契数! 题目大意:斐波那契数列f[N],给你a,b,n,求f[a^b]%n. 思路:数论题.f[a^b]%n是有周期的,我们求出来这个周期后就可以将简化成f[(a%周期)^b]%周期运用分治法幂取模. 注意用unsigned long long(貌似是 long long的二倍),不然会溢出,又学了一招... 不知道哪的bug,一直改不对,一直,后来捡来别人的和自己一样的代码一改就对了,,, #include<iostream>//UVA #include<cstdio>