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行时可 ...
随机推荐
- iOS开发 适配iOS10以及Xcode8-b
现在在苹果的官网上,我们已经可以下载到Xcode8的GM版本了,加上9.14日凌晨,苹果就要正式推出iOS10系统的推送了,在此之际,iOS10的适配已经迫在眉睫啦,不知道Xcode8 beat版本, ...
- C#开源大全--汇总
商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...
- For和While在C和MATLAB中的区别——MATLAB的大坑
For和while是常见的循环关键字,在许多语言中都是通用的.但是想必不是所有人,都被其中的区别困扰过,尤其是MATLAB“程序员”. x=[,,,,,,]; i=; while i<=leng ...
- Codeforces Beta Round #10 D. LCIS
题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...
- spoj 375 Query on a tree(树链剖分,线段树)
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Sub ...
- 【UOJ】【34】多项式乘法
快速傅里叶变换模板题 算法理解请看<算法导论>第30章<多项式与快速傅里叶变换>,至于证明插值唯一性什么的看不懂也没关系啦-只要明白这个过程是怎么算的就ok. 递归版:(425 ...
- PHP开发框架[国内框架]
1.Thinkphp http://thinkphp.cn/ 2.Brophp http://www.brophp.com/zf/ 由LAMP兄弟连打造 3.WindFramework http ...
- Caffe训练好的网络对图像分类
对于训练好的Caffe 网络 输入:彩色or灰度图片 做minist 下手写识别分类,不能直接使用,需去除均值图像,同时将输入图像像素归一化到0-1直接即可. #include <caffe/c ...
- 6 个基于 jQuery 的表单向导插件推荐
表单向导可以很好地引导用户进行一步一步的操作,从而降低用户错误输入的几率.尽管互联网中有大量的类似插件,但真正好用的不多. 本文整理了6个比较优秀的表单向导插件,希望能够为你带来帮助. 1. Smar ...
- 【设计模式六大原则6】开闭原则(Open Close Principle)
定义:一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 问题由来:在软件的生命周期内,因为变化.升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不 ...