因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色。

如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择。

反过来,如果一个格子位于第一列,或者上面相邻的格子是不能被染色的格子,则共有k中选择。

虽然,矩阵的行数不定,但至少为所有不能被染色格子行标的最大值m。

分别检验一下染m行和m+1行的方案数(mod 100000007)是否为r

否则的话,后面每染一行方案数都会乘p = (k-1)n,假设前面计算出来的m+1行方案数为cnt

下面的任务就是求解 px * cnt = r (mod MOD),两边乘cnt的逆,得 px = r * cnt-1 (mod MOD)

最后加上前面的m+1行,答案为x + m + 1

最后吐槽一下我认为的坑点=_=,看到那个模想当然地以为是1e9+7,但是最后一个样例调了好久没过,后来才发现题中给的模是1e8+7

 #include <cstdio>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
#define MP make_pair
#define INS insert
typedef long long LL; const int MOD = ;
const int maxb = + ;
int n, m, k, b, r, x[maxb], y[maxb], fir_row;
set<pair<int, int> > Set; int mul_mod(int a, int b)
{ return (LL) a * b % MOD; } int pow_mod(int a, LL n)
{
int ans = , base = a;
while(n)
{
if(n & ) ans = mul_mod(ans, base);
base = mul_mod(base, base);
n >>= ;
}
return ans;
} int inv(int a)
{ return pow_mod(a, MOD - ); } int log_mod(int a, int b)
{//a^x=b (mod MOD)
int m, v, e = , i;
m = (int)sqrt(MOD + 0.5);
v = inv(pow_mod(a, m));
map<int, int> x;
x[] = ;
for(i = ; i < m; i++)
{
e = mul_mod(e, a);
if(e == b) return i;
if(!x.count(e)) x[e] = i;
}
for(i = ; i < m; i++)
{
if(x.count(b)) return i*m + x[b];
b = mul_mod(b, v);
}
return -;
} int solve()
{
int c = ;//前m行涂k种颜色的格子个数
for(int i = ; i < b; i++)
if(x[i] != m && !Set.count(MP(x[i] + , y[i]))) c++;
c += n - fir_row;
int cnt = mul_mod(pow_mod(k, c), pow_mod(k-, (LL)m*n - b - c));
if(cnt == r) return m; c = ;//第m+1行涂k种颜色的格子个数
for(int i = ; i < b; i++) if(x[i] == m) c++;
cnt = mul_mod(mul_mod(cnt, pow_mod(k, c)), pow_mod(k-, n-c));
if(cnt == r) return m + ; int p = pow_mod(k-, n);
int v = inv(cnt);
return log_mod(p, mul_mod(r, v)) + m + ;
} int main()
{
//freopen("in.txt", "r", stdin); int T;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
scanf("%d%d%d%d", &n, &k, &b, &r);
Set.clear();
m = ; fir_row = ;
for(int i = ; i < b; i++)
{
scanf("%d%d", &x[i], &y[i]);
Set.INS(MP(x[i], y[i]));
if(x[i] > m) m = x[i];
if(x[i] == ) fir_row++;//第一行不能被涂色的个数
}
printf("Case %d: %d\n", kase, solve());
} return ;
}

代码君

UVa 11916 (离散对数) Emoogle Grid的更多相关文章

  1. uva 11916 Emoogle Grid (BSGS)

    UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...

  2. [uva11916] Emoogle Grid (离散对数)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Emoogle Grid  You have to color an MxN ( ...

  3. uva 11916 解模方程a^x=b (mod n)

      Emoogle Grid  You have to color an M x N ( 1M, N108) two dimensional grid. You will be provided K  ...

  4. UVA11916 Emoogle Grid

    Emoogle Grid You have to color an M × N (1 ≤ M, N ≤ 108 ) two dimensional grid. You will be provided ...

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

    LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <mat ...

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

    假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...

  7. UVA 11916 Emoogle Grid(同余模)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. uva 11916 Emoogle Grid

    题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足 ...

  9. uva11916 Emoogle Grid (BSGS)

    https://uva.onlinejudge.org/external/119/p11916.pdf 令m表示不能染色的格子的最大行号 设>m行时可以染k种颜色的格子数有ck个,恰好有m行时可 ...

随机推荐

  1. 《C++Primer》复习——with C++11 [4]

    考虑到STL的掌握主要靠的是练习,所以对于STL这部分,我把书中的练习都做一遍,加深印象.这些练习是第9.10.11.17章的,分别是顺序容器.泛型算法和关联容器等. ——10月22日 /*----- ...

  2. CentOS安装RockMongo

    rockmongo官网下载页面在这里: http://rockmongo.com/downloads 找到最新版本的下载链接,一般第一个就是: 右键复制url,比如说是这个: http://rockm ...

  3. Matlab 高斯分布 均匀分布 以及其他分布 的随机数

    Matlab 高斯分布 均匀分布 以及其他分布 的随机数 betarnd 贝塔分布的随机数生成器 binornd 二项分布的随机数生成器 chi2rnd 卡方分布的随机数生成器 exprnd 指数分布 ...

  4. 【Asp.Net MVC-视频】

    Asp.Net MVC官网网发布的pluralsight视频教学: http://pluralsight.com/training/Player?author=scott-allen&name ...

  5. 【C++基础】构造函数

    说说你对构造函数的理解? 构造函数:对象创建时,利用特定的值构造对象(不是构造类),将对象初始化(保证数据成员有初始值),是类的一个public 函数 ①   与类同名 ②   无返回值 ③   声明 ...

  6. Unique Binary Search Tree II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. nmap使用详解

    nmap是一个网络探测和安全扫描程序, 系统管理者和个人可以使用这个软件扫描大型的网络,获取那台主机正在运行以及提供什么服务等信息.nmap支持很多扫描技术,例如:UDP.TCP connect(). ...

  8. hdu1022 Train Problem I

    http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...

  9. Project Euler 86:Cuboid route 长方体路径

    Cuboid route A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, ...

  10. lintcode :搜索二维矩阵

    题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...