QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#661584#9426. Relearn through ReviewShwStoneTL 253ms4120kbC++144.2kb2024-10-20 16:57:032024-10-20 16:57:04

Judging History

你现在查看的是最新测评结果

  • [2024-10-20 16:57:04]
  • 评测
  • 测评结果:TL
  • 用时:253ms
  • 内存:4120kb
  • [2024-10-20 16:57:03]
  • 提交

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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 4120kb

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: 0
Accepted
time: 253ms
memory: 3908kb

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
657035115675878115
182894211060271407
880411769063535667
560553564512176618
183698346865682381
962990836390050009
616597869896951268
878097339332572161
188820994675344528
997057718507559252
949074379610491450
37337367838628559
632093288650732211
3771217139073309...

result:

ok 100000 lines

Test #3:

score: -100
Time Limit Exceeded

input:

1000
71 451750502977198411
701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 7015137...

output:


result: