結果
問題 | No.3048 Swing |
ユーザー |
![]() |
提出日時 | 2025-03-07 21:17:06 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 3,009 bytes |
コンパイル時間 | 1,017 ms |
コンパイル使用メモリ | 130,120 KB |
実行使用メモリ | 7,844 KB |
最終ジャッジ日時 | 2025-06-20 02:23:45 |
合計ジャッジ時間 | 2,585 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 62 |
ソースコード
#include <iostream> #include <iomanip> #include <cassert> #include <vector> #include <algorithm> #include <utility> #include <numeric> #include <cstdint> #include <cstddef> namespace zawa { using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using i128 = __int128_t; using u8 = std::uint8_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t; using usize = std::size_t; } // namespace zawa #include <cmath> #include <functional> #include <type_traits> namespace zawa { namespace internal { template <class T> T MidPoint(T a, T b) { if (a > b) std::swap(a, b); return a + ((b - a) >> 1); } template <class T> T Abs(T a, T b) { return (a >= b ? a - b : b - a); } } // namespace zawa::internal template <class T, class Function> T BinarySearch(T ok, T ng, const Function& f) { static_assert(std::is_integral_v<T>, "T must be integral type"); static_assert(std::is_convertible_v<Function, std::function<bool(T)>>, "f must be function bool(T)"); while (internal::Abs(ok, ng) > 1) { T mid{ internal::MidPoint(ok, ng) }; (f(mid) ? ok : ng) = mid; } return ok; } template <class T, class Function> T BinarySearch(T ok, T ng, const Function& f, u32 upperLimit) { static_assert(std::is_signed_v<T>, "T must be signed arithmetic type"); static_assert(std::is_convertible_v<Function, std::function<bool(T)>>, "f must be function bool(T)"); for (u32 _{} ; _ < upperLimit ; _++) { T mid{ (ok + ng) / (T)2 }; (f(mid) ? ok : ng) = mid; } return ok; } } // namespace zawa // #include "Src/Sequence/CompressedSequence.hpp" // #include "Src/Sequence/RunLengthEncoding.hpp" using namespace zawa; // #include "atcoder/modint" // using mint = atcoder::modint998244353; long long X, N; long long solve() { if (X > 0) { auto f = [&](long long k) -> bool { return X - k * (k + 1) / 2 <= 0LL; }; long long first = BinarySearch((long long)2e9, 0LL, f); if (N <= first) return X - N * (N + 1) / 2; X -= first * (first + 1) / 2; assert(X <= 0LL); long long n = std::max(0LL, N - first); if (n & 1) { return X + first + (n + 1) / 2; } else { return X - n / 2; } } else { auto f = [&](long long k) -> bool { return X + k * (k + 1) / 2 > 0LL; }; long long first = BinarySearch((long long)2e9, 0LL, f); // std::cout << first << std::endl; if (N <= first) return X + N * (N + 1) / 2; X += first * (first + 1) / 2; assert(X > 0LL); long long n = std::max(0LL, N - first); if (n & 1) { return X - first - (n + 1) / 2; } else { return X + n / 2; } } } int main() { std::cin.tie(nullptr); std::cout.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> X >> N; std::cout << solve() << '\n'; }