-
Notifications
You must be signed in to change notification settings - Fork 1
/
aoj_1300.test.cpp
149 lines (139 loc) · 3.81 KB
/
aoj_1300.test.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
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1300"
#include "../../template/template.hpp"
#include "../../template/parsing_template.hpp"
#include "../../math/gauss_jordan.hpp"
#include "../../utility/rational.hpp"
using namespace lib;
std::vector<std::vector<rational>> chemical_equation(State &);
std::vector<std::map<std::string, ll>> molecule_sequence(State &);
std::map<std::string, ll> molecule(State &);
std::map<std::string, ll> group(State &);
std::map<std::string, ll> unit_group(State &);
std::pair<std::string, ll> chemical_element(State &);
ll number(State &);
std::vector<std::vector<rational>> chemical_equation(State &begin) {
auto lhs = molecule_sequence(begin);
consume(begin, '-');
consume(begin, '>');
auto rhs = molecule_sequence(begin);
std::set<std::string> set;
for (auto &map : lhs) {
for (auto &p : map) set.insert(p.first);
}
int h = set.size();
std::vector a(h, std::vector<rational>());
rep(i, 0, lhs.size()) {
int j = 0;
for (auto &s : set) {
a[j].emplace_back(lhs[i][s]);
j++;
}
}
rep(i, 0, rhs.size()) {
int j = 0;
for (auto &s : set) {
a[j].emplace_back(-rhs[i][s]);
j++;
}
}
assert(a[0].size() == lhs.size() + rhs.size());
return a;
}
std::vector<std::map<std::string, ll>> molecule_sequence(State &begin) {
std::vector<std::map<std::string, ll>> mole;
mole.emplace_back(molecule(begin));
while (expect(begin, '+')) {
consume(begin, '+');
mole.emplace_back(molecule(begin));
}
return mole;
}
std::map<std::string, ll> molecule(State &begin) {
auto ret = group(begin);
while (isAlpha(*begin) || expect(begin, '(')) {
for (auto [s, c] : group(begin)) {
ret[s] += c;
}
}
return ret;
}
std::map<std::string, ll> group(State &begin) {
auto ret = unit_group(begin);
if (isdigit(*begin)) {
ll num = number(begin);
for (auto &p : ret) {
p.second *= num;
}
}
return ret;
}
std::map<std::string, ll> unit_group(State &begin) {
if (isAlpha(*begin)) {
std::string ret = "";
ret += *begin;
consume(begin, *begin);
if (isalpha(*begin)) {
ret += *begin;
consume(begin, *begin);
}
std::map<std::string, ll> map;
map[ret] = 1;
return map;
} else {
consume(begin, '(');
auto ret = molecule(begin);
consume(begin, ')');
return ret;
}
}
ll number(State &begin) {
ll ret = 0;
while (isdigit(*begin)) {
ret *= 10;
ret += *begin - '0';
consume(begin, *begin);
}
return ret;
}
int main() {
std::string s;
while (std::cin >> s, !(s == ".")) {
State begin = s.begin();
auto a = chemical_equation(begin);
consume(begin, '.');
a = gauss_jordan<rational>(a);
int n = a.size();
int m = a[0].size();
int x = -1;
{
bool is_break = false;
rrep(i, 0, n) {
if(is_break) break;
rep(j, 0, m) {
if (a[i][j] != 0) {
x = j + 1;
is_break = true;
break;
}
}
}
}
std::vector<ll> ans(m, 1);
rep(j,x,m) {
rep(i,0,n) {
if(a[i][j] != 0) {
ans[j] = std::lcm(ans[j], a[i][j].val().second);
}
}
}
rep(i,0,x) {
ans[i] = 0;
rep(j,x,m) {
ans[i] -= (a[i][j] * ans[j]).val().first;
}
}
rep(i,0,m) {
std::cout << ans[i] << " \n"[i == m-1];
}
}
}