#include <bits/stdc++.h> constexpr long long MOD = 1'000'000'007; long long N, Q, BLUE, RED, GREEN, N_BLUE, N_RED, N_GREEN, P_BLUE, P_RED, P_GREEN; std::string S; long long INV_3 = 333'333'336; long long POW_2[200'005]; void make_pow_2(){ POW_2[0] = 1; for (int i = 1; i < 200'005; ++i) POW_2[i] = (POW_2[i - 1] * 2) % MOD; } long long F(){ long long output = POW_2[BLUE]; if ((N + RED) % 3 == 0){ if (BLUE % 6 == 0) output -= ((POW_2[BLUE] + 2) * INV_3) % MOD; else if (BLUE % 6 == 1) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 2) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 3) output -= ((POW_2[BLUE] - 2) * INV_3) % MOD; else if (BLUE % 6 == 4) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 5) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; } else if ((N + RED) % 3 == 1){ if (BLUE % 6 == 0) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 1) output -= ((POW_2[BLUE] - 2) * INV_3) % MOD; else if (BLUE % 6 == 2) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 3) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 4) output -= ((POW_2[BLUE] + 2) * INV_3) % MOD; else if (BLUE % 6 == 5) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; } else{ if (BLUE % 6 == 0) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 1) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 2) output -= ((POW_2[BLUE] + 2) * INV_3) % MOD; else if (BLUE % 6 == 3) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 4) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 5) output -= ((POW_2[BLUE] - 2) * INV_3) % MOD; } if (N % 2 == 1){ if ((N_GREEN == 0 && N_BLUE > 0 && N_RED == RED) || (P_GREEN == 0 && P_BLUE > 0 && P_RED == RED)) output += MOD - 1; } return (output + 3LL * MOD) % MOD; } int main(){ std::ios_base::sync_with_stdio(NULL); std::cin.tie(nullptr); std::cout.tie(nullptr); make_pow_2(); std::cin >> N >> Q >> S; for (int i = 0; i < N; ++i){ if (S[i] == 'N') ++BLUE; else if (S[i] == 'C') ++RED; else if (S[i] == 'Z') ++GREEN; if (i % 2) { if (S[i] == 'N') ++N_BLUE; else if (S[i] == 'C') ++N_RED; else if (S[i] == 'Z') ++N_GREEN; } else { if (S[i] == 'N') ++P_BLUE; else if (S[i] == 'C') ++P_RED; else if (S[i] == 'Z') ++P_GREEN; } } std::cout << F() << '\n'; for (int i = 0; i < Q; ++i){ int M; char C; std::cin >> M >> C; if (C == 'N') ++BLUE; else if (C == 'C') ++RED; else if (C == 'Z') ++GREEN; if ((M - 1) % 2){ if (C == 'N') ++N_BLUE; else if (C == 'C') ++N_RED; else if (C == 'Z') ++N_GREEN; } else{ if (C == 'N') ++P_BLUE; else if (C == 'C') ++P_RED; else if (C == 'Z') ++P_GREEN; } if (S[M - 1] == 'N') --BLUE; else if (S[M - 1] == 'C') --RED; else if (S[M - 1] == 'Z') --GREEN; if ((M - 1) % 2){ if (S[M - 1] == 'N') --N_BLUE; else if (S[M - 1] == 'C') --N_RED; else if (S[M - 1] == 'Z') --N_GREEN; } else{ if (S[M - 1] == 'N') --P_BLUE; else if (S[M - 1] == 'C') --P_RED; else if (S[M - 1] == 'Z') --P_GREEN; } S[M - 1] = C; std::cout << F() << '\n'; } 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 160 161 | #include <bits/stdc++.h> constexpr long long MOD = 1'000'000'007; long long N, Q, BLUE, RED, GREEN, N_BLUE, N_RED, N_GREEN, P_BLUE, P_RED, P_GREEN; std::string S; long long INV_3 = 333'333'336; long long POW_2[200'005]; void make_pow_2(){ POW_2[0] = 1; for (int i = 1; i < 200'005; ++i) POW_2[i] = (POW_2[i - 1] * 2) % MOD; } long long F(){ long long output = POW_2[BLUE]; if ((N + RED) % 3 == 0){ if (BLUE % 6 == 0) output -= ((POW_2[BLUE] + 2) * INV_3) % MOD; else if (BLUE % 6 == 1) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 2) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 3) output -= ((POW_2[BLUE] - 2) * INV_3) % MOD; else if (BLUE % 6 == 4) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 5) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; } else if ((N + RED) % 3 == 1){ if (BLUE % 6 == 0) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 1) output -= ((POW_2[BLUE] - 2) * INV_3) % MOD; else if (BLUE % 6 == 2) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 3) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 4) output -= ((POW_2[BLUE] + 2) * INV_3) % MOD; else if (BLUE % 6 == 5) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; } else{ if (BLUE % 6 == 0) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 1) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 2) output -= ((POW_2[BLUE] + 2) * INV_3) % MOD; else if (BLUE % 6 == 3) output -= ((POW_2[BLUE] + 1) * INV_3) % MOD; else if (BLUE % 6 == 4) output -= ((POW_2[BLUE] - 1) * INV_3) % MOD; else if (BLUE % 6 == 5) output -= ((POW_2[BLUE] - 2) * INV_3) % MOD; } if (N % 2 == 1){ if ((N_GREEN == 0 && N_BLUE > 0 && N_RED == RED) || (P_GREEN == 0 && P_BLUE > 0 && P_RED == RED)) output += MOD - 1; } return (output + 3LL * MOD) % MOD; } int main(){ std::ios_base::sync_with_stdio(NULL); std::cin.tie(nullptr); std::cout.tie(nullptr); make_pow_2(); std::cin >> N >> Q >> S; for (int i = 0; i < N; ++i){ if (S[i] == 'N') ++BLUE; else if (S[i] == 'C') ++RED; else if (S[i] == 'Z') ++GREEN; if (i % 2) { if (S[i] == 'N') ++N_BLUE; else if (S[i] == 'C') ++N_RED; else if (S[i] == 'Z') ++N_GREEN; } else { if (S[i] == 'N') ++P_BLUE; else if (S[i] == 'C') ++P_RED; else if (S[i] == 'Z') ++P_GREEN; } } std::cout << F() << '\n'; for (int i = 0; i < Q; ++i){ int M; char C; std::cin >> M >> C; if (C == 'N') ++BLUE; else if (C == 'C') ++RED; else if (C == 'Z') ++GREEN; if ((M - 1) % 2){ if (C == 'N') ++N_BLUE; else if (C == 'C') ++N_RED; else if (C == 'Z') ++N_GREEN; } else{ if (C == 'N') ++P_BLUE; else if (C == 'C') ++P_RED; else if (C == 'Z') ++P_GREEN; } if (S[M - 1] == 'N') --BLUE; else if (S[M - 1] == 'C') --RED; else if (S[M - 1] == 'Z') --GREEN; if ((M - 1) % 2){ if (S[M - 1] == 'N') --N_BLUE; else if (S[M - 1] == 'C') --N_RED; else if (S[M - 1] == 'Z') --N_GREEN; } else{ if (S[M - 1] == 'N') --P_BLUE; else if (S[M - 1] == 'C') --P_RED; else if (S[M - 1] == 'Z') --P_GREEN; } S[M - 1] = C; std::cout << F() << '\n'; } return 0; } |