#include <bits/stdc++.h> using namespace std; constexpr long MOD = 1e9 + 7; int length, updates, redNumber, greenNumber, blueNumber, oddRedNumber, oddGreenNumber; char* particles; int diff[2][3][3] = {1, 0, 0, 0, 0, 1, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0}; int* pascal = new int[200000]; void countColors(int index) { if (particles[index] == 'N') { blueNumber++; } if (particles[index] == 'Z') { greenNumber++; if (index % 2 == 1) { oddGreenNumber++; } } if (particles[index] == 'C') { redNumber++; if (index % 2 == 1) { oddRedNumber++; } } } int getAlternatingNumber() { if (length % 2 == 0) { return 0; } if (length < 2) { return 0; } if (length == blueNumber) { return 2; } if ((oddGreenNumber == 0 && redNumber == oddRedNumber) || (oddRedNumber == 0 && greenNumber == oddGreenNumber)) { return 1; } return 0; } long twoToPower(int number) { long result = 1; for (int i = 0; i < number; ++i) { result = (result * 2) % MOD; } return result; } long plusMod(long x, long y) { return (MOD + x + y) % MOD; } long divideByThree(long x) { return (x * 333333336) % MOD; } int newRed() { return (3 - ((2*redNumber + blueNumber + greenNumber) % 3)) % 3; } int getSubsetsNumber(int xxx, int yyy) { return (int) plusMod(pascal[xxx-1], -diff[xxx % 2][xxx % 3][yyy]); } void compute() { int alternatingNumber = getAlternatingNumber(); int subsetsNumber = getSubsetsNumber(blueNumber, newRed()); int result = subsetsNumber - alternatingNumber; cout << result << endl; } void update() { int index; cin >> index; index--; if (particles[index] == 'C') { redNumber--; if (index % 2 == 1) { oddRedNumber--; } } if (particles[index] == 'Z') { greenNumber--; if (index % 2 == 1) { oddGreenNumber--; } } if (particles[index] == 'N') { blueNumber--; } cin >> particles[index]; countColors(index); } void computePascal() { long result = 1; long power = 2; bool plusOne = false; pascal[0] = 1; for (int i = 1; i <= 200000; ++i) { power = (power * 2) % MOD; result = (2 * result + (plusOne ? 1 : -1)) % MOD; plusOne = !plusOne; pascal[i] = (int)((MOD + power - result) % MOD); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); computePascal(); cin >> length >> updates; particles = new char[length]; for (int i = 0; i < length; ++i) { cin >> particles[i]; countColors(i); } compute(); for (int i = 0; i < updates; ++i) { update(); compute(); } 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 | #include <bits/stdc++.h> using namespace std; constexpr long MOD = 1e9 + 7; int length, updates, redNumber, greenNumber, blueNumber, oddRedNumber, oddGreenNumber; char* particles; int diff[2][3][3] = {1, 0, 0, 0, 0, 1, 0, 1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0}; int* pascal = new int[200000]; void countColors(int index) { if (particles[index] == 'N') { blueNumber++; } if (particles[index] == 'Z') { greenNumber++; if (index % 2 == 1) { oddGreenNumber++; } } if (particles[index] == 'C') { redNumber++; if (index % 2 == 1) { oddRedNumber++; } } } int getAlternatingNumber() { if (length % 2 == 0) { return 0; } if (length < 2) { return 0; } if (length == blueNumber) { return 2; } if ((oddGreenNumber == 0 && redNumber == oddRedNumber) || (oddRedNumber == 0 && greenNumber == oddGreenNumber)) { return 1; } return 0; } long twoToPower(int number) { long result = 1; for (int i = 0; i < number; ++i) { result = (result * 2) % MOD; } return result; } long plusMod(long x, long y) { return (MOD + x + y) % MOD; } long divideByThree(long x) { return (x * 333333336) % MOD; } int newRed() { return (3 - ((2*redNumber + blueNumber + greenNumber) % 3)) % 3; } int getSubsetsNumber(int xxx, int yyy) { return (int) plusMod(pascal[xxx-1], -diff[xxx % 2][xxx % 3][yyy]); } void compute() { int alternatingNumber = getAlternatingNumber(); int subsetsNumber = getSubsetsNumber(blueNumber, newRed()); int result = subsetsNumber - alternatingNumber; cout << result << endl; } void update() { int index; cin >> index; index--; if (particles[index] == 'C') { redNumber--; if (index % 2 == 1) { oddRedNumber--; } } if (particles[index] == 'Z') { greenNumber--; if (index % 2 == 1) { oddGreenNumber--; } } if (particles[index] == 'N') { blueNumber--; } cin >> particles[index]; countColors(index); } void computePascal() { long result = 1; long power = 2; bool plusOne = false; pascal[0] = 1; for (int i = 1; i <= 200000; ++i) { power = (power * 2) % MOD; result = (2 * result + (plusOne ? 1 : -1)) % MOD; plusOne = !plusOne; pascal[i] = (int)((MOD + power - result) % MOD); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); computePascal(); cin >> length >> updates; particles = new char[length]; for (int i = 0; i < length; ++i) { cin >> particles[i]; countColors(i); } compute(); for (int i = 0; i < updates; ++i) { update(); compute(); } return 0; } |