题目链接

题意:

  有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案。

  1)每个可涂色的位置必须涂上一种颜色

  2)不可涂色位置不能涂色

  3)每个位置必须从K种颜色中选出一种颜色进行涂色

  4)当前格子(x,y) 上面的那个格子(x+1,y)不能同色

  

  现在已知N, K, B, R, 求满足条件的最小的M

  

思路:

  B个不可涂色位置设为(x1, y1), (x2, y2), (x3, y3), ... , (xb, yb)

  1)M必然 ≥ max(x[i])

  2)设前max(x[i]) 行 与 max(x[i]) + 1 行涂色方案为 cnt, 则 max(x[i]) + 1之后的每一行, 涂色方案都是 (K-1)^N。

  3)设P = (K - 1) ^ N

  存在一个j 使得:

          cnt * P^j = R

  右乘cnt的逆元 cnt ^ -1 得:

          P^j = R * cnt ^ -1

  即求一个最小的j满足上式。

          P^j ≡ R * cnt ^ -1 (mod MOD) , MOD = 100000007

  利用对数取余进行求解

  

  代码如下:

  

 #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 550
#define MOD 100000007
int n, b, r, k, m;
int x[MAXN], y[MAXN];
set<pair<int, int> > best; void ex_gcd(LL a, LL b, LL& d, LL& x, LL& y)
{
if(!b) {d = a; x = ; y = ;}
else
{ex_gcd(b, a%b, d, y, x); y -= x*(a/b);}
} LL inv(LL a)
{
LL d, x, y;
ex_gcd(a, MOD, d, x, y);
return d == ?(x + MOD) % MOD : -;
} LL mul_mod(LL a, LL b)
{
return a * b % MOD;
} LL pow_mod(LL a, LL p)
{
if(p == ) return ;
LL ans = pow_mod(a, p/);
ans = ans * ans % MOD;
if(p % == ) ans = ans * a % MOD;
return ans;
} int log_mod(int a, int b)
{
int m, v, e = , i;
m = (int)sqrt(MOD+0.5);
v = inv(pow_mod(a, m));
map<int, int> x;
x[] = ;
for (int i = ; i < m; i++)
{
e = mul_mod(e, a);
if (!x.count(e))
x[e] = i;
}
for (int i = ; i < m; i++)
{
if (x.count(b))
return i*m + x[b];
b = mul_mod(b, v);
}
return -;
} int solve()
{
int cur = ;
int cnt = ; for(int i = ; i < b; i ++)
if(x[i] != m && !best.count(make_pair(x[i] + , y[i]))) cur ++;
cur += n;
for(int i = ; i < b; i ++)
if(x[i] == ) cur --; // ans = k^cur * (k-1) ^ (n*m - b - cur);
cnt = mul_mod(pow_mod(k, cur), pow_mod(k - , (LL)n * m - b - cur)); if(cnt == r) return m; cur = ;
for(int i = ; i < b; i ++)
if(x[i] == m) cur ++; //ans = cnt * k^cur * (k - 1)^(n - cur);
cnt = mul_mod(cnt, pow_mod(k, cur));
cnt = mul_mod(cnt, pow_mod(k - , n - cur));
m ++; if(cnt == r) return m; //printf("%d %d %d\n", pow_mod(k - 1, n), inv(cnt), log_mod(pow_mod(k - 1, n), mul_mod(r, inv(cnt))));
// P = (k - 1) ^ n, P^x = r * cnt^-1;
return log_mod(pow_mod(k - , n), mul_mod(r, inv(cnt))) + m;
} int main()
{
int T;
scanf("%d", &T);
for(int kcase = ; kcase <= T; kcase ++)
{
scanf("%d %d %d %d", &n, &k, &b, &r);
best.clear();
m = ;
for(int i = ; i < b; i ++)
{
scanf("%d %d",&x[i], & y[i]);
m = max(m, x[i]);
best.insert(make_pair(x[i], y[i]));
}
printf("Case %d: %d\n", kcase, solve());
}
return ;
}

  这题WA了一天, 反复找错都没找到, 重写第N + 1次的时候终于找到错误 , 求逆函数写错了 囧

Uva_11916 Emoogle Grid的更多相关文章

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

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

  2. UVA11916 Emoogle Grid

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

  3. UVA 11916 Emoogle Grid(同余模)

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

  4. UVa 11916 (离散对数) Emoogle Grid

    因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色. 如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择. 反过来,如果一个格子位于第一列,或者上面相邻的格子 ...

  5. uva 11916 Emoogle Grid

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

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

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

  7. uva11916 Emoogle Grid (BSGS)

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

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

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

  9. uva 11916 Emoogle Grid (BSGS)

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

随机推荐

  1. FontCombobox 和FontSizeCombobox

    附件:http://files.cnblogs.com/xe2011/WindowsFormsFontCombox.rar 自定义组件字体组合框 自定义组件字体组合框如何使用 自定义组件字体大小组合框 ...

  2. im消息丢失插件

    https://github.com/laughin/mocamsg mocamsg Moca message interceptor Openfire网络不好的情况下经常丢消息,一般情况都是服务器端 ...

  3. 自定义String类,并且实现在STL容器中添加自定义的类型

    13.44 编写标准库string类的简化版本,命名String.你的类应该至少有一个默认构造函数和一个接受C风格字符串指针参数的构造函数.使用allocator为你的String类分配所需内存. 1 ...

  4. 学习PHP时的一些总结(二)

    类中的构造方法和析构方法: 构造方法是对象创建完成后第一个被对象自动调用的方法.析构方法是对象在销毁之前最后一个被对象自动调用的方法. 如果没有显示的声明构造方法,类中都会默认存在一个没有参数列表并且 ...

  5. 利用反射把数据库查询到的数据转换成Model、List(改良版)

    之前也写过一篇这样的博文,但是非常的粗糙.    博文地址 后来看到了一位前辈(@勤快的小熊)对我的博文的评论后,让我看到了更加优雅的实现方式,于是重构了之前的代码. public static Li ...

  6. ObjectInputStream ObjectOutStream

    1:不能多次read 2:read 与write操作必须一对一

  7. CentOS 6.7下iPython提示“WARNING: Readline services not available or not loaded.”的解决办法

    yum install readline-devel 然后,使用pip或者easy_install安装readline即可 pip install readline

  8. 造一个Badge Service(徽章)的轮子

    什么是Badge Service 细心的读者朋友一定在很多Github的Repo,npm的package页面看到过诸如 的徽章.这些徽章是干什么用的? 大家看到上文中我引用的Badge的左侧,是Dow ...

  9. WPF中窗口控件的跨线程调用

    在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...

  10. [FTP] FTPOperater--FTP操作帮助类 (转载)

    点击下载 FTPOperater.zip 这个类是关于FTP的一些操作的1.连接FTP服务器 2.上传3.下载4.删除文件5.获取当前目录下明细(包含文件和文件夹)  6.获取FTP文件列表(包括文件 ...