#include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::unordered_set<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } bool viable(int k, int n) { int red = k; int green = n - k; while (red > 1 || green > 1) { if (green > red) { using std::swap; swap(green, red); } // red >= green green += red / 2; red -= (red / 2) * 2; } return red == 0 || green == 0; } class Bin { public: Bin(std::vector<int64_t> rep) : rep_(std::move(rep)) {} int64_t get(int index, int mod) const { return rep_[3 * index + mod]; } private: std::vector<int64_t> rep_; }; constexpr int64_t kMod = 1'000'000'007; Bin binomial_mod_3(int n) { std::vector<int64_t> result; result.reserve(3 * n + 3); result.push_back(1); result.push_back(0); result.push_back(0); for (int i = 0; i < n; ++i) { result.push_back((result[3 * i] + result[3 * i + 2]) % kMod); result.push_back((result[3 * i + 1] + result[3 * i]) % kMod); result.push_back((result[3 * i + 2] + result[3 * i + 1]) % kMod); } return Bin(result); }; bool violates_zcz(char c, int idx) { return (c == 'Z' && idx % 2 == 1) || (c == 'C' && idx % 2 == 0); } bool violates_czc(char c, int idx) { return (c == 'C' && idx % 2 == 1) || (c == 'Z' && idx % 2 == 0); } int64_t count(const Bin &bin, const std::unordered_set<int> &violations_czc, const std::unordered_set<int> &violations_zcz, const std::unordered_map<char, int> &counts, int n) { //debug(violations_zcz, "zcz"); //debug(violations_czc, "czc"); int excluded_mod = (3 - (n % 3)) % 3; int starts_at_mod = counts.at('Z') % 3; int excluded_adjusted_mod = (3 + excluded_mod - starts_at_mod) % 3; int n_count = counts.at('N'); int64_t result = (kMod + (bin.get(n_count, 0) + bin.get(n_count, 1) + bin.get(n_count, 2)) % kMod - bin.get(n_count, excluded_adjusted_mod)) % kMod; // Edge cases bool violations_counted = n % 2 == 1 && n > 1; int64_t violations_to_subtract = violations_counted ? ((violations_czc.empty() ? 1 : 0) + (violations_zcz.empty() ? 1 : 0)) : 0; //debug(violations_counted, "violations_counted"); result = (kMod + result - violations_to_subtract) % kMod; return result; } int main() { prelude(); int n; int q; std::string str; std::cin >> n >> q; std::cin.ignore(); std::getline(std::cin, str); std::unordered_map<char, int> counts; counts['Z'] = counts['N'] = counts['C'] = 0; std::unordered_set<int> violations_czc; std::unordered_set<int> violations_zcz; for (int i = 0; i < str.length(); ++i) { counts[str[i]]++; if (violates_czc(str[i], i)) violations_czc.insert(i); if (violates_zcz(str[i], i)) violations_zcz.insert(i); } const auto bin = binomial_mod_3(n); std::cout << count(bin,violations_czc, violations_zcz, counts, n) << "\n"; while (q--) { int idx; char c; std::cin >> idx >> c; // Input 1-based. idx -= 1; //debug(idx, "idx"); //debug(c, "c"); char previous_c = str[idx]; str[idx] = c; counts[previous_c]--; counts[c]++; violations_czc.erase(idx); violations_zcz.erase(idx); if (violates_czc(c, idx)) violations_czc.insert(idx); if (violates_zcz(c, idx)) violations_zcz.insert(idx); std::cout << count(bin, violations_czc, violations_zcz, counts, n) << "\n"; } // On each iteration: // - update CZN, corner case sets and position // - determine mod3 of C/Z vs equal mod3 to calculate // - subtract for corner cases 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 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 150 151 152 153 154 155 156 157 158 159 | #include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::unordered_set<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } bool viable(int k, int n) { int red = k; int green = n - k; while (red > 1 || green > 1) { if (green > red) { using std::swap; swap(green, red); } // red >= green green += red / 2; red -= (red / 2) * 2; } return red == 0 || green == 0; } class Bin { public: Bin(std::vector<int64_t> rep) : rep_(std::move(rep)) {} int64_t get(int index, int mod) const { return rep_[3 * index + mod]; } private: std::vector<int64_t> rep_; }; constexpr int64_t kMod = 1'000'000'007; Bin binomial_mod_3(int n) { std::vector<int64_t> result; result.reserve(3 * n + 3); result.push_back(1); result.push_back(0); result.push_back(0); for (int i = 0; i < n; ++i) { result.push_back((result[3 * i] + result[3 * i + 2]) % kMod); result.push_back((result[3 * i + 1] + result[3 * i]) % kMod); result.push_back((result[3 * i + 2] + result[3 * i + 1]) % kMod); } return Bin(result); }; bool violates_zcz(char c, int idx) { return (c == 'Z' && idx % 2 == 1) || (c == 'C' && idx % 2 == 0); } bool violates_czc(char c, int idx) { return (c == 'C' && idx % 2 == 1) || (c == 'Z' && idx % 2 == 0); } int64_t count(const Bin &bin, const std::unordered_set<int> &violations_czc, const std::unordered_set<int> &violations_zcz, const std::unordered_map<char, int> &counts, int n) { //debug(violations_zcz, "zcz"); //debug(violations_czc, "czc"); int excluded_mod = (3 - (n % 3)) % 3; int starts_at_mod = counts.at('Z') % 3; int excluded_adjusted_mod = (3 + excluded_mod - starts_at_mod) % 3; int n_count = counts.at('N'); int64_t result = (kMod + (bin.get(n_count, 0) + bin.get(n_count, 1) + bin.get(n_count, 2)) % kMod - bin.get(n_count, excluded_adjusted_mod)) % kMod; // Edge cases bool violations_counted = n % 2 == 1 && n > 1; int64_t violations_to_subtract = violations_counted ? ((violations_czc.empty() ? 1 : 0) + (violations_zcz.empty() ? 1 : 0)) : 0; //debug(violations_counted, "violations_counted"); result = (kMod + result - violations_to_subtract) % kMod; return result; } int main() { prelude(); int n; int q; std::string str; std::cin >> n >> q; std::cin.ignore(); std::getline(std::cin, str); std::unordered_map<char, int> counts; counts['Z'] = counts['N'] = counts['C'] = 0; std::unordered_set<int> violations_czc; std::unordered_set<int> violations_zcz; for (int i = 0; i < str.length(); ++i) { counts[str[i]]++; if (violates_czc(str[i], i)) violations_czc.insert(i); if (violates_zcz(str[i], i)) violations_zcz.insert(i); } const auto bin = binomial_mod_3(n); std::cout << count(bin,violations_czc, violations_zcz, counts, n) << "\n"; while (q--) { int idx; char c; std::cin >> idx >> c; // Input 1-based. idx -= 1; //debug(idx, "idx"); //debug(c, "c"); char previous_c = str[idx]; str[idx] = c; counts[previous_c]--; counts[c]++; violations_czc.erase(idx); violations_zcz.erase(idx); if (violates_czc(c, idx)) violations_czc.insert(idx); if (violates_zcz(c, idx)) violations_zcz.insert(idx); std::cout << count(bin, violations_czc, violations_zcz, counts, n) << "\n"; } // On each iteration: // - update CZN, corner case sets and position // - determine mod3 of C/Z vs equal mod3 to calculate // - subtract for corner cases return 0; } |