結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint = atcoder::static_modint<998244353>;
// using mint = atcoder::static_modint<1000000007>;
using ld = long double;
using ll = long long;
#define mp(a,b) make_pair(a,b)
#define rep(i,s,n) for(int i=s; i<(int)n; i++)
const vector<int> dx{1,0,-1,0},dy{0,1,0,-1};


ll solve_posi(__int128_t x,__int128_t n){
    if(n*(n+1)/2<=x){
        return x-(n+1)*n/2;
    }

    __int128_t L=0,R=n;
    while(R-L>1){
        __int128_t mid=(L+R)/2;
        if(x-mid*(mid+1)/2<=0)R=mid;
        else L=mid;
    }
    // cout << (ll)L << " " << (ll)R;

    __int128_t x_R=x-R*(R+1)/2;
    if((n-R)%2==0){
        __int128_t ans=x_R-(n-R)/2;
        return ans;
    }
    else{
        __int128_t ans=x_R+R+1+(n-R)/2;
        return ans;
    }
}

ll solve_nega(__int128_t x,__int128_t n){
    
    if(x+n*(n+1)/2<=0){
        return x+(n+1)*n/2;
    }

    __int128_t L=0,R=n;
    while(R-L>1){
        __int128_t mid=(L+R)/2;
        if(x+mid*(mid+1)/2>0)R=mid;
        else L=mid;
    }
    // cout << (ll)L << " " << (ll)R;

    __int128_t x_R=x+R*(R+1)/2;
    if((n-R)%2==0){
        __int128_t ans=x_R+(n-R)/2;
        return ans;
    }
    else{
        __int128_t ans=x_R-R-1-(n-R)/2;
        return ans;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll x,n;cin >> x >> n;
    if(x==0){
        if(n%2==0)cout << -n/2;
        else cout << (n+1)/2;
    }
    else if(x>0)cout << solve_posi(x,n);
    else cout << solve_nega(x,n);
}
0