萌新求助月赛 C
  • 板块学术版
  • 楼主Cadmus
  • 当前回复8
  • 已保存回复8
  • 发布时间2023/7/22 19:01
  • 上次更新2023/11/3 08:12:56
查看原帖
萌新求助月赛 C
858406
Cadmus楼主2023/7/22 19:01

问什么这份 Pollard's Rho 挂了,是常数太大还是写假了qwq

测评记录:https://www.luogu.com.cn/record/116912269

#include <bits/stdc++.h>
using namespace std;
namespace Cadmus {
    typedef long long LL;
    const int N = 1e6 + 5;
    namespace Factor {
        const LL Testers[] = {0, 2, 3, 7, 11, 13, 17, 19};
		mt19937 rnd(time(NULL));
		LL mul(LL a, LL b, LL mod) {
		    LL res = 0;
		    for (a %= mod; b; a = (a << 1) % mod, b >>= 1)
		        if (b & 1) res = (res + a) % mod;
		    return res; 
		}
		LL qpow(LL bas, LL k, LL mod) {
		    LL ans = 1;
		    for (bas %= mod; k; bas = mul(bas, bas, mod), k >>= 1)
		        if (k & 1) ans = mul(ans, bas, mod);
		    return ans;
		}
		bool MillerRabin(LL p) {
		    if (p == 1) return false;
		    LL t = p - 1, k = 0;
		    while (!(t & 1)) t >>= 1, k ++;
		    for (int i = 1; i <= 7; i ++) {
		        if (p == Testers[i]) return true;
		        LL a = qpow(Testers[i], t, p), sqr = 0;
		        for (int j = 1; j <= k; j ++) {
		            sqr = mul(a, a, p);
		            if (sqr == 1 && a != 1 && a != p - 1)
		                return false;
		            a = sqr;
		        }
		        if (a != 1) return false;
		    }
		    return true;
		}
		LL floyd(LL n) {
		    LL seed = rnd() % n, s, t;
		    auto f = [&](LL x) { return (mul(x, x, n) + seed) % n; };
		    s = t = rnd() % n, t = f(t);
		    while (s != t) {
		        LL d = __gcd(abs(s - t + n), n);
		        if (d != 1) return d;
		        s = f(s), t = f(f(t));
		    } 
		    return 1;
		}
		void PollardRho(LL n, unordered_set<LL>& res) {
		    if (n == 1) return;
		    if (MillerRabin(n)) { res.insert(n); return; }
		    LL d = 1;
		    while (d == 1) d = floyd(n);
		    PollardRho(d, res), PollardRho(n / d, res); 
		}
    }
    LL n, a[N]; bool success;
    unordered_set<LL> S, qwq;
    int main() {
        cin >> n, S.clear();
        for (int i = 1; i <= n; i ++) cin >> a[i];
        if (n == 2) return puts("Yes"), 0;
        for (int i = 1; i <= n; i ++) {
			qwq.clear(), Factor::PollardRho(a[i], qwq);
           	for (LL v : qwq) {
           		if (S.count(v)) return puts("No"), 0;
           		S.insert(v);
			}
        }
        puts("Yes");
        return 0;
    }
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
    int T = 1; cin >> T;
    while (T --) Cadmus::main();
    return 0;
}
2023/7/22 19:01
加载中...