问关于 Miller_Rabin
  • 板块学术版
  • 楼主__er
  • 当前回复4
  • 已保存回复4
  • 发布时间2023/6/5 19:03
  • 上次更新2023/10/23 13:53:42
查看原帖
问关于 Miller_Rabin
713955
__er楼主2023/6/5 19:03

这个 Miller_Rabin 函数错在哪里:

bool Miller_Rabin(int p) {
    if (p < 2) return false;
    if (p == 2 || p == 3)return true;
    int d = p - 1, r = 0;
    while (!(d & 1))r++, d >>= 1;
    for (int k = 1; k <= 12; k++) {
        if (basenum[k] == p) return true;
        int x = qpow(basenum[k], d, p), y = x;
        for (int i = 1; i <= r; i++) {
            y = (lllong)x * x % p;
            if (y == 1 && x != 1 && x == p - 1)return false;
            x = y;
        }
        if (x != 1) return false;
    }
    return true;
}

其中 lllong 宏定义为 __int128 其余 int 全部宏定义为 long long

其中 qpow 快速幂如下:

int qpow(int x, int p, int mod) {
    int ans = 1;
    while (p) {
        if (p & 1) ans = (lllong)ans * x % mod;
        x = (lllong)x * x % mod, p >>= 1;
    }
    return ans;
}

basenum[]:

const int basenum[] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

会 T 掉

2023/6/5 19:03
加载中...