#include<bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; vector<array<long long, 3>> goodSequence; //ile jest ciagów 01010111, z liczba 1 przystającą do danej liczby mod 3 long long goodCnt(int red, int green, int blue){ int x = red - green - blue; x %= 3; if(x < 0)x+=3; if(x==0){ return (goodSequence[blue][1] + goodSequence[blue][2]) % MOD; } if(x==1){ return (goodSequence[blue][0] + goodSequence[blue][2]) % MOD; } if(x==2){ return (goodSequence[blue][0] + goodSequence[blue][1]) % MOD; } return 0; } long long getRes(int red, int green, int blue, int diff1, int diff2, int n){ long long res = goodCnt(red, green, blue); if(n % 2 == 1){ if(diff1 == 0)res--; if(diff2 == 0)res--; } res %= MOD; if(res < 0)res += MOD; res %= MOD; return res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin>>n>>q; goodSequence.resize(n+1); goodSequence[0][0] = 1; goodSequence[0][1] = 0; goodSequence[0][2] = 0; for(int i = 1; i<= n; i++){ for(int j = 0; j < 3; j++){ goodSequence[i][j] = (goodSequence[i-1][j] + goodSequence[i-1][(j+2)%3]) % MOD; } } //na razie bez przypadku, że n jest nieparzyste i zablokowany układ początkowy string str; cin>>str; vector<int> color(n); for(int i= 0; i < n; i++){ if(str[i] == 'C')color[i] = 0; if(str[i] == 'Z')color[i] = 1; if(str[i] == 'N')color[i] = 2; } array<int, 3> colorCnt; for(int i= 0; i < 3; i++)colorCnt[i] = 0; for(int i= 0; i < n; i++){ colorCnt[color[i]]++; } int diff1 = 0; int diff2 = 0; for(int i= 0; i < n; i++){ if(color[i] == i%2){ diff1++; }else if(color[i] == (i+1)%2){ diff2++; } } cout<<getRes(colorCnt[0], colorCnt[1], colorCnt[2], diff1, diff2, n)<<"\n"; while(q--){ int k; string str; cin>>k>>str; k--; colorCnt[color[k]]--; if(color[k] == k % 2){ diff1--; }else if(color[k] == (k+1) % 2){ diff2--; } if(str == "C")color[k] = 0; if(str == "Z")color[k] = 1; if(str == "N")color[k] = 2; colorCnt[color[k]]++; if(color[k] == k % 2){ diff1++; }else if(color[k] == (k+1) % 2){ diff2++; } cout<<getRes(colorCnt[0], colorCnt[1], colorCnt[2], diff1, diff2, n)<<"\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 | #include<bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; vector<array<long long, 3>> goodSequence; //ile jest ciagów 01010111, z liczba 1 przystającą do danej liczby mod 3 long long goodCnt(int red, int green, int blue){ int x = red - green - blue; x %= 3; if(x < 0)x+=3; if(x==0){ return (goodSequence[blue][1] + goodSequence[blue][2]) % MOD; } if(x==1){ return (goodSequence[blue][0] + goodSequence[blue][2]) % MOD; } if(x==2){ return (goodSequence[blue][0] + goodSequence[blue][1]) % MOD; } return 0; } long long getRes(int red, int green, int blue, int diff1, int diff2, int n){ long long res = goodCnt(red, green, blue); if(n % 2 == 1){ if(diff1 == 0)res--; if(diff2 == 0)res--; } res %= MOD; if(res < 0)res += MOD; res %= MOD; return res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin>>n>>q; goodSequence.resize(n+1); goodSequence[0][0] = 1; goodSequence[0][1] = 0; goodSequence[0][2] = 0; for(int i = 1; i<= n; i++){ for(int j = 0; j < 3; j++){ goodSequence[i][j] = (goodSequence[i-1][j] + goodSequence[i-1][(j+2)%3]) % MOD; } } //na razie bez przypadku, że n jest nieparzyste i zablokowany układ początkowy string str; cin>>str; vector<int> color(n); for(int i= 0; i < n; i++){ if(str[i] == 'C')color[i] = 0; if(str[i] == 'Z')color[i] = 1; if(str[i] == 'N')color[i] = 2; } array<int, 3> colorCnt; for(int i= 0; i < 3; i++)colorCnt[i] = 0; for(int i= 0; i < n; i++){ colorCnt[color[i]]++; } int diff1 = 0; int diff2 = 0; for(int i= 0; i < n; i++){ if(color[i] == i%2){ diff1++; }else if(color[i] == (i+1)%2){ diff2++; } } cout<<getRes(colorCnt[0], colorCnt[1], colorCnt[2], diff1, diff2, n)<<"\n"; while(q--){ int k; string str; cin>>k>>str; k--; colorCnt[color[k]]--; if(color[k] == k % 2){ diff1--; }else if(color[k] == (k+1) % 2){ diff2--; } if(str == "C")color[k] = 0; if(str == "Z")color[k] = 1; if(str == "N")color[k] = 2; colorCnt[color[k]]++; if(color[k] == k % 2){ diff1++; }else if(color[k] == (k+1) % 2){ diff2++; } cout<<getRes(colorCnt[0], colorCnt[1], colorCnt[2], diff1, diff2, n)<<"\n"; } return 0; } |