-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.cpp
More file actions
71 lines (66 loc) · 2.03 KB
/
6.cpp
File metadata and controls
71 lines (66 loc) · 2.03 KB
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
#include "common.h"
namespace {
int64_t p1(const std::vector<std::string> &in) {
std::vector<int> times{};
std::vector<int> distances{};
{
std::stringstream ss{in[0]};
ss.ignore(std::numeric_limits<std::streamsize>::max(), ':');
std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(),
std::back_inserter(times));
}
{
std::stringstream ss{in[1]};
ss.ignore(std::numeric_limits<std::streamsize>::max(), ':');
std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(),
std::back_inserter(distances));
}
int64_t r{1};
for (size_t i{0}; i < times.size(); ++i) {
int t = times[i];
int d = distances[i];
// typical roots of quadratic eq: r(x - r) > y
// -b +- sqrt(b^2 - 4ac) / 2a where a = -1, b = x, c = -y
double l = (-t + sqrt(t * t - 4 * -1 * -d)) / -2.0;
double u = (-t - sqrt(t * t - 4 * -1 * -d)) / -2.0;
l = (ceil(l) == l)
? ceil(l) + 1
: ceil(l); // if the roots are integers than > 0 cannot be
// satisified; so ignore the limits if they satisfy
u = (floor(u) == u) ? floor(u) - 1 : floor(u);
r *= (u - l + 1);
}
return r;
}
int64_t p2(const std::vector<std::string> &in) {
int64_t t, d;
{
std::istringstream iss{in[0]};
iss.ignore(std::numeric_limits<std::streamsize>::max(), ':');
std::ostringstream oss{};
for (std::string s; iss >> s;) {
oss << s;
}
t = std::stoll(oss.str());
}
{
std::istringstream iss{in[1]};
iss.ignore(std::numeric_limits<std::streamsize>::max(), ':');
std::ostringstream oss{};
for (std::string s; iss >> s;) {
oss << s;
}
d = std::stoll(oss.str());
};
double l = (-t + sqrt(t * t - 4 * -1 * -d)) / -2.0;
double u = (-t - sqrt(t * t - 4 * -1 * -d)) / -2.0;
l = (ceil(l) == l) ? ceil(l) + 1 : ceil(l);
u = (floor(u) == u) ? floor(u) - 1 : floor(u);
return (u - l + 1);
}
} // namespace
int main() {
const auto &in = gb::readIn();
gb::writeOut(p1(in));
gb::writeOut(p2(in));
}