結果
問題 | No.2648 [Cherry 6th Tune D] 一次元の馬 |
ユーザー |
|
提出日時 | 2024-02-23 22:13:23 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 243 ms / 2,000 ms |
コード長 | 8,246 bytes |
コンパイル時間 | 1,371 ms |
コンパイル使用メモリ | 136,752 KB |
最終ジャッジ日時 | 2025-02-19 20:00:49 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 36 |
ソースコード
#include <algorithm>#include <array>#include <bitset>#include <cassert>#include <chrono>#include <cmath>#include <complex>#include <cstdint>#include <cstring>#include <ctime>#include <deque>#include <iomanip>#include <iostream>#include <iterator>#include <map>#include <numeric>#include <queue>#include <random>#include <set>#include <stack>#include <unordered_map>#include <unordered_set>#include <vector>template <int mod>struct ModInt {int x;ModInt() : x(0) {}ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}ModInt &operator+=(const ModInt &p) {if ((x += p.x) >= mod) x -= mod;return *this;}ModInt &operator-=(const ModInt &p) {if ((x += mod - p.x) >= mod) x -= mod;return *this;}ModInt &operator*=(const ModInt &p) {x = (int)(1LL * x * p.x % mod);return *this;}ModInt &operator/=(const ModInt &p) {*this *= p.inverse();return *this;}ModInt &operator^=(long long p) { // quick_pow here:3ModInt res = 1;for (; p; p >>= 1) {if (p & 1) res *= *this;*this *= *this;}return *this = res;}ModInt operator-() const { return ModInt(-x); }ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }ModInt operator^(long long p) const { return ModInt(*this) ^= p; }bool operator==(const ModInt &p) const { return x == p.x; }bool operator!=(const ModInt &p) const { return x != p.x; }explicit operator int() const { return x; } // added by QCFiumModInt operator=(const int p) {x = p;return ModInt(*this);} // added by QCFiumModInt inverse() const {int a = x, b = mod, u = 1, v = 0, t;while (b > 0) {t = a / b;a -= t * b;std::swap(a, b);u -= t * v;std::swap(u, v);}return ModInt(u);}friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &p) {return os << p.x;}friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {long long x;is >> x;a = ModInt<mod>(x);return (is);}};using mint = ModInt<998244353>;const int MOD = 998244353;struct MComb {std::vector<mint> fact;std::vector<mint> inversed;MComb(int n) { // O(n+log(mod))fact = std::vector<mint>(n + 1, 1);for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * mint(i);inversed = std::vector<mint>(n + 1);inversed[n] = fact[n] ^ (MOD - 2);for (int i = n - 1; i >= 0; i--)inversed[i] = inversed[i + 1] * mint(i + 1);}mint ncr(int n, int r) {if (n < r) return 0;return (fact[n] * inversed[r] * inversed[n - r]);}mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }mint nhr(int n, int r) {assert(n + r - 1 < (int)fact.size());return ncr(n + r - 1, r);}};mint ncr(int n, int r) {mint res = 1;for (int i = n - r + 1; i <= n; i++) res *= i;for (int i = 1; i <= r; i++) res /= i;return res;}int noinit = 1;long long memo[2005][2005];long long nCr(long long n, long long r) {if (noinit) {for (int i = 0; i < 2005; i++) {for (int j = 0; j < 2005; j++) {memo[i][j] = -1;noinit = 0;}}}if (r == 0 || n == r) return 1;if (0 <= memo[n][r]) return memo[n][r];return memo[n][r] = nCr(n - 1, r - 1) + nCr(n - 1, r);}/*mint res = (mint(2) ^ n) - 1 - ncr(n, a) - ncr(n, b);std::cout << res << std::endl;*/std::vector<bool> prime_table(int n) {std::vector<bool> prime(n + 1, true);if (n >= 0) prime[0] = false;if (n >= 1) prime[1] = false;for (int i = 2; i * i <= n; i++) {if (!prime[i]) continue;for (int j = i * i; j <= n; j += i) {prime[j] = false;}}return prime;}std::vector<int> enumerate_primes(int n) {if (n <= 1) return {};auto d = prime_table(n);std::vector<int> primes;primes.reserve(count(begin(d), end(d), true));for (int i = 0; i < d.size(); i++) {if (d[i]) primes.push_back(i);}return primes;}template <typename T>std::vector<T> get_divisors(T x, bool sorted = true) {std::vector<T> res;for (T i = 1; i <= x / i; i++)if (x % i == 0) {res.push_back(i);if (i != x / i) res.push_back(x / i);}if (sorted) std::sort(res.begin(), res.end());return res;}template <class Type>class WeightedUnionFind {public:WeightedUnionFind() = default;/// @brief 重み付き Union-Find 木を構築します。/// @param n 要素数explicit WeightedUnionFind(size_t n): m_parentsOrSize(n, -1), m_diffWeights(n) {}/// @brief 頂点 i の root のインデックスを返します。/// @param i 調べる頂点のインデックス/// @return 頂点 i の root のインデックスint find(int i) {if (m_parentsOrSize[i] < 0) {return i;}const int root = find(m_parentsOrSize[i]);m_diffWeights[i] += m_diffWeights[m_parentsOrSize[i]];// 経路圧縮return (m_parentsOrSize[i] = root);}/// @brief a のグループと b のグループを統合します。/// @param a 一方のインデックス/// @param b 他方のインデックス/// @param w (b の重み) - (a の重み)void merge(int a, int b, Type w) {w += weight(a);w -= weight(b);a = find(a);b = find(b);if (a != b) {// union by size (小さいほうが子になる)if (-m_parentsOrSize[a] < -m_parentsOrSize[b]) {std::swap(a, b);w = -w;}m_parentsOrSize[a] += m_parentsOrSize[b];m_parentsOrSize[b] = a;m_diffWeights[b] = w;}}/// @brief (b の重み) - (a の重み) を返します。/// @param a 一方のインデックス/// @param b 他方のインデックス/// @remark a と b が同じグループに属さない場合の結果は不定です。/// @return (b の重み) - (a の重み)Type diff(int a, int b) { return (weight(b) - weight(a)); }/// @brief a と b が同じグループに属すかを返します。/// @param a 一方のインデックス/// @param b 他方のインデックス/// @return a と b が同じグループに属す場合 true, それ以外の場合は falsebool connected(int a, int b) { return (find(a) == find(b)); }/// @brief i が属するグループの要素数を返します。/// @param i インデックス/// @return i が属するグループの要素数int size(int i) { return -m_parentsOrSize[find(i)]; }private:// m_parentsOrSize[i] は i の 親,// ただし root の場合は (-1 * そのグループに属する要素数)std::vector<int> m_parentsOrSize;// 重みstd::vector<Type> m_diffWeights;Type weight(int i) {find(i); // 経路圧縮return m_diffWeights[i];}};template <typename T>struct DSU {std::vector<T> f, siz;DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); }T leader(T x) {while (x != f[x]) x = f[x] = f[f[x]];return x;}bool same(T x, T y) { return leader(x) == leader(y); }bool merge(T x, T y) {x = leader(x);y = leader(y);if (x == y) return false;siz[x] += siz[y];f[y] = x;return true;}T size(int x) { return siz[leader(x)]; }};void solve() {int n;std::cin >> n;std::vector<long long> a(n);for (int i = 0; i < n; i++) std::cin >> a[i];long long l = 0, r = 1e18 + 5e17;auto check = [&](long long step) {std::vector<__int128_t> b(n);for (int i = 0; i < n; i++) b[i] = a[i];for (int i = 0; i < n; i++) {b[i] = b[i] + (__int128_t)step * (i + 1);if (i) {if (b[i - 1] >= b[i]) return false;}}return true;};while (l < r) {long long mid = l + (r - l) / 2;if (check(mid))r = mid;elsel = mid + 1;}if (l == 1e18 + 5e17) l = -1;std::cout << l << '\n';}int main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);int t = 1;std::cin >> t;while (t--) {solve();}}