Felix's Library

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

View the Project on GitHub fffelix-huang/CP-stuff

:heavy_check_mark: Integer division ($\lfloor \frac{a}{b} \rfloor \lceil \frac{a}{b} \rceil$)
(library/math/integer-div.hpp)

floor_div(a, b) 回傳最大且 $\leq \frac{a}{b}$ 的整數

ceil_div(a, b) 回傳最小且 $\geq \frac{a}{b}$ 的整數

使用方法

int x, y;

int a = floor_div(x, y);
int b = ceil_div(x, y);
int c = floor_div(-2, 4); // -1
int d = floor_div(-5, 2); // -3
int e = ceil_div(-2, 4);  //  0

Required by

Verified with

Code

#pragma once

namespace felix {

template<class T>
T floor_div(T a, T b) {
	return a / b - ((a ^ b) < 0 && a % b != 0);
}

template<class T>
T ceil_div(T a, T b) {
	return a / b + ((a ^ b) > 0 && a % b != 0);
}

} // namespace felix
#line 2 "library/math/integer-div.hpp"

namespace felix {

template<class T>
T floor_div(T a, T b) {
	return a / b - ((a ^ b) < 0 && a % b != 0);
}

template<class T>
T ceil_div(T a, T b) {
	return a / b + ((a ^ b) > 0 && a % b != 0);
}

} // namespace felix
Back to top page