#include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <chrono> #include <functional> #include <cstdlib> using namespace std; const unsigned long long P = 1000000007; const unsigned long long MAX_N = 200001; inline char getLetterOfForbiddenLetterSequence1(int position) { if (position % 2 == 0) return 'C'; else return 'Z'; } inline char getLetterOfForbiddenLetterSequence2(int position) { if (position % 2 == 0) return 'Z'; else return 'C'; } struct SequenceDescription { int size = 0; int redCount = 0; int blueCount = 0; int greenCount = 0; int numberMatches1 = 0; int numberMatches2 = 0; void reportTermion(char termion, int position) { ++size; if (termion == 'C') ++redCount; if (termion == 'Z') ++greenCount; if (termion == 'N') ++blueCount; if (getLetterOfForbiddenLetterSequence1(position) == termion) ++numberMatches1; if (getLetterOfForbiddenLetterSequence2(position) == termion) ++numberMatches2; } void unreportTermion(char termion, int position) { --size; if (termion == 'C') --redCount; if (termion == 'Z') --greenCount; if (termion == 'N') --blueCount; if (getLetterOfForbiddenLetterSequence1(position) == termion) --numberMatches1; if (getLetterOfForbiddenLetterSequence2(position) == termion) --numberMatches2; } }; class Newton { std::vector<unsigned long long> k0; std::vector<unsigned long long> k1; std::vector<unsigned long long> k2; public: Newton(): k0(MAX_N, 0), k1(MAX_N, 0), k2(MAX_N, 0) { k0[0] = 1; for (int i = 1; i < MAX_N; ++i) { k0[i] = (k0[i-1] + k2[i-1]) % P; k1[i] = (k0[i-1] + k1[i-1]) % P; k2[i] = (k1[i-1] + k2[i-1]) % P; } } unsigned long long getValue(int n, int k) { if (k == 0) return k0[n]; if (k == 1) return k1[n]; return k2[n]; } }; unsigned long long getNumberOfCombinations(SequenceDescription & desc, Newton & newton) { int diff = std::abs(desc.greenCount - desc.redCount); unsigned long long result = 0; for (int i = 0; i < 3; ++i) { int newDiff = diff + (desc.blueCount - 2*i); if (std::abs(newDiff) % 3 != 0) result = (result + newton.getValue(desc.blueCount, i)) % P; } if (desc.size % 2 == 1) { if (desc.numberMatches1 == (desc.greenCount + desc.redCount)) result = (result + P - 1) % P; if (desc.numberMatches2 == (desc.greenCount + desc.redCount)) result = (result + P - 1) % P; } return result; } int main() { const clock_t begin_time = clock(); ios::sync_with_stdio(false); Newton newton; int termionCount; std::cin >> termionCount; int changeCount; std::cin >> changeCount; std::string termions; std::cin >> termions; SequenceDescription desc; for (int i = 0; i < termions.size(); ++i) { desc.reportTermion(termions[i], i); } std::cout << getNumberOfCombinations(desc, newton) << "\n"; for (int i = 0; i < changeCount; ++i) { int pos; std::cin >> pos; --pos; std::string singleTermion; std::cin >> singleTermion; desc.unreportTermion(termions[pos], pos); termions[pos] = singleTermion[0]; desc.reportTermion(singleTermion[0], pos); unsigned long long numberOfCombinations = getNumberOfCombinations(desc, newton); std::cout << numberOfCombinations << "\n"; } //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\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 | #include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <chrono> #include <functional> #include <cstdlib> using namespace std; const unsigned long long P = 1000000007; const unsigned long long MAX_N = 200001; inline char getLetterOfForbiddenLetterSequence1(int position) { if (position % 2 == 0) return 'C'; else return 'Z'; } inline char getLetterOfForbiddenLetterSequence2(int position) { if (position % 2 == 0) return 'Z'; else return 'C'; } struct SequenceDescription { int size = 0; int redCount = 0; int blueCount = 0; int greenCount = 0; int numberMatches1 = 0; int numberMatches2 = 0; void reportTermion(char termion, int position) { ++size; if (termion == 'C') ++redCount; if (termion == 'Z') ++greenCount; if (termion == 'N') ++blueCount; if (getLetterOfForbiddenLetterSequence1(position) == termion) ++numberMatches1; if (getLetterOfForbiddenLetterSequence2(position) == termion) ++numberMatches2; } void unreportTermion(char termion, int position) { --size; if (termion == 'C') --redCount; if (termion == 'Z') --greenCount; if (termion == 'N') --blueCount; if (getLetterOfForbiddenLetterSequence1(position) == termion) --numberMatches1; if (getLetterOfForbiddenLetterSequence2(position) == termion) --numberMatches2; } }; class Newton { std::vector<unsigned long long> k0; std::vector<unsigned long long> k1; std::vector<unsigned long long> k2; public: Newton(): k0(MAX_N, 0), k1(MAX_N, 0), k2(MAX_N, 0) { k0[0] = 1; for (int i = 1; i < MAX_N; ++i) { k0[i] = (k0[i-1] + k2[i-1]) % P; k1[i] = (k0[i-1] + k1[i-1]) % P; k2[i] = (k1[i-1] + k2[i-1]) % P; } } unsigned long long getValue(int n, int k) { if (k == 0) return k0[n]; if (k == 1) return k1[n]; return k2[n]; } }; unsigned long long getNumberOfCombinations(SequenceDescription & desc, Newton & newton) { int diff = std::abs(desc.greenCount - desc.redCount); unsigned long long result = 0; for (int i = 0; i < 3; ++i) { int newDiff = diff + (desc.blueCount - 2*i); if (std::abs(newDiff) % 3 != 0) result = (result + newton.getValue(desc.blueCount, i)) % P; } if (desc.size % 2 == 1) { if (desc.numberMatches1 == (desc.greenCount + desc.redCount)) result = (result + P - 1) % P; if (desc.numberMatches2 == (desc.greenCount + desc.redCount)) result = (result + P - 1) % P; } return result; } int main() { const clock_t begin_time = clock(); ios::sync_with_stdio(false); Newton newton; int termionCount; std::cin >> termionCount; int changeCount; std::cin >> changeCount; std::string termions; std::cin >> termions; SequenceDescription desc; for (int i = 0; i < termions.size(); ++i) { desc.reportTermion(termions[i], i); } std::cout << getNumberOfCombinations(desc, newton) << "\n"; for (int i = 0; i < changeCount; ++i) { int pos; std::cin >> pos; --pos; std::string singleTermion; std::cin >> singleTermion; desc.unreportTermion(termions[pos], pos); termions[pos] = singleTermion[0]; desc.reportTermion(singleTermion[0], pos); unsigned long long numberOfCombinations = getNumberOfCombinations(desc, newton); std::cout << numberOfCombinations << "\n"; } //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n"; return 0; }; |