結果

問題 No.3048 Swing
ユーザー warner1129
提出日時 2025-03-07 21:13:24
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,245 bytes
コンパイル時間 2,844 ms
コンパイル使用メモリ 278,872 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-20 02:23:34
合計ジャッジ時間 4,151 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 62
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template<class F, class S>
ostream &operator<<(ostream &s, const pair<F, S> &v) {
    s << "(" << v.first << ", " << v.second << ")";
    return s;
}
template<ranges::range T> requires (!is_convertible_v<T, string_view>)
istream &operator>>(istream &s, T &&v) { 
    for (auto &&x : v) s >> x; 
    return s; 
}
template<ranges::range T> requires (!is_convertible_v<T, string_view>)
ostream &operator<<(ostream &s, T &&v) { 
    for (auto &&x : v) s << x << ' '; 
    return s; 
}

#ifdef LOCAL
template<class... T> void dbg(T... x) {
    char e{};
    ((cerr << e << x, e = ' '), ...);
}
#define debug(x...) dbg(#x, '=', x, '\n')
#else
#define debug(...) ((void)0)
#endif

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second

template<class T> inline constexpr T inf = numeric_limits<T>::max() / 2;
bool chmin(auto &a, auto b) { return (b < a and (a = b, true)); }
bool chmax(auto &a, auto b) { return (a < b and (a = b, true)); }

using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;

constexpr i64 mod = 998244353;

void solve() {
    i64 t, n;
    cin >> t >> n;

    i64 p = *ranges::partition_point(
        views::iota(0LL, n),
        [&](i64 k) {
            return (i128)k * (k + 1) / 2 <= -t;
        }
    );
    t += p * (p + 1) / 2;

    if (p < n) {
        i64 q = *ranges::partition_point(
            views::iota(p + 1, n),
            [&](i64 k) {
                return (i128)k * (k + 1) / 2 - (i128)p * (p + 1) / 2 < t;
            }
        );
        t -= q * (q + 1) / 2 - (i128)p * (p + 1) / 2;
        p = q;
    }

    i128 x = t;
    if (p + 1 <= n) {
        i64 l = p + 1;
        i64 r = (n - p - 1) / 2 * 2 + p + 1;
        x += i128(l + r) * (r - l + 2) / 2 / 2;
    }
    if (p + 2 <= n) {
        i64 l = p + 2;
        i64 r = (n - p - 2) / 2 * 2 + p + 2;
        x -= i128(l + r) * (r - l + 2) / 2 / 2;
    }

    cout << (i64)x << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}
0