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个解. 解题思路: 看见模方程 ...
随机推荐
- C语言二维数组作为函数参数
设有整型二维数组a[3][4]如下:0 1 2 34 5 6 78 9 10 11 它的定义为: int a[3][4]={{0,1,2,3},{4,5,6,7} ...
- iTOP-IMX6UL 实战项目:ssh 服务器移植到 arm 开发板
实验环境:迅为提供的Ubuntu12.04.2 以及虚拟机 编译器:arm-2009q3 编译器 开发板系统:QT系统 开发板使用手册中给Windows 系统安装了 ssh 客户端,给 Ubunt ...
- fluent_python1
Magic Method python中有些跟对象本身有关的方法, 以两个下划线开始,两个下划线结束, 一般称为魔法方法(magic method). 比如 obj[key] 的背后就是 __geti ...
- 10048 - Audiophobia (Floyd)
Floyd的变形,本质是动态规划,路径分成的两个部分中取最大值作为该路径的答案,在所有可行路径之中选一个最小值. #include<bits/stdc++.h> using namespa ...
- 多源最短路径 – Floyd-Warshall Algorithm
介绍: 是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权(但不可存在负权回路)的最短路径问题,同时也被用于计算有向图的传递闭包. Floyd-Warshall算法的时间复杂度是O(N3) ...
- 第009课 gcc和arm-linux-gcc和MakeFile
from:第009课 gcc和arm-linux-gcc和MakeFile 第001节_gcc编译器1_gcc常用选项_gcc编译过程详解 gcc的使用方法 gcc [选项] 文件名 gcc常用选项 ...
- 常用的 Excel 函数
概述 Excel 学的好,函数不可少.接下来就了解常用的函数. 首先作下简要说明: 本文的内容大多从网上搜集并加以个人理解整理而来,由于初学,可能会出现错误,如有欢迎指出: 所用演示软件为免费丑陋的 ...
- Mac下搜索神兵利器Alfred 3.1.1最新和谐版
http://bbs.feng.com/read-htm-tid-9891194.html 相比Windows而言Mac自带的Spotlight搜索已经非常强大了,尤其是Mac OS Yosemite ...
- dubbo 多连接,多线程池.
1. consumer 多连接 Dubbo protocol options: <dubbo:protocolname=“dubbo” port=“9090” server=“netty” cl ...
- 新浪oAuth授权
首先要拥有一个微博账号 第一步 成为新浪开发者 1.登录微博开发者界面 open.weibo.com 2. 点击登录 点击移动应用,创建应用 3.需要进行开发者认证,填写个人信息及邮箱认证,等 ...