Skip to content

Commit e71276e

Browse files
committed
Add quotient_range to library
1 parent a82424b commit e71276e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: lib/number/quitient_range.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 計算量: O(√N)
3+
* 自然数 N の商の種類数を返す。
4+
* ⌊N/i⌋ = k となる最左の i を l , 最右の i を r とすると [l, r] で N / i のあまりは等差数列になる。
5+
* verified: https://codeforces.com/contest/1263/problem/C
6+
* @param N 自然数 N
7+
* @return x <= i <= y を満たす整数の商 ⌊N/i⌋ が z であるとき, {{x, y}, z} と表す。x の昇順で返す
8+
*/
9+
vector<pair<P, ll>> quotient_range(ll N) {
10+
ll M;
11+
vector<pair<P, ll> > res;
12+
for (M = 1; M * M <= N; M++) {
13+
res.emplace_back(make_pair(M, M), N / M);
14+
}
15+
for (ll i = M; i >= 1; i--) {
16+
ll L = N / (i + 1) + 1;
17+
ll R = N / i;
18+
if (L <= R && res.back().first.second < L) res.emplace_back(make_pair(L, R), N / L);
19+
}
20+
return res;
21+
}

0 commit comments

Comments
 (0)