結果

問題 No.2452 Incline
ユーザー Focus_SashFocus_Sash
提出日時 2023-09-01 21:57:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 5,391 bytes
コンパイル時間 2,030 ms
コンパイル使用メモリ 199,952 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-01 21:58:10
合計ジャッジ時間 3,810 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 101 ms
4,376 KB
testcase_04 AC 110 ms
4,380 KB
testcase_05 AC 101 ms
4,376 KB
testcase_06 AC 89 ms
4,376 KB
testcase_07 AC 97 ms
4,376 KB
testcase_08 AC 85 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
// #define ONLINE_JUDGE

#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define reps(i, k, n) for (ll(i) = (k); (i) < (n); ++(i))
#define repsi(i, k, n) for (ll(i) = (k); (i) <= (n); ++(i))
#define dreps(i, k, n) for (ll(i) = (k); (i) >= (n); --(i))

namespace util {
using ll = long long;
using vl = std::vector<long long>;
using pl = std::pair<long long, long long>;

constexpr long long kInf = std::numeric_limits<long long>::max() / 8;
constexpr long long kMax = std::numeric_limits<long long>::max();

template <typename T, typename U>
inline bool UpdateMax(T &x, const U &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T, typename U>
inline bool UpdateMin(T &x, const U &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

// verified
inline long long Pow(long long x, long long n) {
  assert(n >= 0);
  if (x == 0) return 0;
  long long res = 1LL;
  while (n > 0) {
    if (n & 1) {
      assert(x != 0 && std::abs(res) <= kMax / std::abs(x));
      res = res * x;
    }
    if (n >>= 1) {
      assert(x != 0 && std::abs(x) <= kMax / std::abs(x));
      x = x * x;
    }
  }
  return res;
}

// verified
inline long long Mod(long long n, const long long m) {
  // returns the "arithmetic modulo"
  // for a pair of integers (n, m) with m != 0, there exists a unique pair of
  // integer (q, r) s.t. n = qm + r and 0 <= r < |m| returns this r
  assert(m != 0);
  if (m < 0) return Mod(n, -m);
  if (n >= 0)
    return n % m;
  else
    return (m + n % m) % m;
}

inline long long Quotient(long long n, long long m) {
  // returns the "arithmetic quotient"
  assert((n - Mod(n, m)) % m == 0);
  return (n - Mod(n, m)) / m;
}

inline long long DivFloor(long long n, long long m) {
  // returns floor(n / m)
  assert(m != 0);
  if (m < 0) {
    n = -n;
    m = -m;
  }
  if (n >= 0)
    return n / m;
  else if (n % m == 0)
    return -(abs(n) / m);
  else
    return -(abs(n) / m) - 1;
}

inline long long DivCeil(long long n, long long m) {
  // returns ceil(n / m)
  assert(m != 0);
  if (n % m == 0)
    return DivFloor(n, m);
  else
    return DivFloor(n, m) + 1;
}

template <typename T>
inline T Sum(const std::vector<T> &vec) {
  return std::accumulate(vec.begin(), vec.end(), T(0));
}
inline long long Max(const std::vector<long long> &v) {
  return *std::max_element(v.begin(), v.end());
}
inline long long Min(const std::vector<long long> &v) {
  return *std::min_element(v.begin(), v.end());
}
template <typename T, typename F>
bool Exists(const std::vector<T> &v, F &&f) {
  return std::any_of(v.begin(), v.end(), f);
}
template <typename T, typename F>
bool ForAll(const std::vector<T> &v, F &&f) {
  return std::all_of(v.begin(), v.end(), f);
}
class Sorted {
 private:
  const std::vector<long long> &vec_;

 public:
  Sorted(const std::vector<long long> &vec) : vec_(vec) {}

  long long CountInRange(long long begin, long long end) {
    return std::lower_bound(vec_.begin(), vec_.end(), end) -
           std::lower_bound(vec_.begin(), vec_.end(), begin);
  }

  long long CountSmaller(long long x) {
    return std::lower_bound(vec_.begin(), vec_.end(), x) - vec_.begin();
  }

  long long CountLarger(long long x) {
    return vec_.end() - std::upper_bound(vec_.begin(), vec_.end(), x);
  }

  long long CountFrom(long long x) {
    return vec_.end() - std::lower_bound(vec_.begin(), vec_.end(), x);
  }

  long long CountTo(long long x) {
    return std::upper_bound(vec_.begin(), vec_.end(), x) - vec_.begin();
  }
};
inline long long PowMod(long long x, long long n, const long long m) {
  assert(n >= 0);
  assert(m != 0);
  if (x == 0) return 0;
  long long res = 1;
  x = Mod(x, m);
  while (n > 0) {
    if (n & 1) {
      assert(x == 0 || std::abs(res) <= kMax / std::abs(x));
      res = Mod(res * x, m);
    }
    if (n >>= 1) {
      assert(x == 0 || std::abs(x) <= kMax / std::abs(x));
      x = Mod(x * x, m);
    }
  }
  return res;
}
void Print(std::string s) { cout << s << '\n'; }
void Print(long long x) { cout << x << '\n'; }
template <typename T>
void Print(std::vector<T> v) {
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] << " \n"[i == v.size() - 1];
  }
}
}  // namespace util
using namespace util;

using i128 = __int128;
std::istream &operator>>(std::istream &is, i128 &m) {
  long long a;
  is >> a;
  m = a;
  return is;
}
std::ostream &operator<<(std::ostream &os, const i128 &m) {
  os << (long long)m;
  return os;
}

i128 floor_sum(i128 n, i128 m, i128 a, i128 b) {
  i128 ans = 0;
  if (a >= m) {
    ans += (n - 1) * n * (a / m) / 2;
    a %= m;
  }
  if (b >= m) {
    ans += n * (b / m);
    b %= m;
  }

  i128 y_max = (a * n + b) / m, x_max = (y_max * m - b);
  if (y_max == 0) return ans;
  ans += (n - (x_max + a - 1) / a) * y_max;
  ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
  return ans;
}

constexpr i128 MOD = 998244353;

void solve() {
  ll t;
  cin >> t;
  while (t--) {
    i128 n, m, l, r;
    cin >> n >> m >> l >> r;
    i128 pos = floor_sum(r + 1, n - 1, 1, 0) - floor_sum(l, n - 1, 1, 0);
    i128 neg =
        floor_sum(m - l + 1, n - 1, 1, 0) - floor_sum(m - r, n - 1, 1, 0);
    Print((pos + neg + (r - l + 1)) % MOD);
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();

  return 0;
}
0