扩展BSGS-传送门

很好的讲解:ZigZagK
好的讲解mjtcn
某个模板:here

模板题:
BSGS:ZigZagK的poj2417

exBSGS:ZigZagK的poj3243AC_Gibson

一般的板子过不了这个题!GYM101853Ghere


exLucas:here

代码:https://ideone.com/ixi3rw

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mme(a,b) memset(a,b,sizeof(a));
typedef long long LL;
const long long mod=998244353;
const int maxn=5e5+5;
const int INF=0x7fffffff;

inline LL gcd(LL a, LL b) {
    return (!b) ? a : gcd(b, a % b);
}
inline void Exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
    if (!b) {
        d = a, x = 1, y = 0;
    } else {
        Exgcd(b, a % b, d, y, x), y -= x * (a / b);
    }
}
inline LL Solve(LL a, LL b, LL c) {// ax%c=b S.T. (a,c)=1
    LL d, x, y;
    Exgcd(a, c, d, x, y);
    x = (x + c) % c;
    return x * b % c;
}
inline LL Ksm(LL x, LL y, LL p) {
    LL res = 1, t = x;
    for(; y; y >>= 1) {
        if (y & 1) res = res * t % p;
        t = t * t % p;
    }
    return res;
}
#define mod 1313131
struct Hashset {
    LL head[mod], next[maxn], f[maxn], v[maxn], ind;
    void reset() {
        ind = 0;
        memset(head, -1, sizeof head);
    }
    void Insert(LL x, LL _v) {
        LL ins = x % mod;
        for(LL j = head[ins]; j != -1; j = next[j])
            if (f[j] == x) {
                v[j] = min(v[j], _v);
                return;
            }
        f[ind] = x, v[ind] = _v;
        next[ind] = head[ins], head[ins] = ind++;
    }
    LL operator [] (const LL &x) const {
        LL ins = x % mod;
        for(LL j = head[ins]; j != -1; j = next[j])
            if (f[j] == x)
                return v[j];
        return -1;
    }
} S;
LL BSGS(LL C, LL A, LL B, LL p) {// A^x%p=B S.T.(A,p)=1
    if (p <= 100) {
        LL d = 1;
        for(int i = 0; i < p; ++i) {
            if (d == B)
                return i;
            d = d * A % p;
        }
        return -1;
    } else {
        LL m = (int)sqrt(p);
        S.reset();
        LL d = 1, Search;
        for(int i = 0; i < m; ++i) {
            S.Insert(d, i);
            d = d * A % p;
        }
        for(int i = 0; i * m < p; ++i) {
            d = Ksm(A, i * m, p) * C % p;
            Search = S[Solve(d, B, p)];
            if (Search != -1)
                return i * m + Search;
        }
        return -1;
    }
}
int main() {
    LL x, z, k;
    register LL i, j;
    int t;
    scanf("%d",&t);
    while(t--) {
      scanf("%I64d%I64d%I64d", &x, &k, &z);
      LL d = 1;
      bool find = 0;
      for(i = 0; i < 100; ++i) {
        if (d == k) {
          printf("%I64d\n", i);
          find = 1;
          break;
        }
        d = d * x % z;
      }
      if (find)continue;
      LL t, C = 1, num = 0;
      bool failed = 0;
      while((t = gcd(x, z)) != 1) {
        z /= t;
        k /= t;
        C = C * x / t % z;
        ++num;
      }
      LL res = BSGS(C, x, k, z);
      if (res == -1)puts("No Solution");
      else printf("%I64d\n", res + num);
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值