Bzoj2242:[SDOI2011]计算器:BSGS

10 篇文章 0 订阅
6 篇文章 0 订阅

题目链接:2242:[SDOI2011]计算器

第一问快速幂,第二问扩展欧几里得,第三问BSGS

注意BSGS时要判断y为p的倍数时如果z不为0是无解的,wa了好久

为什么我跑得这样慢QAQ……

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int mod=1331;
const int maxn=1000010;
int T,typ;

struct hashmap{
	int next[maxn],tot,As[maxn],h[mod];
	LL val[maxn];
	void init(){
		memset(h,0,sizeof(h));tot=0;
	}
	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;
	}
}s;

LL powe(LL x,LL y,LL p){
	LL ret=1;
	while (y){
		if (y&1) ret=ret*x%p;
		x=x*x%p; y>>=1;
	}return ret;
}

LL gcd(LL a,LL b){
	return b==0?a:gcd(b,a%b);
}

void exgcd(LL a,LL b,LL &x,LL &y){
	LL t;
	if (!b){x=1;y=0;return;}
	exgcd(b,a%b,x,y);
	t=x; x=y; y=t-a/b*y;
}

void solve1(){
	LL a,b,p;
	for (int i=1;i<=T;++i){
		scanf("%lld%lld%lld",&a,&b,&p);
  		printf("%lld\n",powe(a,b,p));
	}
}

void solve2(){
	LL x,y,a,b,p;
	for (int i=1;i<=T;++i){
		scanf("%lld%lld%lld",&a,&b,&p);
		LL G=gcd(a,p);
		if (b%G!=0)
			{printf("Orz, I cannot find x!\n");continue;}
		a/=G; b/=G; p/=G;
		exgcd(a,p,x,y);
		x=(x*b%p+p)%p;
		while(x<0)x+=p;
		printf("%lld\n",x);
	}
}

void BSGS(LL a,LL b,LL p){
	if (a%p==0)//!
	{printf("Orz, I cannot find x!\n");return;}
	s.init();
	int m=ceil(sqrt(p));
	LL last=1; s.push(1,0);
	for (int i=1;i<m;++i){
		last=(last*a%p+p)%p;
		s.push(last,i);
	}
	LL x,y,ans; exgcd(1,p,x,y);
	x=(x*b%p+p)%p;
	if (s.find(x)!=-1)
		{ans=0*m+s.find(x);printf("%lld\n",ans);return;}
	LL D=last*a%p,tmp=D;
	for (int i=1;i<=m;++i){
		exgcd(D,p,x,y);
		x=(x*b%p+p)%p;
		if (s.find(x)!=-1)
		    {ans=i*m+s.find(x);printf("%lld\n",ans);return;}
		D=(D*tmp%p+p)%p;
	}printf("Orz, I cannot find x!\n");
}

void solve3(){
	LL a,b,p;
	for (int i=1;i<=T;++i){
		scanf("%lld%lld%lld",&a,&b,&p);
		BSGS(a,b,p);
	}
}

int main(){
	scanf("%d%d",&T,&typ);
	if (typ==1) solve1();
	if (typ==2) solve2();
	if (typ==3) solve3();
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值