結果

問題 No.3048 Swing
ユーザー keisuke6
提出日時 2025-03-07 22:07:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 824 bytes
コンパイル時間 2,096 ms
コンパイル使用メモリ 193,796 KB
実行使用メモリ 8,612 KB
最終ジャッジ日時 2025-03-07 22:07:54
合計ジャッジ時間 3,922 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 42 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int x,n;
    cin>>x>>n;
    if(x >= 0){
    	if(n <= 2e9 && n*(n+1)/2 <= x){
    		cout<<x-n*(n+1)/2<<endl;
    		return 0;
    	}
    	int r = min((int)(2e9),n), l = 0;
    	while(r-l > 1){
    		int d = (l+r)/2;
    		if(d*(d+1)/2 > x) r = d;
    		else l = d;
    	}
    	int ans = x-(r*(r+1)/2);
    	ans -= (n-r)/2;
    	if((n-r)%2 == 1) ans += n;
    	cout<<ans<<endl;
    }
    else{
    	if(n <= 2e9 && n*(n+1)/2 <= -x){
    		cout<<x+n*(n+1)/2<<endl;
    		return 0;
    	}
    	int r = min((int)(2e9),n), l = 0;
    	while(r-l > 1){
    		int d = (l+r)/2;
    		if(d*(d+1)/2 > -x) r = d;
    		else l = d;
    	}
    	int ans = -x+(r*(r+1)/2);
    	ans += (n-r)/2;
    	if((n-r)%2 == 1) ans -= n;
    	cout<<ans<<endl;
    }
}
0