結果

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

ソースコード

diff #

#include <iostream>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
typedef boost::multiprecision::cpp_int bint;
const long long linf = 4610000000000000000;
int main()
{
  bint x, n;
  cin >> x >> n;
  if (0 < x)
  {
    bint l = 0, r = n+1;
    while (abs(l - r) > 1)
    {
      bint c = (l + r) / 2;
      if (x - c * (c+1) / 2 > 0)
      {
        l = c;
      }
      else
      {
        r = c;
      }
    }
    if (n <= l)
    {
      cout << x - n * (n+1) / 2 << '\n';
    }
    else
    {
      bint ans = x - l * (l+1) / 2;
      ans += (n - l) / 2;
      if ((n - l) % 2 == 1)
      {
        ans -= n;
      }
      cout << ans << '\n';
    }
  }
  else
  {
    bint l = 0, r = n+1;
    while (abs(l - r) > 1)
    {
      bint c = (l + r) / 2;
      if (x + c * (c+1) / 2 <= 0)
      {
        l = c;
      }
      else
      {
        r = c;
      }
    }
    if (n <= l)
    {
      cout << x + n * (n+1) / 2 << '\n';
    }
    else
    {
      bint ans = x + l * (l+1) / 2;
      ans -= (n - l) / 2;
      if ((n - l) % 2 == 1)
      {
        ans += n;
      }
      cout << ans << '\n';
    }
  }
  
  return 0;
}
/*
  File : ~/yukicoder/526/A.cpp
  Date : 2025/03/07
  Time : 21:02:05
*/
0