[uva 11916]Emoogle Grid 数学 BSGS
来源:互联网 发布:python将字典传入函数 编辑:程序博客网 时间:2024/06/01 09:59
11916 - 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
43 3 0 17284 4 2 1866243 13 32 5 2 201 22 22 3 0 989323
Sample Output
Case 1: 3Case 2: 3Case 3: 2Case 4: 20
#include <cstdio>#include <iostream>#include <algorithm>#include <ctime>#include <cctype>#include <cmath>#include <string>#include <cstring>#include <stack>#include <queue>#include <list>#include <vector>#include <map>#include <set>#define sqr(x) ((x)*(x))#define LL long long #define INF 0x3f3f3f3f#define PI acos(-1.0)#define eps 1e-10#define mod 100000007using namespace std;void egcd(LL a,LL b,LL &x,LL &y){ if (b==0) { x=1; y=0; return ; } egcd(b,a%b,x,y); LL t=x; x=y;y=t-a/b*y;}LL qmod(LL x,LL y){ LL ans=1; LL poo=x; while (y) { if (y&1){ ans=(ans*poo)%mod; } poo=(poo*poo)%mod; y/=2; } return ans;}set <LL> sett;set <LL> ::iterator it;map <LL , int> times;int main(){ LL n,k,r,cntm,fbl,lbl;//fbl代表需要用多少个k lbl代表需要用多上个k-1 int T,b,x,y,ca=0; scanf("%d",&T); while (T--) { scanf("%lld%lld%d%lld",&n,&k,&b,&r); sett.clear(); times.clear(); cntm=1; for (int i=1;i<=b;i++) { scanf("%d%d",&x,&y); cntm=max(cntm,(LL)x); sett.insert((LL)x*(n+1)+y); } printf("Case %d: ",++ca ); fbl=n; lbl=n*(cntm-1); for (it=sett.begin(); it!=sett.end();it++) { x=(*it)/(n+1); y=(*it)%(n+1); if (x==1) fbl--; else lbl--; if ((x!=cntm)&&(sett.find((x+1)*(n+1)+y)==sett.end())) {lbl--;fbl++;} } LL tmp=1; tmp=(tmp*qmod((LL)k,fbl))%mod; tmp=(tmp*qmod((LL)(k-1),lbl))%mod; if (tmp==r) { printf("%lld\n",cntm ); continue; } cntm++; fbl=n; lbl=n*(cntm-1); for (it=sett.begin(); it!=sett.end();it++) { x=(*it)/(n+1); y=(*it)%(n+1); if (x==1) fbl--; else lbl--; if ((x!=cntm)&&(sett.find((x+1)*(n+1)+y)==sett.end())) {lbl--;fbl++;} } tmp=1; tmp=(tmp*qmod((LL)k,fbl))%mod; tmp=(tmp*qmod((LL)(k-1),lbl))%mod; if (tmp==r) { printf("%lld\n",cntm ); continue; } LL t=qmod((LL)k-1,n)%mod; LL cur=1; if (tmp==r) { printf("%lld\n",cntm ); continue; } for (int i=1;i<10002;i++) { times[(cur*tmp)%mod]=i; cur=(cur*t)%mod; } cur=(cur*t)%mod; t=cur; cur=1; LL ans=mod+1; for (int i=1;i<10002;i++) { LL xx,yy; egcd(cur,mod,xx,yy); xx=(xx+mod)%mod; LL inve=(xx*(r))%mod; if (times[inve]) {ans=min(ans,(LL)(times[inve])-1+(i-1)*10002);} cur=(cur*t)%mod; } printf("%lld\n",ans+cntm); } return 0;}
- [uva 11916]Emoogle Grid 数学 BSGS
- UVA 11916 Emoogle Grid(离散对数、BSGS算法)
- UVA 11916 Emoogle Grid 数论
- UVA 11916 - Emoogle Grid(数论)
- UVA 11916 Emoogle Grid(离散对数)
- uva 11916 - Emoogle Grid(大步小步算法)
- UVa 11916 - Emoogle Grid (离散对数)
- [离散对数] uva 11916 Emoogle Grid
- [离散对数] uva 11916 Emoogle Grid
- UVA - 11916 Emoogle Grid (离散对数取模)
- UVA 11916 Emoogle Grid(模指数方程)
- UVA 11916 Emoogle Grid(大步小步算法(解模方程对数) 快速幂 模的逆)
- uva11916 Emoogle Grid
- UVA11916 Emoogle Grid
- UVa 12279 - Emoogle Balance
- uva11916 - Emoogle Grid 网格涂色
- 数学模板-BSGS&&EXBSGS
- Emoogle Grid (方案数,逆元求解,对数求解)
- hdu - 2669 - Romantic(扩展欧几里得)
- mina整合spring ,服务端反向发送消息到客户端 完整实例
- 十月学习总结
- HDU 2136 Largest prime factor 参考代码
- 听姚明的,鼓励身体对抗!
- [uva 11916]Emoogle Grid 数学 BSGS
- 寻找数组最大K个数
- Revenge of LIS II (hdu 5087 LIS)
- VS使用过程中的常用技巧
- onBlur事件与onfocus事件(js)
- 辗转相除法的证明
- 【Android】弹出菜单popupmenu
- 该段代码编译运行后显示的是蓝色背景,白色闪烁字的效果
- 离散余弦变换DCT