-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD. Different Triangles.cpp
214 lines (198 loc) · 4.94 KB
/
D. Different Triangles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*8<{==========~ BEGIN TEMPLATE ~============>8*/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.cpp"
#else
#define dbg(...)
#endif
#define endl '\n'
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define int long long
#define all(j) j.begin(), j.end()
#define rall(j) j.rbegin(), j.rend()
#define len(j) (int)j.size()
#define rep(i, a, b) \
for (common_type_t<decltype(a), decltype(b)> \
i = (a); \
i < (b); i++)
#define rrep(i, a, b) \
for (common_type_t<decltype(a), decltype(b)> \
i = (a); \
i > (b); i--)
#define trav(a, x) for (auto &a : x)
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define emp emplace
#define ins insert
#define divc(a, b) ((a) + (b)-1ll) / (b)
using str = string;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vll2d = vector<vll>;
using vi = vector<int>;
using vi2d = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vc = vector<char>;
using vs = vector<str>;
template <typename T>
using pqmn =
priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using pqmx = priority_queue<T, vector<T>>;
template <typename T, typename U>
inline bool chmax(T &a, U const &b) {
return (a < b ? a = b, 1 : 0);
}
template <typename T, typename U>
inline bool chmin(T &a, U const &b) {
return (a > b ? a = b, 1 : 0);
}
/*8<============~ END TEMPLATE ~============}>8*/
void run();
int32_t main() {
#ifndef LOCAL
fastio;
#endif
int T = 1;
// cin >> T;
rep(t, 0, T) {
dbg(t);
run();
}
}
/*8<
@Title:
Max \& Min Check
@Description:
Returns the min/max value in range [l, r] that
satisfies the lambda function check, if there
is no such value the max/min possible value
for that type will be returned.
@Time:
$O(\log{l-r+1})$
>8*/
template <typename T>
optional<T> maxCheck(T l, T r, auto check) {
optional<T> best;
while (l <= r) {
T m = midpoint(l, r);
if (check(m))
best ? chmax(best, m) : best = m, l = m + 1;
else
r = m - 1;
}
return best;
}
template <typename T>
optional<T> minCheck(T l, T r, auto check) {
optional<T> best;
while (l <= r) {
T m = midpoint(l, r);
if (check(m))
best ? chmin(best, m) : best = m, r = m - 1;
else
l = m + 1;
}
return best;
}
/*8<
@Title: Arithmetic Progression Sum
@Usage:
\begin{compactitem}
\item $s$ : first term
\item $d$ : common difference
\item $n$ : number of terms
\end{compactitem}
>8*/
inline ll f(ll s, ll d, ll n) {
return (s + (s + d * (n - 1))) * n / 2ll;
}
/*8< itera pelo maior lado
e acho e fecha uma formula pros outros dois
se o maior lado for l:
- então o segundo maior lado precisa ser
pelo menos ceil(l/2)
3 3 [ 1..3 ] 1->min(max(N-3*2
3 2 [ 2..2 ]
3 1 [ 2..2 ] X
[l] = [1]
[l,l2,l3mn,l3mx,inc] = [1, 1, 1, 1, 1]
[l] = [2]
[l,l2,l3mn,l3mx,inc] = [2, 2, 1, 2, 2]
[l] = [3]
[l,l2,l3mn,l3mx,inc] = [3, 2, 2, 2, 1]
[l,l2,l3mn,l3mx,inc] = [3, 3, 1, 3, 3]
[l] = [4]
[l,l2,l3mn,l3mx,inc] = [4, 3, 2, 3, 2]
[l,l2,l3mn,l3mx,inc] = [4, 4, 1, 4, 4]
[l] = [5]
[l,l2,l3mn,l3mx,inc] = [5, 3, 3, 3, 1]
[l,l2,l3mn,l3mx,inc] = [5, 4, 2, 4, 3]
[l,l2,l3mn,l3mx,inc] = [5, 5, 1, 5, 5]
[l] = [6]
[l,l2,l3mn,l3mx,inc] = [6, 4, 3, 4, 2]
[l,l2,l3mn,l3mx,inc] = [6, 5, 2, 5, 4]
[l,l2,l3mn,l3mx,inc] = [6, 6, 1, 6, 6]
[l] = [7]
[l,l2,l3mn,l3mx,inc] = [7, 4, 4, 4, 1]
[l,l2,l3mn,l3mx,inc] = [7, 5, 3, 5, 3]
[l,l2,l3mn,l3mx,inc] = [7, 6, 2, 6, 5]
[l,l2,l3mn,l3mx,inc] = [7, 7, 1, 6, 6]
[l] = [8]
[l,l2,l3mn,l3mx,inc] = [8, 5, 4, 5, 2]
[l,l2,l3mn,l3mx,inc] = [8, 6, 3, 6, 4]
[l,l2,l3mn,l3mx,inc] = [8, 7, 2, 5, 4]
[l,l2,l3mn,l3mx,inc] = [8, 8, 1, 4, 4]
[l] = [9]
[l,l2,l3mn,l3mx,inc] = [9, 5, 5, 5, 1]
[l,l2,l3mn,l3mx,inc] = [9, 6, 4, 5, 2]
[l,l2,l3mn,l3mx,inc] = [9, 7, 3, 4, 2]
[l,l2,l3mn,l3mx,inc] = [9, 8, 2, 3, 2]
[l,l2,l3mn,l3mx,inc] = [9, 9, 1, 2, 2]
>8*/
const int MOD = 1'000'000'000 + 7;
void run() {
int N;
cin >> N;
int ans = 0;
rep(l, 1, N + 1) {
if (l + l + 1 > N) break;
dbg(l);
int s = l / 2 + 1;
// l+2*s <= N which is the highest s?
auto hs = maxCheck(s, l, [&](int m) -> bool {
return l + m * 2 <= N;
});
if (hs) {
int inis = 2 - (l & 1);
ll inc = f(inis, 2, *hs - s + 1) % MOD;
ans = (ans + inc) % MOD;
}
hs = minCheck((int)1, l, [&](int m) -> bool {
return l + m * 2 > N;
});
if (hs) {
int l3mn = (l + 1) - *hs;
int l3mx = N - l - *hs;
int sz = l3mx - l3mn + 1;
int hs2 = min(N - 1 - l, l);
int inc = (hs2 - *hs + 1) * sz;
dbg(*hs, hs2, sz, inc);
ans = (ans + inc) % MOD;
}
}
cout << ans << endl;
}
// AC, binary search, math, geometry