This documentation is automatically generated by competitive-verifier/competitive-verifier
// clang-format off
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/sum_of_floor_of_linear
// clang-format on
#include <iostream>
#include "../../math/number-theory/sum-of-floor-of-linear.hpp"
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long N, M, A, B;
cin >> N >> M >> A >> B;
cout << sum_of_floor_of_linear(N, M, A, B) << "\n";
}
}
#line 1 "test/verify/yosupo-sum-of-floor-of-linear.test.cpp"
// clang-format off
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/sum_of_floor_of_linear
// clang-format on
#include <iostream>
#line 2 "math/number-theory/sum-of-floor-of-linear.hpp"
template <typename T>
T sum_of_floor_of_linear(const T& n, const T& m, T a, T b) {
T ret = 0;
if (a >= m) ret += (n - 1) * n * (a / m) / 2, a %= m;
if (b >= m) ret += n * (b / m), b %= m;
T y = (a * n + b) / m;
if (y == 0) return ret;
T x = y * m - b;
ret += (n - (x + a - 1) / a) * y;
ret += sum_of_floor_of_linear(y, a, m, (a - x % a) % a);
return ret;
}
#line 8 "test/verify/yosupo-sum-of-floor-of-linear.test.cpp"
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long N, M, A, B;
cin >> N >> M >> A >> B;
cout << sum_of_floor_of_linear(N, M, A, B) << "\n";
}
}
| Env | Name | Status | Elapsed | Memory |
|---|---|---|---|---|
| g++ | example_00 |
|
2 ms | 4 MB |
| g++ | random_00 |
|
47 ms | 4 MB |
| g++ | random_01 |
|
169 ms | 4 MB |
| g++ | random_02 |
|
170 ms | 4 MB |
| g++ | random_03 |
|
85 ms | 4 MB |
| g++ | random_04 |
|
36 ms | 4 MB |
| g++ | small_00 |
|
30 ms | 4 MB |
| g++ | small_01 |
|
98 ms | 4 MB |
| g++ | small_02 |
|
81 ms | 4 MB |
| g++ | small_03 |
|
51 ms | 4 MB |
| g++ | small_04 |
|
25 ms | 4 MB |
| clang++ | example_00 |
|
2 ms | 4 MB |
| clang++ | random_00 |
|
44 ms | 4 MB |
| clang++ | random_01 |
|
157 ms | 4 MB |
| clang++ | random_02 |
|
184 ms | 4 MB |
| clang++ | random_03 |
|
118 ms | 4 MB |
| clang++ | random_04 |
|
57 ms | 4 MB |
| clang++ | small_00 |
|
41 ms | 4 MB |
| clang++ | small_01 |
|
141 ms | 4 MB |
| clang++ | small_02 |
|
112 ms | 4 MB |
| clang++ | small_03 |
|
52 ms | 4 MB |
| clang++ | small_04 |
|
35 ms | 4 MB |