玄学RE求助
查看原帖
玄学RE求助
153123
AK_Dream楼主2020/6/14 23:23

三个打注释的地方,如果把上面两行换成注释那行就能AC,不然就会RE。。。感觉应该是哪里除了0或者模了0才RE,但是没找到啊

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll ttt, n, ans;

inline ll fmul(ll x, ll y, ll p) {
	return (x * y - (ll)((long double)x / p * y) * p + p) % p;
}

inline ll fpow(ll x, ll t, ll p) {
	ll ret = 1;
	for (; t; t >>= 1, x = fmul(x, x, p)) if (t & 1) ret = fmul(ret, x, p);
	return ret;
}

inline bool MR(ll x, ll p) {
	if (fpow(x, p - 1, p) != 1) return 0;
	ll k = p - 1;
	while (!(k & 1)) {
		k >>= 1;
		ll now = fpow(x, k, p);
		if (now != 1 && now != p - 1) return 0;
		else if (now == p - 1) return 1;
	}
	return 1;
}

inline bool Test(ll p) {
	if (p == 1 || p == 2152302898747ll) return 0;
	if (p == 2 || p == 3 || p == 5 || p == 7 || p == 11 || p == 13) return 1;
	return MR(2, p) && MR(3, p) && MR(5, p) && MR(7, p) && MR(11, p) && MR(13, p) && MR(61, p);
} 

inline ll rnd() {
	return ((1ll * rand()) << 15) + rand();
}

void PR(ll x) {
	if (x <= ans) return;
	if (Test(x)) {
		ans = max(ans, x);
		return;
	}	
	ll t1 = rand() % (x - 1) + 1, b = rnd() % (x - 1) + 1;
	ll t2 = (fmul(t1, t1, x) + b) % x;
	ll now = 1, i = 0;
	while (t1 != t2) {
		i++;
		now = fmul(now, abs(t1 - t2), x);
		if (now == 0) {
			now = __gcd(abs(t1 - t2), x);
			if (now > 1 && now < x) {
				while (x % now == 0) x /= now;
				PR(now); PR(x);
				// PR(now); PR(x / now);
			}
			return;
		}
		if (i % 127 == 0) {
			now = __gcd(now, x);
			if (now > 1 && now < x) {
				while (x % now == 0) x /= now;
				PR(now); PR(x);
				// PR(now); PR(x / now);
				return;
			}
			now = 1; 
		}
		t1 = (fmul(t1, t1, x) + b) % x;
		t2 = (fmul(t2, t2, x) + b) % x;
		t2 = (fmul(t2, t2, x) + b) % x;
	}
	now = __gcd(now, x);
	if (now > 1 && now < x) {
		while (x % now == 0) x /= now;
		PR(now); PR(x);
		// PR(now); PR(x / now);
		return;
	}
}

int main() {
	srand(time(NULL));
	scanf("%lld", &ttt);
	while (ttt--) {
		scanf("%lld", &n);
		if (Test(n)) {
			puts("Prime");
		} else {
			ans = 0;
			while (!ans) PR(n);
			printf("%lld\n", ans);
		}
	}
	return 0;
} 
2020/6/14 23:23
加载中...