uva 11916 解模方程a^x=b (mod n)
Emoogle Grid |
You have to color an M x N ( 1M, N
108) two dimensional grid. You will be provided K ( 2
K
108) different colors to do so. You will also be provided a list of B( 0
B
500) list of blocked cells of this grid. You cannot color those blocked cells. A cell can be described as (x, y), which points to the y-th cell from the left of the x-th row from the top.
While coloring the grid, you have to follow these rules -
- You have to color each cell which is not blocked.
- You cannot color a blocked cell.
- You can choose exactly one color from K given colors to color a cell.
- No two vertically adjacent cells can have the same color, i.e. cell (x, y) and cell (x + 1, y) cannot contain the same color.

Now the great problem setter smiled with emotion and thought that he would ask the contestants to find how many ways the board can be colored. Since the number can be very large and he doesn't want the contestants to be in trouble dealing with big integers; he decided to ask them to find the result modulo 100,000,007. So he prepared the judge data for the problem using a random generator and saved this problem for a future contest as a giveaway (easiest) problem.
But unfortunately he got married and forgot the problem completely. After some days he rediscovered his problem and became very excited. But after a while, he saw that, in the judge data, he forgot to add the integer which supposed to be the `number of rows'. He didn't find the input generator and his codes, but luckily he has the input file and the correct answer file. So, he asks your help to regenerate the data. Yes, you are given the input file which contains all the information except the `number of rows' and the answer file; you have to find the number of rows he might have used for this problem.
Input
Input starts with an integer T (T150), denoting the number of test cases.
Each test case starts with a line containing four integers N, K, B and R ( 0R < 100000007) which denotes the result for this case. Each of the next B lines will contains two integers x and y ( 1
x
M, 1
y
N), denoting the row and column number of a blocked cell. All the cells will be distinct.
Output
For each case, print the case number and the minimum possible value of M. You can assume that solution exists for each case.
Sample Input
4
3 3 0 1728
4 4 2 186624
3 1
3 3
2 5 2 20
1 2
2 2
2 3 0 989323
Sample Output
Case 1: 3
Case 2: 3
Case 3: 2
Case 4: 20
题目大意:已知N,K,R和B个格子的位置求最小可能的M。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
using namespace std; typedef long long LL;
const int MOD=;
const int Max=;
int N,M,B,K,R,x[Max],y[Max];
set<pair<int,int> > bset; LL mult_mod(LL a,LL b)
{
LL t=;
a%=MOD;
while(b)
{
if(b&) t=(t+a)%MOD;
b>>=;
a=(a<<)%MOD;
}
return t;
} LL pow_mod(LL a,LL b)
{
LL t=;
a%=MOD;
while(b)
{
if(b&) t=mult_mod(t,a);
b>>=;
a=mult_mod(a,a);
}
return t;
} int Extended_Euclid(int a,int b,int &x,int &y)
{
int d,t;
if(b==)
{
x=;y=;return a;
}
d=Extended_Euclid(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return d;
} int inv(int a)
{
int x,y,d;
d=Extended_Euclid(a,MOD,x,y);
x=(x%MOD+MOD)%MOD;
return x;
} int log_mod(int a,int b)
{
int c,v,e=,i;
c=(int)sqrt(MOD+0.5);
v=inv(pow_mod(a,c));
map<int,int> xx;
xx[]=;
for(i=;i<c;i++)//计算e[i]
{
e=mult_mod(e,a);
if(!xx.count(e)) xx[e]=i;
}
for(i=;i<c;i++)
{
if(xx.count(b)) return (i*c+xx[b]);
b=mult_mod(b,v);
}
return -;
} int Count()
{
int cnt;//涂色种数
int c=;//能涂k种的个数
int i;
for(i=;i<B;i++)
//上面是不能涂色的下面是能涂色的情况,排除不能涂色的相邻的情况
if(x[i]!=M && !bset.count(make_pair(x[i]+,y[i])) ) c++;
c+=N;//第一行的都能涂k种色
for(i=;i<B;i++)//减去第一行不能涂色的
if(x[i]==) c--;
//ans=k^c * (k-1)^(m*n-c-b) mod MOD
cnt=mult_mod(pow_mod(K,c),pow_mod(K-,(LL)M*N-B-c));
return cnt;
} int Deal()
{
int i,cnt=Count();
if(cnt==R) return M;
int c=,m;
for(i=;i<B;i++)//不变部分最后一行的下一行能涂k种的个数
if(x[i]==M) c++;
M++;
cnt=mult_mod(cnt,pow_mod(K,c));
cnt=mult_mod(cnt,pow_mod(K-,N-c));
if(cnt==R) return M;
//模方程求解 a^x=b (mod n),用log_mod(a,b,n)函数求解
m=log_mod(pow_mod(K-,N),mult_mod(R,inv(cnt)))+M;
return m;
} int main()
{
int t,i,Case=;
scanf("%d",&t);
while(t--)
{
bset.clear();
Case++;
scanf("%d %d %d %d",&N,&K,&B,&R);
M=;
for(i=;i<B;i++)
{
scanf("%d %d",x+i,y+i);
bset.insert(make_pair(x[i],y[i]));//插入一个座标
if(M<x[i]) M=x[i];
}
printf("Case %d: %d\n",Case,Deal());
}
return ;
}
uva 11916 解模方程a^x=b (mod n)的更多相关文章
- uva 11916 Emoogle Grid (BSGS)
UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...
- UVA 11916 Emoogle Grid(同余模)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 11916 (离散对数) Emoogle Grid
因为题目要求同列相邻两格不同色,所以列与列之间不影响,可以逐列染色. 如果一个格子的上面相邻的格子,已经被染色则染这个格子的时候,共有k-1中选择. 反过来,如果一个格子位于第一列,或者上面相邻的格子 ...
- 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 ...
- 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法
先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝 扩展Baby Step Gian ...
- UVA 1426 - Discrete Square Roots(数论)
UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...
- UVA 11754 (暴力+中国剩余定理)
题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...
随机推荐
- (WWWWWWWWWW)codevs 3305 水果姐逛水果街Ⅱ
写这么长了不A有点舍不得.. 想A又调不出来.. 于是乎就存一下.. 屠龙宝刀点击就送 #include <cstdio> #include <vector> #define ...
- Spring IOC的Bean对象
---恢复内容开始--- 在Spring IOC模块中Bean是非常重要的.在这里我想给大家讲讲关于Bean对象实例化的三种注入方式: 首先,我先讲一下关于Bean对象属性值的两种注入方式:set注入 ...
- 作用域插槽 向父组件传递 <template slot-scope="{ row, index }" slot="dateNo">
作用域插槽 向父组件传递 <template slot-scope="{ row, index }" slot="dateNo"> slotTes ...
- flume启动报错
执行flume-ng agent -c conf -f conf/load_balancer_server.conf -n a1 -Dflume.root.logger=DEBUG,console , ...
- shell脚本,如何写进度条。
[root@localhost ~]# cat jindutiao.sh #!/bin/bash #进度条 n=$((/)) N=$((/)) ` do sleep 0.01 [ $(($i%$n)) ...
- windows使用文件服务器搭建Git服务器
背景: 1.windows下搭建git服务器. 2.git服务器搭建在局域网文件共享区中. 3.没有复杂的权限控制,文件共享区都有访问权限. 步骤: 1.文件共享区中创建git远程仓库. 2.本地克隆 ...
- Hanoi双塔问题
题目描述: 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆盘是不加区分的(下图为n=3的情形).现要将这些圆盘移到C柱上 ...
- emoji等表情符号存mysql的方法
项目中需要存储用户信息(用户昵称有表情符号),自然就遇到了emoji等表情符号如何被mysql DB支持的问题 这里引用先行者博文:https://segmentfault.com/a/1190000 ...
- service worker 消息推送
https://developers.google.com/web/fundamentals/codelabs/push-notifications/?hl=en 首先下载源码: git clone ...
- String中indexof函数的用法
int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引. int indexOf(int ch, int fromIndex) 从指定的索引开始搜索,返回在此字符串中第一次 ...