POJ 3243

Babystep算法。具体为什么,我也不太明白,好像资料不多。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int Maxn=65535;

struct hash{
	int a,b,next;
}Hash[Maxn*2];
int flag[Maxn+66];
int top,idx;

void insert(int a,int b){
	int k=b&Maxn;
	if(flag[k]!=idx){
		flag[k]=idx;
		Hash[k].next=-1;
		Hash[k].a=a;
		Hash[k].b=b;
		return ;
	}
	while(Hash[k].next!=-1){
		if(Hash[k].b==b) return ;
		k=Hash[k].next;
	}
	Hash[k].next=++top;
	Hash[top].next=-1;
	Hash[top].a=a;
	Hash[top].b=b;
}

int find(int b){
	int k=b&Maxn;
	if(flag[k]!=idx) return -1;
	while(k!=-1){
		if(Hash[k].b==b)
		return Hash[k].a;
		k=Hash[k].next;
	}
	return -1;
}

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

int ext_gcd(int a,int b,int &x,int &y){
	int t,ret;
	if(!b){
		x=1; y=0;
		return a;
	}
	ret=ext_gcd(b,a%b,x,y);
	t=x; x=y; y=t-a/b*y;
	return ret;
}

int Inval(int a,int b,int n){
	int x,y,e;
	ext_gcd(a,n,x,y);
	e=(long long )x * b%n;
	return e<0?e+n:e;
}

int pow_mod(long long a,int b,int c){
	long long ret=1%c; a%=c;
	while(b){
		if(b&1)
		ret=ret*a%c;
		a=a*a%c;
		b=b>>1;
	}
	return ret;
}

int BabyStep(int A,int B,int C){
	top=Maxn; ++idx;
	long long buf=1%C,D=buf,K;
	int i,d=0,tmp;
	for(i=0;i<=100;buf=buf*A%C,i++){
		if(buf==B)
		return i;
	}
	while((tmp=gcd(A,C))!=1){
		if(B%tmp) return -1;
		++d;
		C/=tmp;
		B/=tmp;
		D=D*A/tmp%C;
	}
	int M=(int)ceil(sqrt((double)C));
	for(buf=1%C,i=0; i<= M; buf=buf*A%C,i++){
		insert(i,buf);
	}
	for(i=0,K=pow_mod((long long )A,M,C);i<=M;D=D*K%C,i++){
		tmp=Inval((int)D,B,C); int w;
		if(tmp>=0&&(w=find(tmp))!=-1)
		return i*M+w+d;
	}
	return -1;
}

int main(){
	int A,B,C;
	while(scanf("%d%d%d",&A,&C,&B)!=EOF&&A||B||C){
		B=B%C;
	//	idx=0;   此处初始后竟然错了,看来真想不明白babystep的为什么了
		int ans=BabyStep(A,B,C);
		if(ans==-1) printf("No Solution\n");
		else printf("%d\n",ans);
	}
	return 0;
}

  

时间: 2024-08-03 04:12:14

POJ 3243的相关文章

poj 3243 Clever Y 高次方程

1 Accepted 8508K 579MS C++ 2237B/** 2 hash的强大,,还是高次方程,不过要求n不一定是素数 3 **/ 4 #include <iostream> 5 #include <cstdio> 6 #include <cmath> 7 #include <cstring> 8 #include <algorithm> 9 using namespace std; 10 long long a,b,n; 11 co

Clever Y POJ - 3243 (扩展BSGS)

Clever Y POJ - 3243 题意:给a,c,b,求最小的x使得 ax≡b (mod c). 扩展BSGS算法~ 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #define ll long long 6 using namespace std; 7 const int mod=99991; 8 ll head[mod],nex

POJ 3243 Clever Y 扩展BSGS

http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details/73162229 在大约sqrt( p )的复杂度求出 ( a^x ) % p = b % p中的x 扩展bsgs增加了对p不是素数的情况的处理. 扩展bsgs在处理过a,b,p之后进行bsgs的时候x处理不到num以下的部分,这部分在处理a,b,p的时候处理过了(b=1输出num)所以不用考虑.

POJ 3243 Clever Y BSGS

Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6861   Accepted: 1676 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, give

【POJ 3243】Clever Y 拓展BSGS

调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那是因为聪哥早就会了,所以他觉得学这个没用,信他才怪233 #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef lo

POJ 3243 // HDU 2815(改下输出,加个判断)

A^x = B (mod C) 的模板题,不够要用扩展BSGS (虽然AC,但完全理解不了模板0.0,以后学好数学在来慢慢理解555555) #include <iostream> #include <cstdio> #include <ctime> #include <cmath> const int MAXN = 1000 + 10; const int maxn = 65535; const int INF = 0x7fffffff; using na

POJ 3243 Clever Y Extended-Baby-Step-Giant-Step

题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quartergeek.com/bsgs/ http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 这两位写的比較具体0.0 能够用于參考 对拍时发现自己代码各种脑残0.0 伤不起啊 #include<cmath> #include<cstd

【POJ】3243 Clever Y

http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <iostream> typedef long long ll; using namespace st

【EXT-BSGS算法求离散对数】POJ Clever Y 3243

Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7259 Accepted: 1795 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, given X,