-
Notifications
You must be signed in to change notification settings - Fork 1
/
Twitter_240428_2.cpp
47 lines (41 loc) · 1.17 KB
/
Twitter_240428_2.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
// 鈴木伸介@数学アカデミー(@suzzukes)さん作の問題の解答
// https://x.com/dc1394/status/1784540223755673932
#include <array> // for std::array
#include <cstdint> // for std::int32_t
#include <iostream> // for std::cout, std::endl
#include <numeric> // for std::iota
#include <optional> // for std::make_optional, std::optional
namespace {
static auto constexpr N = 1000;
static auto constexpr M = 11;
static auto constexpr L = 19;
std::optional<std::int32_t> get_answer();
} // namespace
int main()
{
if (auto const answer = get_answer()) {
std::cout << "answer = " << *answer << std::endl;
}
}
namespace {
std::optional<std::int32_t> get_answer()
{
for (auto i = 1; i < N; i++) {
std::array<std::int32_t, M> arr;
std::iota(arr.begin(), arr.end(), i);
auto oddsum = 0;
auto evensum = 0;
for (auto j = 0; j < M; j++) {
if (j & 1) {
oddsum += arr[j];
} else {
evensum += arr[j];
}
}
if (evensum - oddsum == L) {
return std::make_optional(i + M - 1);
}
}
return std::nullopt;
}
} // namespace