#include<algorithm>
#include<array>
#include<iomanip>
#include<ios>
#include<iostream>
#include<map>
#include<numeric>
#include<string>
#include<set>
#include<vector>
#include<queue>
// PROTOTYPY
// TYPY
struct Oczekuje {
uint64_t kod;
uint64_t t;
};
// DANE
static int suma;
// PROCEDURY
static uint64_t koduj(unsigned a, unsigned b, unsigned c) {
(void)c;
return (uint64_t(a) << 32) + b;
}
static void odkoduj(uint64_t kod, unsigned &a, unsigned &b, unsigned &c) {
a = kod >> 32;
b = kod - (uint64_t(a) << 32);
c = suma - a - b;
}
static void przelej(unsigned &z, unsigned &w, const unsigned W) {
if (z + w <= W) {
w += z;
z = 0;
} else {
z = (z + w) - W;
w = W;
}
}
int main()
{
unsigned a, b, c, A, B, C;
std::map<unsigned, uint64_t> kiedy;
std::set<uint64_t> stany;
std::queue<Oczekuje> kolejka;
std::set<unsigned> pozadane;
std::cin >> A >> B >> C;
std::cin >> a >> b >> c;
for (unsigned i = 0; i <= C; ++i) {
pozadane.insert(i);
}
suma = a + b + c;
kolejka.push({koduj(a, b, c), 1});
stany.insert(koduj(a, b, c));
kiedy.insert({a, 0});
kiedy.insert({b, 0});
kiedy.insert({c, 0});
pozadane.erase(a);
pozadane.erase(b);
pozadane.erase(c);
while (!kolejka.empty() && !pozadane.empty()) {
Oczekuje ocz = kolejka.front();
uint64_t kod = ocz.kod;
uint64_t t = ocz.t;
kolejka.pop();
// std::cerr << std::hex << std::setw(12) << kod << ' ' << std::dec << kiedy.size() << std::endl;
auto dzialaj = [&a, &b, &c, &t, &kod, &kolejka, &stany, &kiedy, &pozadane](unsigned &z, unsigned &w, unsigned W) {
odkoduj(kod, a, b, c);
// std::cerr << std::hex << std::setw(12) << kod << ' ' << std::dec << a << ' ' << b << ' ' << c << ' ' << z << ' ' << w << std::endl;
przelej(z, w, W);
kiedy.insert({z, t});
kiedy.insert({w, t});
pozadane.erase(z);
pozadane.erase(w);
uint64_t nowy_kod = koduj(a, b, c);
// std::cerr << std::hex << std::setw(12) << nowy_kod << ' ' << std::dec << a << ' ' << b << ' ' << c << std::endl;
if (stany.find(nowy_kod) == stany.end()) {
stany.insert(nowy_kod);
kolejka.push({nowy_kod, t+1});
}
};
dzialaj(a, b, B);
dzialaj(a, c, C);
dzialaj(b, a, A);
dzialaj(b, c, C);
dzialaj(c, a, A);
dzialaj(c, b, B);
// std::cerr << std::endl;
}
for (unsigned i = 0; i < C+1; ++i) {
if (kiedy.find(i) != kiedy.end()) {
std::cout << kiedy[i] << ' ';
} else {
std::cout << -1 << ' ';
}
}
std::cout << std::endl;
return 0;
}
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 | #include<algorithm> #include<array> #include<iomanip> #include<ios> #include<iostream> #include<map> #include<numeric> #include<string> #include<set> #include<vector> #include<queue> // PROTOTYPY // TYPY struct Oczekuje { uint64_t kod; uint64_t t; }; // DANE static int suma; // PROCEDURY static uint64_t koduj(unsigned a, unsigned b, unsigned c) { (void)c; return (uint64_t(a) << 32) + b; } static void odkoduj(uint64_t kod, unsigned &a, unsigned &b, unsigned &c) { a = kod >> 32; b = kod - (uint64_t(a) << 32); c = suma - a - b; } static void przelej(unsigned &z, unsigned &w, const unsigned W) { if (z + w <= W) { w += z; z = 0; } else { z = (z + w) - W; w = W; } } int main() { unsigned a, b, c, A, B, C; std::map<unsigned, uint64_t> kiedy; std::set<uint64_t> stany; std::queue<Oczekuje> kolejka; std::set<unsigned> pozadane; std::cin >> A >> B >> C; std::cin >> a >> b >> c; for (unsigned i = 0; i <= C; ++i) { pozadane.insert(i); } suma = a + b + c; kolejka.push({koduj(a, b, c), 1}); stany.insert(koduj(a, b, c)); kiedy.insert({a, 0}); kiedy.insert({b, 0}); kiedy.insert({c, 0}); pozadane.erase(a); pozadane.erase(b); pozadane.erase(c); while (!kolejka.empty() && !pozadane.empty()) { Oczekuje ocz = kolejka.front(); uint64_t kod = ocz.kod; uint64_t t = ocz.t; kolejka.pop(); // std::cerr << std::hex << std::setw(12) << kod << ' ' << std::dec << kiedy.size() << std::endl; auto dzialaj = [&a, &b, &c, &t, &kod, &kolejka, &stany, &kiedy, &pozadane](unsigned &z, unsigned &w, unsigned W) { odkoduj(kod, a, b, c); // std::cerr << std::hex << std::setw(12) << kod << ' ' << std::dec << a << ' ' << b << ' ' << c << ' ' << z << ' ' << w << std::endl; przelej(z, w, W); kiedy.insert({z, t}); kiedy.insert({w, t}); pozadane.erase(z); pozadane.erase(w); uint64_t nowy_kod = koduj(a, b, c); // std::cerr << std::hex << std::setw(12) << nowy_kod << ' ' << std::dec << a << ' ' << b << ' ' << c << std::endl; if (stany.find(nowy_kod) == stany.end()) { stany.insert(nowy_kod); kolejka.push({nowy_kod, t+1}); } }; dzialaj(a, b, B); dzialaj(a, c, C); dzialaj(b, a, A); dzialaj(b, c, C); dzialaj(c, a, A); dzialaj(c, b, B); // std::cerr << std::endl; } for (unsigned i = 0; i < C+1; ++i) { if (kiedy.find(i) != kiedy.end()) { std::cout << kiedy[i] << ' '; } else { std::cout << -1 << ' '; } } std::cout << std::endl; return 0; } |
English