LRJ白书上的题

#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
typedef long long LL;
const LL mod=1e8+;
const int N=; LL n,m,k,b,r,x[N],y[N];
set<pair<LL,LL> >bset;
void exgcd(LL a,LL b,LL& d,LL &x,LL &y){
if(!b){d=a;x=;y=;}
else {exgcd(b,a%b,d,y,x);y-=x*(a/b);}
}
LL pow_mod(LL a,LL p){
LL ans=;
while(p){
if(p&)ans=(ans*a)%mod;
p/=;
a=(a*a)%mod;
}
return ans;
}
LL inv(LL a){
LL d,x,y;
exgcd(a,mod,d,x,y);
return d==?(x+mod)%mod:-;
}
LL log_mod(LL a,LL b){
LL tmp,v,e=;
tmp=(LL)sqrt(mod+0.5);
v=inv(pow_mod(a,tmp));
map<LL,LL>mp;
mp.clear();
mp[]=;
for(LL i=;i<tmp;++i){
e=(e*a)%mod;
if(!mp.count(e))mp[e]=i;
}
for(LL i=;i<tmp;++i){
if(mp.count(b))return i*tmp+mp[b];
b=(b*v)%mod;
}
return -;
}
LL count(){
LL c=;
for(LL i=;i<b;++i)
if(x[i]!=m&&!bset.count(make_pair(x[i]+,y[i])))++c;
c+=n;
for(LL i=;i<b;++i)if(x[i]==)--c;
return pow_mod(k,c)*pow_mod((k-),n*m-b-c)%mod;
}
LL solve(){
LL cnt=count();
if(cnt==r)return m;
LL c=;
for(LL i=;i<b;++i)if(x[i]==m)++c;
++m;
cnt=(cnt*pow_mod(k,c))%mod;
cnt=(cnt*pow_mod(k-,n-c))%mod;
if(cnt==r)return m;
return log_mod(pow_mod(k-,n),(r*inv(cnt))%mod)+m;
}
int main()
{
int T;
scanf("%d",&T);
for(int t=;t<=T;++t){
scanf("%lld%lld%lld%lld",&n,&k,&b,&r);
bset.clear();
m=;
for(LL i=;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 ;
}

最新文章

  1. Android Studio 导出jar包
  2. Java对象大小计算
  3. Cursor的用法
  4. JQuery mobile中按钮自定义属性的改变
  5. LeetCode &quot;Is Subsequence&quot;
  6. 微信公众号入门学习2_使用C#,ASP.NET APIController如何被动回复用户消息
  7. UI键盘通知
  8. 【bzoj1597】 土地购买
  9. [翻译] java NIO Channel
  10. (转)深入理解PHP之数组(遍历顺序)
  11. POJ2288 Islands and Bridges(TSP:状压DP)
  12. 使用MbrFix.exe修复MBR分区表
  13. Function语义学之member function
  14. Codeforces 526D - Om Nom and Necklace 【KMP】
  15. mahout安装和测试
  16. 国外android开源站点
  17. ECMAScript6之Set结构和Map结构
  18. spring security:ajax请求的session超时处理
  19. windows环境下,怎么解决无法使用ping命令
  20. WRT 版本说明

热门文章

  1. C# 解析带前缀的Xml节点内容
  2. querySelector 和 querySelectorAll 的使用
  3. PHP学习笔记(3) - 奇怪的class与autoload
  4. 走进WCF一 (异常如此多娇,引无数码农竞折煞)
  5. CSS学习_属性选择器
  6. WPF学习笔记2&mdash;&mdash;XAML之2
  7. c语言贪吃蛇
  8. FontDialog组件设置字体
  9. 创建第一个UI
  10. ms flexbox 布局 (ko list)