#include <vector> #include <iostream> #include <sstream> #include <math.h> #include <sys/time.h> #include <cstdlib> #include <algorithm> #include <cassert> #include <cstring> #include <fstream> #include <set> #include <map> #include <iomanip> #include <queue> #define FOR(i,a,b) for(__typeof(b) i=(a);i<(b);++i) #define REP(i,a) FOR(i,0,a) #define FOREACH(x,c) for(__typeof(c.begin()) x=c.begin();x != c.end(); x++) #define ALL(c) c.begin(),c.end() #define CLEAR(c) memset(c,0,sizeof(c)) #define SIZE(c) (int) ((c).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define ULL unsigned long long #define LL long long #define LD long double #define II pair<int, int> #define DD pair<double, double> #define FF pair<float,float> #define VC vector #define VI VC<int> #define VVI VC<VI> #define VVVI VC<VVI> #define VD VC<double> #define VS VC<string> #define VII VC<II> #define VDD VC<DD> #define VFF VC<FF> #define VVD VC<VD> #define VVVD VC<VVD> #define VULL VC<ULL> #define DB(a) cerr << #a << ": " << a << endl; using namespace std; template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";} template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); } VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;} int MOD = 1000000007; int main(int argc, char *argv[]){ int N, Q; cin >> N >> Q; string s; cin >> s; // binom int bin[N+1][3]; bin[0][0] = 1; bin[0][1] = 0; bin[0][2] = 0; REP(i,N){ bin[i+1][0] = (bin[i][1] + bin[i][2])%MOD; bin[i+1][1] = (bin[i][0] + bin[i][2])%MOD; bin[i+1][2] = (bin[i][0] + bin[i][1])%MOD; } int R[2] = {0,0}, G[2] = {0,0}; REP(i,N){ if (s[i] == 'C') R[i%2]++; else if (s[i] == 'Z') G[i%2]++; } int cnt = 0; REP(i,Q+1){ int diff = (3*N + R[0] + R[1] - G[0] - G[1])%3; int B = N-R[0]-R[1]-G[0]-G[1]; //cout << "FSDFSD " << B << " " << diff << endl; cnt = (bin[B][(diff+1)%3] + bin[B][(diff+2)%3])%MOD; if (N%2 != 0){ cnt -= 2; if (R[0] != 0 || G[1] != 0) cnt++; if (R[1] != 0 || G[0] != 0) cnt++; } cnt = (cnt+MOD)%MOD; cout << cnt << endl; if (i == Q) break; int pos; char c; cin >> pos >> c; pos--; if (s[pos] == 'C') R[pos%2]--; else if (s[pos] == 'Z') G[pos%2]--; s[pos] = c; if (s[pos] == 'C') R[pos%2]++; else if (s[pos] == 'Z') G[pos%2]++; } 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 | #include <vector> #include <iostream> #include <sstream> #include <math.h> #include <sys/time.h> #include <cstdlib> #include <algorithm> #include <cassert> #include <cstring> #include <fstream> #include <set> #include <map> #include <iomanip> #include <queue> #define FOR(i,a,b) for(__typeof(b) i=(a);i<(b);++i) #define REP(i,a) FOR(i,0,a) #define FOREACH(x,c) for(__typeof(c.begin()) x=c.begin();x != c.end(); x++) #define ALL(c) c.begin(),c.end() #define CLEAR(c) memset(c,0,sizeof(c)) #define SIZE(c) (int) ((c).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define ULL unsigned long long #define LL long long #define LD long double #define II pair<int, int> #define DD pair<double, double> #define FF pair<float,float> #define VC vector #define VI VC<int> #define VVI VC<VI> #define VVVI VC<VVI> #define VD VC<double> #define VS VC<string> #define VII VC<II> #define VDD VC<DD> #define VFF VC<FF> #define VVD VC<VD> #define VVVD VC<VVD> #define VULL VC<ULL> #define DB(a) cerr << #a << ": " << a << endl; using namespace std; template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";} template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); } VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;} int MOD = 1000000007; int main(int argc, char *argv[]){ int N, Q; cin >> N >> Q; string s; cin >> s; // binom int bin[N+1][3]; bin[0][0] = 1; bin[0][1] = 0; bin[0][2] = 0; REP(i,N){ bin[i+1][0] = (bin[i][1] + bin[i][2])%MOD; bin[i+1][1] = (bin[i][0] + bin[i][2])%MOD; bin[i+1][2] = (bin[i][0] + bin[i][1])%MOD; } int R[2] = {0,0}, G[2] = {0,0}; REP(i,N){ if (s[i] == 'C') R[i%2]++; else if (s[i] == 'Z') G[i%2]++; } int cnt = 0; REP(i,Q+1){ int diff = (3*N + R[0] + R[1] - G[0] - G[1])%3; int B = N-R[0]-R[1]-G[0]-G[1]; //cout << "FSDFSD " << B << " " << diff << endl; cnt = (bin[B][(diff+1)%3] + bin[B][(diff+2)%3])%MOD; if (N%2 != 0){ cnt -= 2; if (R[0] != 0 || G[1] != 0) cnt++; if (R[1] != 0 || G[0] != 0) cnt++; } cnt = (cnt+MOD)%MOD; cout << cnt << endl; if (i == Q) break; int pos; char c; cin >> pos >> c; pos--; if (s[pos] == 'C') R[pos%2]--; else if (s[pos] == 'Z') G[pos%2]--; s[pos] = c; if (s[pos] == 'C') R[pos%2]++; else if (s[pos] == 'Z') G[pos%2]++; } return 0; } |