LRJ petition on the question
#include <stdio.h>#include<iostream>#include<vector>#include<math.h>#include<Set>#include<map>#include<queue>#include<algorithm>#include<string.h>#include<string>using namespaceStd;typedefLong LongLL;ConstLL mod=1e8+7;Const intn=505; LL N,m,k,b,r,x[n],y[n];Set<pair<LL,LL> >bset;voidEXGCD (LL a,ll b,ll& d,ll &x,ll &y) { if(!B) {d=a;x=1; y=0;} Else{EXGCD (b,a%b,d,y,x); y-=x* (A/b);}} ll Pow_mod (ll A,ll p) {ll ans=1; while(p) {if(p&1) ans= (ans*a)%MoD; P/=2; A= (a*a)%MoD; } returnans;} LL INV (ll a) {ll d,x,y; EXGCD (A,mod,d,x,y); returnd==1? (X+mod)%mod:-1;} ll Log_mod (ll A,ll b) {ll tmp,v,e=1; TMP= (LL) sqrt (mod+0.5); V=Inv (Pow_mod (a,tmp)); Map<LL,LL>MP; Mp.clear (); mp[1]=0; for(LL i=1; i<tmp;++i) {e= (e*a)%MoD; if(!mp.count (e)) mp[e]=i; } for(LL i=0; i<tmp;++i) { if(Mp.count (b))returni*tmp+Mp[b]; b= (b*v)%MoD; } return-1;} ll count () {ll C=0; for(LL i=0; i<b;++i)if(X[i]!=m&&!bset.count (Make_pair (x[i]+1, Y[i])) + +C; C+=N; for(LL i=0; i<b;++i)if(x[i]==1)--B; returnPow_mod (K,c) *pow_mod (K-1), n*m-b-c)%MoD;} ll solve () {ll CNT=count (); if(CNT==R)returnm; LL C=0; for(LL i=0; i<b;++i)if(x[i]==m) + +C; ++m; CNT= (Cnt*pow_mod (k,c))%MoD; CNT= (Cnt*pow_mod (k-1, n-c))%MoD; if(CNT==R)returnm; returnLog_mod (Pow_mod (K-1, N), (R*INV (CNT))%mod) +m;}intMain () {intT; scanf ("%d",&T); for(intt=1; t<=t;++t) {scanf ("%lld%lld%lld%lld",&n,&k,&b,&R); Bset.clear (); M=1; for(LL i=0; i<b;++i) {scanf ("%lld%lld",&x[i],&Y[i]); M=Max (m,x[i]); Bset.insert (Make_pair (x[i],y[i)); } printf ("Case %d:%lld\n", T,solve ()); } return 0;}
View Code
UVA 11916 emoogle Grid Discrete logarithm stride Step algorithm