QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#661583 | #5417. Chat Program | ShwStone | WA | 0ms | 3776kb | C++14 | 4.2kb | 2024-10-20 16:56:39 | 2024-10-20 16:56:39 |
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];
unordered_map<ll, int> p;
unordered_map<ll, ll> mp, tmp;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll m_p(int a, int b) {
return a * (1ll << 32) + 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 calclr(int l, int r) {
for (int i = l; i <= r; i++) a[i] += k;
ll res = a[1];
for (int i = 2; i <= n; i++) res = gcd(res, a[i]);
for (int i = l; i <= r; 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[m_p(-1, -1)] = max(tmp[m_p(-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[m_p(l, r)] = max(tmp[m_p(l, r)], v);
} else {
tmp[m_p(0, 0)] = max(tmp[m_p(0, 0)], v);
}
}
v *= tv;
}
}
ll ry = tmp.count(m_p(-1, -1)) ? tmp[m_p(-1, -1)] : 1;
for (auto x : tmp) {
if (x.first != m_p(-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(m_p(-1, -1)) ? mp[m_p(-1, -1)] : 1;
ll res = 1;
for (auto &x : mp) {
if (x.first != m_p(-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(m_p(-1, -1)) ? mp[m_p(-1, -1)] : 1;
ll res = 1;
for (auto &x : mp) {
if (x.first != m_p(-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);
}
if (n <= 500) {
ll ans = 1;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
ans = max(ans, calclr(i, j));
}
}
printf("%lld\n", ans);
} else {
ll ans = max({calclr(1, n), calc1(), calcn()});
printf("%lld\n", ans);
}
}
int main() {
int _;
scanf("%d", &_);
while (_--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3776kb
input:
6 4 3 1 2 1 1 4 5 1 4
output:
1 3 3 3 3 3
result:
wrong answer 1st numbers differ - expected: '4', found: '1'