QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#661516 | #9426. Relearn through Review | ShwStone | TL | 0ms | 3804kb | C++14 | 4.0kb | 2024-10-20 16:37:32 | 2024-10-20 16:37:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN(1e5 + 5);
using ll = long long;
using ull = unsigned long long;
int n;
ll k;
ll a[MAXN];
map<ll, int> p;
map<pair<int, int>, ll> mp, tmp;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll bmul(ll a, ll b, ll m) { // 快速乘
ull c = (ull)a * (ull)b - (ull)((long double)a / m * b + 0.5L) * (ull)m;
if (c < (ull)m) return c;
return c + m;
}
ll qpow(ll x, ll p, ll mod) { // 快速幂
ll ans = 1;
while (p) {
if (p & 1) ans = bmul(ans, x, mod);
x = bmul(x, x, mod);
p >>= 1;
}
return ans;
}
bool Miller_Rabin(ll p) { // 判断素数
if (p < 2) return false;
if (p == 2) return true;
if (p == 3) return true;
ll d = p - 1, r = 0;
while (!(d & 1)) ++r, d >>= 1; // 将d处理为奇数
for (ll k = 0; k < 10; ++k) {
ll a = rand() % (p - 2) + 2;
ll x = qpow(a, d, p);
if (x == 1 || x == p - 1) continue;
for (int i = 0; i < r - 1; ++i) {
x = bmul(x, x, p);
if (x == p - 1) break;
}
if (x != p - 1) return false;
}
return true;
}
ll Pollard_Rho(ll x) {
ll s = 0, t = 0;
ll c = (ll)rand() % (x - 1) + 1;
int step = 0, goal = 1;
ll val = 1;
for (goal = 1;; goal *= 2, s = t, val = 1) { // 倍增优化
for (step = 1; step <= goal; ++step) {
t = (bmul(t, t, x) + c) % x;
val = bmul(val, abs(t - s), x);
if ((step % 127) == 0) {
ll d = gcd(val, x);
if (d > 1) return d;
}
}
ll d = gcd(val, x);
if (d > 1) return d;
}
}
ll calc1n() {
for (int i = 1; i <= n; i++) a[i] += k;
ll res = a[1];
for (int i = 2; i <= n; i++) res = gcd(res, a[i]);
for (int i = 1; i <= n; i++) a[i] -= k;
return res;
}
void calcP(ll v, int cc) {
if (v == 1) return;
if (Miller_Rabin(v)) {
p[v] += cc;
return;
}
ll t = v;
while (t >= v) t = Pollard_Rho(v);
int tc = 0;
while (v % t == 0) v /= t, tc++;
calcP(t, cc * tc);
calcP(v, cc);
}
void calc(int cnt, ll v) {
tmp.clear();
ll tv = v;
while (cnt--) {
if (k % v == 0) {
bool flag = true;
for (int i = 1; i <= n; i++) {
if (a[i] % v != 0) {
flag = false;
break;
}
}
if (flag) {
tmp[make_pair(-1, -1)] = max(tmp[make_pair(-1, -1)], v);
}
} else {
int l = 0, r = 0;
bool flag = true;
for (int i = 1; i <= n; i++) {
if (a[i] % v != 0) {
if ((a[i] + k) % v != 0) {
flag = false;
break;
}
if (!l) l = i;
else if (r) {
flag = false;
break;
}
} else {
if (l && !r) r = i - 1;
}
}
if (flag) {
if (l) {
if (!r) r = n;
tmp[make_pair(l, r)] = max(tmp[make_pair(l, r)], v);
} else {
tmp[make_pair(0, 0)] = max(tmp[make_pair(0, 0)], v);
}
}
v *= tv;
}
}
ll ry = tmp.count(make_pair(-1, -1)) ? tmp[make_pair(-1, -1)] : 1;
for (auto x : tmp) {
if (x.first != make_pair(-1, -1)) {
x.second /= ry;
}
if (mp.count(x.first)) {
mp[x.first] *= x.second;
} else {
mp[x.first] = x.second;
}
}
}
ll calc1() {
p.clear();
calcP(a[1], 1);
mp.clear();
for (auto x : p) {
calc(x.second, x.first);
}
ll ry = mp.count(make_pair(-1, -1)) ? mp[make_pair(-1, -1)] : 1;
ll res = 1;
for (auto &x : mp) {
if (x.first != make_pair(-1, -1)) x.second *= ry;
res = max(res, x.second);
}
return res;
}
ll calcn() {
p.clear();
calcP(a[n], 1);
mp.clear();
for (auto x : p) {
calc(x.second, x.first);
}
ll ry = mp.count(make_pair(-1, -1)) ? mp[make_pair(-1, -1)] : 1;
ll res = 1;
for (auto &x : mp) {
if (x.first != make_pair(-1, -1)) x.second *= ry;
res = max(res, x.second);
}
return res;
}
void solve() {
scanf("%d %lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%lld", a + i);
}
ll ans = max({calc1n(), calc1(), calcn()});
printf("%lld\n", ans);
}
int main() {
int _;
scanf("%d", &_);
while (_--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3804kb
input:
2 6 2 5 3 13 8 10 555 3 0 3 6 9
output:
5 3
result:
ok 2 lines
Test #2:
score: -100
Time Limit Exceeded
input:
100000 1 608611451460421713 33155506392034032 1 743116173559300609 6138108577573005 7 364454564010802125 657035115675878115 657035115675878115 657035115675878115 657035115675878115 657035115675878115 292580551665075990 657035115675878115 4 316648374341335221 365788422120542814 182894211060271407 731...
output:
641766957852455745 749254282136873614 219011705225292705 20321579006696823 880411769063535667 560553564512176618 183698346865682381 962990836390050009 616597869896951268 878097339332572161 188820994675344528 997057718507559252 949074379610491450 37337367838628559 632093288650732211 37712171390733092...