UVA 11916 Emoogle Grid(同余模)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3067
题意:用K种颜色给一个N*M的格子涂色。其中有B个格子是不能涂色的。涂色时满足同一列上下紧邻的两个格子的颜色不同。所有的涂色方案模100000007后为R。现在给出M、K、B、R,求一个最小的N,满足题意。
思路:设给出的B个不能涂的格子的最大行坐标为maxX。首先,我们能计算出前maxX行的方案数ans,若ans=R则maxX就是答案。接着,我们能计算出前maxX+1行的方案数ans1,若ans1=R则答案为 maxX+1。否则,设下面还需要t行,那么有ans1*((K-1)^M)^t%100000007=R,将ans1的逆元乘到右侧得到新的R'=R*reverse(ans1),令p=(K-1)^M。那么就成了求最小的t满足p^t%100000007=R'。
int Pow(int x,int y)
{
int ans=1;
while(y)
{
if(y&1) ans=(i64)ans*x%mod;
x=(i64)x*x%mod;
y>>=1;
}
return ans;
}
int Pow(int x,int y,int mod)
{
int ans=1;
while(y)
{
if(y&1) ans=(i64)ans*x%mod;
x=(i64)x*x%mod;
y>>=1;
}
return ans;
}
int exGcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=1; y=0;
return a;
}
int temp=exGcd(b,a%b,x,y);
int t=x;
x=y;
y=t-a/b*y;
return temp;
}
int reverse(int a,int b)
{
int x,y;
exGcd(a,b,x,y);
x=x%b;
if(x<0) x+=b;
return x;
}
int logMod(int a,int b,int n)
{
int m=sqrt(n+0.5);
int v=reverse(Pow(a,m,n),n);
map<int,int> mp;
int x=1,i;
mp[1]=0;
for(i=1;i<m;i++)
{
x=(i64)x*a%n;
if(!mp.count(x)) mp[x]=i;
}
FOR0(i,m)
{
if(mp.count(b)) return i*m+mp[b];
b=(i64)b*v%n;
}
return -1;
}
int m,R,K,B,p;
int cal(int x)
{
if(x<=0) return 1;
if(x==1) return K;
int ans=(i64)K*Pow(K-1,x-1,mod)%mod;
return ans;
}
int cal(vector<int> V,int maxX)
{
int pre=0,ans=1,i;
FOR0(i,SZ(V))
{
ans=(i64)ans*cal(V[i]-pre-1)%mod;
pre=V[i];
}
ans=(i64)ans*cal(maxX-pre)%mod;
return ans;
}
void cal(vector<int> V[N],int n,int maxX)
{
int temp,i,x=m-n;
int ans=Pow(cal(maxX),x);
for(i=1;i<=n;i++)
{
temp=cal(V[i],maxX);
ans=(i64)ans*temp%mod;
temp=SZ(V[i])-1;
if(V[i][temp]<maxX) x++;
}
if(ans==R) PR(maxX);
else
{
ans=(i64)ans*Pow(K-1,x)%mod*Pow(K,m-x)%mod;
if(ans==R) PR(maxX+1);
else
{
temp=(i64)reverse(ans,mod)*R%mod;
PR(maxX+1+logMod(p,temp,mod));
}
}
}
int main()
{
int num=0;
rush()
{
RD(m,K); RD(B,R);
vector<int> V[N];
map<int,int> mp;
int i,x,y,t=0,maxX=0;
FOR1(i,B)
{
RD(x,y);
if(!mp.count(y)) mp[y]=++t;
V[mp[y]].pb(x);
maxX=max(maxX,x);
}
FOR1(i,t) sort(V[i].begin(),V[i].end());
p=Pow(K-1,m,mod);
printf("Case %d: ",++num);
int ans;
if(maxX==0)
{
ans=Pow(K,m);
if(ans==R) puts("1");
else
{
ans=(i64)reverse(ans,mod)*R%mod;
PR(1+logMod(p,ans,mod));
}
}
else cal(V,t,maxX);
}
}
UVA 11916 Emoogle Grid(同余模)的更多相关文章
- uva 11916 Emoogle Grid (BSGS)
UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...
- uva 11916 Emoogle Grid
题意:用K种颜色给一个N*M的格子涂色.其中有B个格子是不能涂色的.涂色时满足同一列上下紧邻的两个格子的颜色不同.所有的涂色方案模100000007后为R.现在给出M.K.B.R,求一个最小的N,满足 ...
- UVA 11916 Emoogle Grid 离散对数 大步小步算法
LRJ白书上的题 #include <stdio.h> #include <iostream> #include <vector> #include <mat ...
- UVA - 11916 Emoogle Grid (组合计数+离散对数)
假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...
- [uva11916] Emoogle Grid (离散对数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Emoogle Grid You have to color an MxN ( ...
- 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 ...
- UVA11916 Emoogle Grid
Emoogle Grid You have to color an M × N (1 ≤ M, N ≤ 108 ) two dimensional grid. You will be provided ...
- UVa 11916 (离散对数) Emoogle Grid
因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色. 如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择. 反过来,如果一个格子位于第一列,或者上面相邻的格子 ...
- uva11916 Emoogle Grid (BSGS)
https://uva.onlinejudge.org/external/119/p11916.pdf 令m表示不能染色的格子的最大行号 设>m行时可以染k种颜色的格子数有ck个,恰好有m行时可 ...
随机推荐
- python之函数式编程
python提供了支持函数式编程的简单机制: 1. map函数 2. filter函数 3. reduce函数. 典型的M/R计算模型. 但还是有点简单...
- 进程和cpu绑定
#include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/sysinfo ...
- PIGCMS提示“你的程序为盗版,非法授权,请联系QQ7530782或者8441010”的修复方法
最近群里又有人发出来微信平台盗版源码这个问题求解决,其实我本人是一直支持正版的,大家有条件的还是购买正好为好,既然有人问我就顺便解决了下,其实很简单,再换个接口就好了,查看了一下是在\PigCms\L ...
- 关于Resources.LoadAssetAtPath
返回的是Object, 返回所在资源路径上的一个资源, 只能应用在unity的编辑器模式下,安卓等平台无效 static function LoadAssetAtPath (assetPath : s ...
- 生鲜电商的O2O之道
- Caffe安装教程(原创)
转载请注明地址 说明:本文档参考自Caffe官网的安装说明,http://caffe.berkeleyvision.org/installation.html 如果对安装过程中,需要用到的依赖不明,请 ...
- spring配置事务
一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...
- Directx3D SimpleSample Sample
在d3d 2010 june这个版本里的samples 不知道为什么SimpleSample Sample 这个 它的documents基本等于没有 Starting point for new Di ...
- 解决asp.net mvc中*.resx资源文件访问报错
个人笔记 问题重现 在asp.net mvc中,使用资源文件会出现一个问题,例如: 紧接着我进入视图界面,输入下面代码: <a href="javascript:void(0);&qu ...
- POJ1321棋盘问题
http://poj.org/problem?id=1321 题意 : 我能说这是迄今为止见到的POJ上第二道中文题吗,既然是中文也很好理解,就不详述了 思路 : 典型的深搜DFS ; #includ ...