BZOJ2242 [SDOI2011]计算器

16 篇文章 0 订阅
1 篇文章 0 订阅

第一问快速幂,第二问扩欧或者快速幂,第三问BSGS,模板题

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
using namespace std;
#define MAXN 1010
#define MAXM 1010
#define INF 1000000000
#define MOD 1000000007
#define eps 1e-8
#define ll long long
ll a,b,p;
ll mi(ll x,ll y,ll mod){
	ll re=1;
	while(y){
		if(y&1){
			(re*=x)%=mod;
		}
		(x*=x)%=mod;
		y>>=1;
	}
	return re;
}
map<ll,ll>h;
void cal1(){
	printf("%lld\n",mi(a,b,p));
}
ll gcd(ll x,ll y){
	return !y?x:gcd(y,x%y);
}
void exgcd(ll a,ll b,ll &x,ll &y){
	if(!b){
		x=1;
		y=0;
		return ;
	}
	exgcd(b,a%b,x,y);
	ll t=x;
	x=y;
	y=t-a/b*y;
}
void cal2(){
	ll g=gcd(a,p);
	if(b%gcd(a,p)){
		printf("Orz, I cannot find x!\n");
		return ;
	}
	ll A=a;
	ll B=p;
	A/=g;
	B/=g;
	b/=g;
	ll x,y;
	exgcd(A,B,x,y);
	x*=b;
	((x%=B)+=B)%=B;
	printf("%lld\n",x);
}
void cal3(){
	int i;
	a%=p;
	b%=p;
	if(!a&&b){
		printf("Orz, I cannot find x!\n");
		return ;
	}
	ll m=ceil(sqrt(p))+1;
	h.clear();
	ll now=b*a%p;
	for(i=1;i<=m;i++){
		h[now]=i;
		(now*=a)%=p;
	}
	ll t=mi(a,m,p);
	now=t;
	for(i=1;i<=m;i++){
		if(h[now]){
			printf("%lld\n",i*m-h[now]);
			return ;
		}
		(now*=t)%=p;
	}
	printf("Orz, I cannot find x!\n");
}
int main(){
	int tmp,o;
	scanf("%d%d",&tmp,&o);
	while(tmp--){
		scanf("%lld%lld%lld",&a,&b,&p);
		if(o==1){
			cal1();
		}
		if(o==2){
			cal2();
		}
		if(o==3){
			cal3();
		}
	}
	return 0;
}

/*

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值