Skip to content

#34: Add explicit for some constructors #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion atcoder/dsu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace atcoder {
struct dsu {
public:
dsu() : _n(0) {}
dsu(int n) : _n(n), parent_or_size(n, -1) {}
explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}

int merge(int a, int b) {
assert(0 <= a && a < _n);
Expand Down
2 changes: 1 addition & 1 deletion atcoder/fenwicktree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ template <class T> struct fenwick_tree {

public:
fenwick_tree() : _n(0) {}
fenwick_tree(int n) : _n(n), data(n) {}
explicit fenwick_tree(int n) : _n(n), data(n) {}

void add(int p, T x) {
assert(0 <= p && p < _n);
Expand Down
2 changes: 1 addition & 1 deletion atcoder/internal_csr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace internal {
template <class E> struct csr {
std::vector<int> start;
std::vector<E> elist;
csr(int n, const std::vector<std::pair<int, E>>& edges)
explicit csr(int n, const std::vector<std::pair<int, E>>& edges)
: start(n + 1), elist(edges.size()) {
for (auto e : edges) {
start[e.first + 1]++;
Expand Down
31 changes: 30 additions & 1 deletion atcoder/internal_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct barrett {
unsigned long long im;

// @param m `1 <= m < 2^31`
barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

// @return m
unsigned int umod() const { return _m; }
Expand Down Expand Up @@ -177,6 +177,35 @@ constexpr int primitive_root_constexpr(int m) {
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}

unsigned long long y_max = a * n + b;
if (y_max < m) break;
// y_max < m * (n + 1)
// floor(y_max / m) <= n
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}

} // namespace internal

} // namespace atcoder
Expand Down
2 changes: 1 addition & 1 deletion atcoder/internal_scc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace internal {
// Depth-First Search and Linear Graph Algorithms
struct scc_graph {
public:
scc_graph(int n) : _n(n) {}
explicit scc_graph(int n) : _n(n) {}

int num_vertices() { return _n; }

Expand Down
4 changes: 2 additions & 2 deletions atcoder/lazysegtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ template <class S,
struct lazy_segtree {
public:
lazy_segtree() : lazy_segtree(0) {}
lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
explicit lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
Expand Down
24 changes: 12 additions & 12 deletions atcoder/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ std::pair<long long, long long> crt(const std::vector<long long>& r,
}

long long floor_sum(long long n, long long m, long long a, long long b) {
long long ans = 0;
if (a >= m) {
ans += (n - 1) * n / 2 * (a / m);
a %= m;
assert(0 <= n && n < (1LL << 32));
assert(1 <= m && m < (1LL << 32));
unsigned long long ans = 0;
if (a < 0) {
unsigned long long a2 = internal::safe_mod(a, m);
ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
a = a2;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
if (b < 0) {
unsigned long long b2 = internal::safe_mod(b, m);
ans -= 1ULL * n * ((b2 - b) / m);
b = b2;
}

long long y_max = a * n + b;
if (y_max < m) return ans;
ans += floor_sum(y_max / m, a, m, y_max % m);
return ans;
return ans + internal::floor_sum_unsigned(n, m, a, b);
}

} // namespace atcoder
Expand Down
2 changes: 1 addition & 1 deletion atcoder/maxflow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace atcoder {
template <class Cap> struct mf_graph {
public:
mf_graph() : _n(0) {}
mf_graph(int n) : _n(n), g(n) {}
explicit mf_graph(int n) : _n(n), g(n) {}

int add_edge(int from, int to, Cap cap) {
assert(0 <= from && from < _n);
Expand Down
2 changes: 1 addition & 1 deletion atcoder/mincostflow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace atcoder {
template <class Cap, class Cost> struct mcf_graph {
public:
mcf_graph() {}
mcf_graph(int n) : _n(n) {}
explicit mcf_graph(int n) : _n(n) {}

int add_edge(int from, int to, Cap cap, Cost cost) {
assert(0 <= from && from < _n);
Expand Down
2 changes: 1 addition & 1 deletion atcoder/modint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ template <int id> struct dynamic_modint : internal::modint_base {
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
Expand Down
2 changes: 1 addition & 1 deletion atcoder/scc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace atcoder {
struct scc_graph {
public:
scc_graph() : internal(0) {}
scc_graph(int n) : internal(n) {}
explicit scc_graph(int n) : internal(n) {}

void add_edge(int from, int to) {
int n = internal.num_vertices();
Expand Down
4 changes: 2 additions & 2 deletions atcoder/segtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace atcoder {
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
segtree() : segtree(0) {}
segtree(int n) : segtree(std::vector<S>(n, e())) {}
segtree(const std::vector<S>& v) : _n(int(v.size())) {
explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
Expand Down
2 changes: 1 addition & 1 deletion atcoder/twosat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace atcoder {
struct two_sat {
public:
two_sat() : _n(0), scc(0) {}
two_sat(int n) : _n(n), _answer(n), scc(2 * n) {}
explicit two_sat(int n) : _n(n), _answer(n), scc(2 * n) {}

void add_clause(int i, bool f, int j, bool g) {
assert(0 <= i && i < _n);
Expand Down
4 changes: 4 additions & 0 deletions document_en/appendix.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ vector<long long> c = convolution<924844033>(a, b);
In the first case, $m$ is automatically set to be $998244353$.
In the second case, $m$ becomes the value that is explicitly specified, which is $924844033$ here.

### 💻 explicit specifier

Constructors of structs except `modint` are declared with the explicit specifier.

## Precise requirements in Segtree / LazySegtree

In some situations, the cardinality of algebraic structures for Segtree / LazySegtree would be infinite. In precise meaning, it may break the constraints in the document.
Expand Down
13 changes: 8 additions & 5 deletions document_en/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,20 @@ $y, z$ $(0 \leq y < z = \mathrm{lcm}(m[i]))$. It returns this $(y, z)$ as a pair
ll floor_sum(ll n, ll m, ll a, ll b)
```

It returns $\sum_{i = 0}^{n - 1} \mathrm{floor}(\frac{a \times i + b}{m})$.
It returns

$$\sum_{i = 0}^{n - 1} \left\lfloor \frac{a \times i + b}{m} \right\rfloor$$

It returns the answer in $\bmod 2^{\mathrm{64}}$, if overflowed.

**@{keyword.constraints}**

- $0 \leq n \leq 10^9$
- $1 \leq m \leq 10^9$
- $0 \leq a, b \lt m$
- $0 \leq n \lt 2^{32}$
- $1 \leq m \lt 2^{32}$

**@{keyword.complexity}**

- $O(\log{(n+m+a+b)})$
- $O(\log{(m+a)})$

## @{keyword.examples}

Expand Down
4 changes: 4 additions & 0 deletions document_ja/appendix.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ vector<long long> c = convolution<924844033>(a, b);
上段のように使った場合は、$m$ の値は自動的に $998244353$ となります。
下段のように使った場合は、$m$ の値は明示的に与えた値 (この場合は $924844033$) となります。

### 💻 explicit 指定子

`modint` 以外の構造体のコンストラクタには explicit が付いています。

## Segtree / LazySegtree の厳密な要件

Segtree / LazySegtree を使いたい状況において、扱う代数構造が無限集合である場合があります。たとえば、与えられた区間の $\mathrm{max}$ を求める、与えられた区間内の全ての要素に定数を足す、の二種類のクエリに対応する LazySegtree はよくありますが、このときたとえば $S = \mathrm{int}$ としてしまうと、$S$ は加法について閉じていない (overflow を起こす可能性がある) ため、厳密な意味でドキュメント本編の制約を満たしません。そこで、AC Library では以下のような場合正しく動くことを保証しています。
Expand Down
11 changes: 5 additions & 6 deletions document_ja/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,18 @@ $$x \equiv r[i] \pmod{m[i]}, \forall i \in \lbrace 0,1,\cdots, n - 1 \rbrace$$
ll floor_sum(ll n, ll m, ll a, ll b)
```

$\sum_{i = 0}^{n - 1} \mathrm{floor}(\frac{a \times i + b}{m})$
$$\sum_{i = 0}^{n - 1} \left\lfloor \frac{a \times i + b}{m} \right\rfloor$$

を返します。
を返します。答えがオーバーフローしたならば $\bmod 2^{\mathrm{64}}$ で等しい値を返します。

**@{keyword.constraints}**

- $0 \leq n \leq 10^9$
- $1 \leq m \leq 10^9$
- $0 \leq a, b \lt m$
- $0 \leq n \lt 2^{32}$
- $1 \leq m \lt 2^{32}$

**@{keyword.complexity}**

- $O(\log{(n+m+a+b)})$
- $O(\log{(m+a)})$

## @{keyword.examples}

Expand Down
7 changes: 4 additions & 3 deletions test/unittest/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ ll pow_mod_naive(ll x, ull n, uint mod) {
ll floor_sum_naive(ll n, ll m, ll a, ll b) {
ll sum = 0;
for (ll i = 0; i < n; i++) {
sum += (a * i + b) / m;
ll z = a * i + b;
sum += (z - internal::safe_mod(z, m)) / m;
}
return sum;
}
Expand Down Expand Up @@ -93,8 +94,8 @@ TEST(MathTest, InvModZero) {
TEST(MathTest, FloorSum) {
for (int n = 0; n < 20; n++) {
for (int m = 1; m < 20; m++) {
for (int a = 0; a < 20; a++) {
for (int b = 0; b < 20; b++) {
for (int a = -20; a < 20; a++) {
for (int b = -20; b < 20; b++) {
ASSERT_EQ(floor_sum_naive(n, m, a, b),
floor_sum(n, m, a, b));
}
Expand Down