kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:x: test/yosupo-judge/sum_of_floor_of_linear/main.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/sum_of_floor_of_linear"

#include <iostream>
#include "Mylib/IO/input_tuples.cpp"
#include "Mylib/Math/sum_of_floor_of_linear.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int T;
  std::cin >> T;

  for (auto [N, M, A, B] : hl::input_tuples<int64_t, int64_t, int64_t, int64_t>(T)) {
    std::cout << hl::sum_of_floor_of_linear(N, M, A, B) << "\n";
  }

  return 0;
}
#line 1 "test/yosupo-judge/sum_of_floor_of_linear/main.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/sum_of_floor_of_linear"

#include <iostream>
#line 2 "Mylib/IO/input_tuples.cpp"
#include <initializer_list>
#line 4 "Mylib/IO/input_tuples.cpp"
#include <tuple>
#include <utility>
#include <vector>
#line 6 "Mylib/IO/input_tuple.cpp"

namespace haar_lib {
  template <typename T, size_t... I>
  static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) {
    (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...};
  }

  template <typename T, typename U>
  std::istream &operator>>(std::istream &s, std::pair<T, U> &value) {
    s >> value.first >> value.second;
    return s;
  }

  template <typename... Args>
  std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) {
    input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>());
    return s;
  }
}  // namespace haar_lib
#line 8 "Mylib/IO/input_tuples.cpp"

namespace haar_lib {
  template <typename... Args>
  class InputTuples {
    struct iter {
      using value_type = std::tuple<Args...>;
      value_type value;
      bool fetched = false;
      int N, c = 0;

      value_type operator*() {
        if (not fetched) {
          std::cin >> value;
        }
        return value;
      }

      void operator++() {
        ++c;
        fetched = false;
      }

      bool operator!=(iter &) const {
        return c < N;
      }

      iter(int N) : N(N) {}
    };

    int N;

  public:
    InputTuples(int N) : N(N) {}

    iter begin() const { return iter(N); }
    iter end() const { return iter(N); }
  };

  template <typename... Args>
  auto input_tuples(int N) {
    return InputTuples<Args...>(N);
  }
}  // namespace haar_lib
#line 2 "Mylib/Math/sum_of_floor_of_linear.cpp"
#include <cstdint>

namespace haar_lib {
  int64_t sum_of_floor_of_linear(int64_t N, int64_t M, int64_t A, int64_t B) {
    int64_t ret = 0;

    if (B >= M) {
      ret += N * (B / M);
      B %= M;
    }

    if (A >= M) {
      ret += N * (N - 1) * (A / M) / 2;
      A %= M;
    }

    int64_t y_max = (A * N + B) / M;
    int64_t x_max = y_max * M - B;
    if (y_max == 0) return ret;

    ret += (N - (x_max + A - 1) / A) * y_max;
    ret += sum_of_floor_of_linear(y_max, A, M, (A - x_max % A) % A);
    return ret;
  }
}  // namespace haar_lib
#line 6 "test/yosupo-judge/sum_of_floor_of_linear/main.test.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int T;
  std::cin >> T;

  for (auto [N, M, A, B] : hl::input_tuples<int64_t, int64_t, int64_t, int64_t>(T)) {
    std::cout << hl::sum_of_floor_of_linear(N, M, A, B) << "\n";
  }

  return 0;
}
Back to top page