Bzoj3239:Discrete Logging:BSGS模板题
2016-04-11 20:57
441 查看
题目链接3239:Discrete Logging
裸BSGS,终于会写了QAQ
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=1500010;
const int mod=99997;
LL p,b,c,ans;
bool flag;
struct hashmap{
int next[maxn],tot;
LL val[maxn];
int h[mod],As[maxn];
void push(LL x,int id){
int now=x%mod; if (now<0) now+=mod;
for (int i=h[now];i;i=next[i])
if (val[i]==x) return;
++tot; next[tot]=h[now]; h[now]=tot;
val[tot]=x; As[tot]=id; return;
}
int find(LL x){
int now=x%mod; if (now<0) now+=mod;
for (int i=h[now];i;i=next[i])
if (val[i]==x) return As[i];
return -1;
}
void init(){
memset(h,0,sizeof(h)); tot=0;
}
}s;
void exgcd(LL D,LL p,LL &x,LL &y){
LL t;
if(!p){x=1;y=0;return;}
exgcd(p,D%p,x,y);
t=x; x=y; y=t-D/p*y;
}
int main(){
while (~scanf("%lld%lld%lld",&p,&b,&c)){
s.init();
int m=ceil(sqrt(p));
LL last=1; s.push(1,0);
for (int i=1;i<m;++i){
LL x=(last*b%p+p)%p;
s.push(x,i); last=x;
}
LL x,y;
exgcd(1,p,x,y);
x=(x*c%p+p)%p;
if (s.find(x)!=-1)
{ans=0*m+s.find(x);printf("%lld\n",ans);continue;}
LL D=last*b%p,tmp=D;
flag=0;
for (int i=1;i<=m;++i){
exgcd(D,p,x,y);
x=(x*c%p+p)%p;
if (s.find(x)!=-1)
{ans=i*m+s.find(x);printf("%lld\n",ans);flag=1;break;}
D=(D*tmp%p+p)%p;
}
if (!flag) printf("no solution\n");
}
}
裸BSGS,终于会写了QAQ
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=1500010;
const int mod=99997;
LL p,b,c,ans;
bool flag;
struct hashmap{
int next[maxn],tot;
LL val[maxn];
int h[mod],As[maxn];
void push(LL x,int id){
int now=x%mod; if (now<0) now+=mod;
for (int i=h[now];i;i=next[i])
if (val[i]==x) return;
++tot; next[tot]=h[now]; h[now]=tot;
val[tot]=x; As[tot]=id; return;
}
int find(LL x){
int now=x%mod; if (now<0) now+=mod;
for (int i=h[now];i;i=next[i])
if (val[i]==x) return As[i];
return -1;
}
void init(){
memset(h,0,sizeof(h)); tot=0;
}
}s;
void exgcd(LL D,LL p,LL &x,LL &y){
LL t;
if(!p){x=1;y=0;return;}
exgcd(p,D%p,x,y);
t=x; x=y; y=t-D/p*y;
}
int main(){
while (~scanf("%lld%lld%lld",&p,&b,&c)){
s.init();
int m=ceil(sqrt(p));
LL last=1; s.push(1,0);
for (int i=1;i<m;++i){
LL x=(last*b%p+p)%p;
s.push(x,i); last=x;
}
LL x,y;
exgcd(1,p,x,y);
x=(x*c%p+p)%p;
if (s.find(x)!=-1)
{ans=0*m+s.find(x);printf("%lld\n",ans);continue;}
LL D=last*b%p,tmp=D;
flag=0;
for (int i=1;i<=m;++i){
exgcd(D,p,x,y);
x=(x*c%p+p)%p;
if (s.find(x)!=-1)
{ans=i*m+s.find(x);printf("%lld\n",ans);flag=1;break;}
D=(D*tmp%p+p)%p;
}
if (!flag) printf("no solution\n");
}
}
相关文章推荐
- WHOIS类的修改版
- BZOJ3275 Number (最小割)
- Begins and tests
- USACO2013 Nov. Gold T3,一道集合DP
- 数学-素数筛及其拓展
- 最大流模板
- 计算几何模板
- 各种树模板(splay,线段树,可持久化线段树...)
- 上下界网络流初探
- 二分图匹配模板
- CCC 2015 总结&回顾
- 【数论】组合数求模
- 【计算几何】POJ 2318 & POJ 2398
- ACM/ICPC World Finals 2013 A Self-Assembly
- 北京集训队 2016 Day4 alarm
- [bzoj1003] [ZJOI2006]物流运输trans
- [bzoj1500][NOI2005]维修数列
- [bzoj1208] [HNOI2004]宠物收养所
- [bzoj1269][AHOI2006]文本编辑器editort
- [bzoj1503][NOI2004]郁闷的出纳员