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 ( 2K108) different colors to do so. You will also be provided a list of B( 0B500) 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 ( 1xM, 1yN), 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个解. 解题思路: 看见模方程 ...
随机推荐
- 在Windows笔记本上调试运行在iOS设备上的前端应用
我在每天工作中需要在不同的移动设备上测试我们开发的前端应用是否正常工作,比如iOS设备和Android设备.我用的工作笔记本电脑又是Lenovo的,安装的是Windows操作系统. 有的时候一个开发好 ...
- elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解
一.快速入门1. 查看集群的健康状况http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 状 ...
- 原生JS forEach()和map()遍历,jQuery$.each()和$.map()遍历
一.原生JS forEach()和map()遍历 共同点: 1.都是循环遍历数组中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项item,当前 ...
- gcc, g++ - GNU 工程的 C 和 C++ 编译器 (egcs-1.1.2)
总览 (SYNOPSIS) gcc [ option | filename ]... g++ [ option | filename ]... 警告 (WARNING) 本手册页 内容 摘自 GNU ...
- 1991: C语言实验——大小写转换
1991: C语言实验——大小写转换 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 183 Solved: 109[Submit][Status][We ...
- Greenplum/Deepgreen(单机/伪分布)安装文档
Greenplum/Deepgreen数据库安装(单机/伪分布) 首先去官网下载centos7:https://www.centos.org/download/,选择其中一个镜像下载即可,网上随意下载 ...
- Java 的访问权限
public>protected>默认(包访问权限)>private,因为protected除了可以被同一包访问,还可以被包外的子类所访问
- 【树形dp】bzoj1304: [CQOI2009]叶子的染色
又是一道优美的dp Description 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方案应该保证根结点到每个叶子的 ...
- 【Java_多线程并发编程】基础篇——synchronized关键字
1. synchronized同步锁的原理 当我们调用某对象的synchronized方法或代码块时,就获取了该对象的同步锁.例如,synchronized(obj)就获取了“obj这个对象”的同步锁 ...
- (39)zabbix snmp自定义OID nginx监控实例
为什么要自定义OID? 前面的文章已经讲过zabbix如何使用snmp监控服务器,但是他有一个很明显的局限性:只能监控定义好的OID项目 假如我们想知道nginx进程是否在运行?在没有zabbix a ...