#include #include #include #include #include #include #include #include using namespace std; typedef vector &, int&)>> v_t; v_t rules(14); map values{ {"2", 2}, {"3", 3}, {"4" , 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10}, {"J", 10}, {"Q", 10}, {"K", 10}, {"A", 10} }; vector primes; string get_val(string card) { if (card.length() == 3) return card.substr(0, 2); else return card.substr(0, 1); } int get_val_i(string card) { return values[get_val(card)]; } int get_val_order(string card) { int val = get_val_i(card); if (val < 10) return val; string val_s = get_val(card); if (val_s == "10") return 10; if (val_s == "J") return 11; if (val_s == "Q") return 12; if (val_s == "K") return 13; else return 14; } string get_deck(string card) { if (card.length() == 3) return card.substr(2, 1); else return card.substr(1, 1); } int rule_1(vector& hand, int& value) { int ret = 0; if (hand.size() >= 4) { ret += 1; int j_count = 0; for (int i = 0; i < hand.size(); i++) { if (get_val(hand[i]) == "J") j_count++; } ret += get_val_i(hand[0]) * j_count; value += ret; return 1; } return 0; } int rule_2(vector& hand, int& value) { map counts; for (int i = 0; i < hand.size(); i++) { if (counts.find(get_deck(hand[i])) != counts.end()) { counts[get_deck(hand[i])]++; } else { counts[get_deck(hand[i])] = 1; } } bool apply = false; for (auto p : counts) { if (p.second >= 2) apply = true; } if (apply) value *= 2; return value != 0; } int rule_3(vector& hand, int& value) { map counts; for (int i = 0; i < hand.size(); i++) { if (counts.find(get_deck(hand[i])) != counts.end()) { counts[get_deck(hand[i])]++; } else { counts[get_deck(hand[i])] = 1; } } if (counts.size() == 4) { value *= 2; } return value != 0; } int rule_4(vector& hand, int& value) { map counts{ {"D", 0}, {"H", 0}, {"C", 0}, {"S", 0} }; for (int i = 0; i < hand.size(); i++) { counts[get_deck(hand[i])]++; } int cou_b = counts["C"] + counts["S"]; int cou_r = counts["D"] + counts["H"]; int val = abs(cou_b - cou_r); value += val; return val != 0; } int rule_5(vector& hand, int& value) { if (value % 2 == 0) { int add = 0; for (int i = 1; i <= value; i++) { if (value % i == 0) add += i; } value += add; return add != 0; } return 0; } int rule_6(vector& hand, int& value) { int count_r_7 = 0; for (int i = 0; i < hand.size(); i++) { if (get_val(hand[i]) == "7") count_r_7++; } if (count_r_7 == 4) { value -= 121; return 1; } return 0; } int rule_7(vector& hand, int& value) { if (value >= 0) { int min_val = -1; for (int i = 0; i < hand.size(); i++) { if (min_val < 0 || min_val > get_val_i(hand[i])) min_val = get_val_i(hand[i]); } value += min_val; return 1; } return 0; } int rule_8(vector& hand, int& value) { if (value < 0) { value *= -1; return 1; } return 0; } int rule_9(vector& hand, int& value) { map counts{ {"D", 0}, {"H", 0}, {"C", 0}, {"S", 0} }; for (int i = 0; i < hand.size(); i++) { counts[get_deck(hand[i])]++; } if (counts["D"] >= 3) { value += 1; for (int i = 0; i < hand.size(); i++) { if (get_val(hand[i]) == "9") { hand[i] = "6" + get_deck(hand[i]); } else if (get_val(hand[i]) == "6") { hand[i] = "9" + get_deck(hand[i]); } else if (get_val(hand[i]) == "2") { hand[i] = "5" + get_deck(hand[i]); } else if (get_val(hand[i]) == "5") { hand[i] = "2" + get_deck(hand[i]); } } return 1; } return 0; } int rule_10(vector& hand, int& value) { if (hand.size() < 5) return 0; vector ranks(hand.size()); for (int i = 0; i < hand.size(); i++) { ranks[i] = get_val_order(hand[i]); } sort(ranks.begin(), ranks.end()); int longest_cons = 1; int len_cons = 1; for (int i = 1; i < hand.size(); i++) { if (ranks[i] == ranks[i - 1] + 1) len_cons++; else { len_cons = 1; } if (len_cons > longest_cons) longest_cons = len_cons; } if (longest_cons >= 5) { map counts; for (int i = 0; i < hand.size(); i++) { if (counts.find(get_val(hand[i])) != counts.end()) { counts[get_val(hand[i])]++; } else { counts[get_val(hand[i])] = 1; } } int count_a = 0; if (counts.find("A") != counts.end()) { count_a = counts["A"]; } value += count_a * 5; return count_a != 0; } return 0; } int rule_11(vector& hand, int& value) { bitset<32> bit(value); auto count = bit.count(); value += count; return count != 0; } int rule_12(vector& hand, int& value, int last_mod) { bool has_2 = false; for (int i = 0; i < hand.size(); i++) { if (get_val_i(hand[i]) == 2) has_2 = true; } if (has_2) { return rules[last_mod](hand, value); } return 0; } int rule_13(vector& hand, int& value) { bool has_2 = false; for (int i = 0; i < hand.size(); i++) { if (get_val_i(hand[i]) == 2) has_2 = true; } if (has_2) { int prod = 1; for (int p : primes) { if (p > value) break; int cur_p = 1; while (value % (cur_p*p) == 0) { cur_p *= p; } prod *= cur_p; } value += prod; return 1; } return 0; } void compute_primes(vector& primes, int limit) { vector flags(limit); for (int i = 0; i < limit; i++) { flags[i] = true; } flags[0] = false; flags[1] = false; for (int j = 2; j < int(sqrt(limit)); j++) { if (flags[j]) { primes.push_back(j); for (int k = j * j; k < limit; k += j) { flags[k] = false; } } } } int main() { int const max_prime = 10000; compute_primes(primes, max_prime); rules[0] = rule_1; rules[1] = rule_2; rules[2] = rule_3; rules[3] = rule_4; rules[4] = rule_5; rules[5] = rule_6; rules[6] = rule_7; rules[7] = rule_8; rules[8] = rule_9; rules[9] = rule_10; rules[10] = rule_11; vector hand; string card; int card_count = 5; int ind = 0; while (cin >> card) { hand.push_back(card); ind++; if (ind == 5) break; } int value = 0; for (int i = 0; i < hand.size(); i++) { value += values[get_val(hand[i])]; } vector applied(14); for (int r = 0; r < 10; r++) { applied[r] = rules[r](hand, value); } int mod_by = 0; for (int r = 0; r < 10; r++) { if (applied[r]) mod_by++; } if (mod_by > 8) { applied[10] = rules[10](hand, value); } int last_mod = -1; for (int r = 10; r >= 0; r--) { if (applied[r] > 0) { last_mod = r; break; } } if (last_mod >= 0) { rule_12(hand, value, last_mod); } rule_13(hand, value); cout << value << endl; return 0; }