poj 2417 Discrete Logging (BSGS算法)

题目链接

题意:

Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that BL == N (mod P)

思路:bsgs算法

详情见BSGS算法笔记

然后被map的count坑了一下? 我想判断map中某个key是否存在,用count会TLE,find也会TLE,[]可以通过....不太懂,复杂度不都是log吗,差常数?还是有人会退化?

不过似乎[]比较安全就对了。

/* ***********************************************
Author :111qqz
Created Time :2017年07月23日 星期日 11时11分00秒
File Name :2417.cpp
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define PB push_back
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
LL p,b,n;
map<LL,LL>Hash;
map<LL,LL>::iterator it;
inline LL ksm(LL a,LL b,LL MOD)
{
    LL res = 1LL;
    while (b)
    {
	if (b&1) res = (res*a)%MOD;
	b = b >> 1;
	a = (a*a)%MOD;
    }
    return res;
}
LL BSGS(LL a,LL b ,LL p) // a^x = b (mod p),求x 
{
    a%=p;
    b%=p;
    if (!a&&!b) return 1;
    if (!a) return -1;
    Hash.clear();
    LL m = ceil(sqrt(double(p)));
    LL tmp = b;
    for (LL j = 0 ; j <= m ; j++)
    {
	Hash[tmp]=j;
	tmp = (tmp*a)%p;
    }
    tmp = ksm(a,m,p);
    LL ret = 1;

    for (LL i = 1  ; i <= m+1 ; i++)
    {
	ret = ret*tmp%p;
	if (Hash[ret]) return i*m-Hash[ret]; //注意处理下%....虽然其实不处理也没关系...
    }
    return -1;

}
int main()
{
        #ifndef  ONLINE_JUDGE 
       // freopen("./in.txt","r",stdin);
  #endif
	while (~scanf("%lld%lld%lld",&p,&b,&n))   // B^L = n(mod p)
	{
	    LL ans = BSGS(b,n,p);
	    if (ans==-1) printf("no solution\n");
	    else printf("%lld\n",(ans%p+p)%p);
	}
	    
    
  #ifndef ONLINE_JUDGE  
  fclose(stdin);
  #endif
    return 0;
}