結果
問題 | No.3048 Swing |
ユーザー |
|
提出日時 | 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 |
コンパイル時間 | 6,611 ms |
コンパイル使用メモリ | 333,952 KB |
実行使用メモリ | 7,324 KB |
最終ジャッジ日時 | 2025-03-14 17:00:29 |
合計ジャッジ時間 | 8,344 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 59 |
ソースコード
#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);}