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行时可 ...
随机推荐
- 序列化form表单内容为json对象
SourceCode: ; (function ($) { $.fn.extend({ serializeJson: function () { var json = {}; $(this.seria ...
- github客户端创建仓库
1.在github上创建立自己项目仓库 登录后,在github首页,点击页面右下角“New Repository” 填写项目信息: project name: project description ...
- OpenWrt固件刷入后串口终端没有反应的问题
[路由器开发板硬件固件配置] MTK双频:MT7620a + MT7612e 内存:256 MB 闪存:16 MB 固件:MTK自带SDK中的OpenWrt固件(mtksdk-openwrt-2.6. ...
- windows批处理(cmd/bat)编程详解
reference: http://blog.csdn.net/bingjie1217/article/details/12947327 http://www.cnblogs.com/doit8791 ...
- html+css学习笔记 4[定位]
如何让图1中的div2移动到如图2上的位置: 思路:哪些css命令能够影响盒子显示的位置呢? relative相对定位/定位偏移量 position:relative; 相对定位 a ...
- python 日期转星期
import time import datetime today = int(time.strftime('%w')) print today anyday = datetime.datetime( ...
- 数据库范式(1NF 2NF 3NF BCNF)
http://blog.csdn.net/xuxurui007/article/details/7738330 http://www.cnblogs.com/laodao1/archive/2009/ ...
- QC缺陷管理操作-细说(转)
一.缺陷常用字段说明 二.缺陷管理流程图 三.开发人员修改缺陷填写规范 四.项目经理决定延期修改缺陷 一.缺陷常用字段说明 1.摘要 对缺陷的简单描述.摘要包括该缺陷所属的模块名称-子模块名称,以及简 ...
- Unity3d开发wp8问题汇总
原地址:http://blog.csdn.net/sunshine_1984/article/details/12849117 .wp8应用点击返回键没有响应将Unity3d导出wp8工程后,编译工程 ...
- node-firefox 二三事
编者按:本文作者为 Soledad Penadés, Sole 在 Mozilla 的 Tech Evangelism 团队工作,帮助人们在网络上创造神奇的东西.本文主要介绍node-firefox的 ...