#include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //Jakub Misiaszek //szablon czesciowo skopiowany z profilu https://codeforces.com/profile/Geothermal using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> pii; typedef pair<LL,LL> pll; typedef pair<LD,LD> pdd; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<LD> vld; typedef vector<LL> vll; typedef vector<pii> vpii; typedef vector<pll> vpll; template<class T> using pq = priority_queue<T>; template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define rep(i, a, b) for (int i=a; i<(b); i++) #define repd(i,a,b) for (int i = (a); i >= b; i--) #define sz(x) (int)(x).size() #define pb push_back #define st first #define nd second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define memo(x) memset(x, 0, sizeof(x)) #define debug(x) cerr << x << " " mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); LL losuj(LL a, LL b){ return a+rng()%(b-a+1);} const int mod = 1e9 + 7; void solve(){ int N, q; cin >> N >> q; int z = 0, c = 0, n = 0; string s; cin >> s; int zgodz = 0, zgodc = 0; for(int i = 0; i < N; i++){ if(s[i] == 'N') n++; else if(s[i] == 'C'){ c++; if(i%2==1){ //cout << i << "\n"; zgodz++; } else zgodc++; } else{ z++; if(i%2==1){ //cout << i << "\n"; zgodc++; } else zgodz++; } } //cout << n << " " << c << " " << z << "\n"; //cout << zgodz << " " << zgodc << "\n\n"; vector<vll> dp(N+1, vll(3,0)); dp[0][0] = 1; for(int i = 1; i <= N; i++){ dp[i][0] = (dp[i-1][1] + dp[i-1][2])%mod; dp[i][1] = (dp[i-1][0] + dp[i-1][2])%mod; dp[i][2] = (dp[i-1][0] + dp[i-1][1])%mod; //cout << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << "\n"; } LL diff = (c - z)%3; //cout << (-diff)%3 << "\n"; LL ans = (dp[n][0] + dp[n][1] + dp[n][2] - dp[n][(-diff + 3)%3]); if(n%2 == 1){ if(zgodz + n == N) ans = ((ans - 1)%mod + mod)%mod; if(zgodc + n == N) ans = ((ans - 1)%mod + mod)%mod; } cout << ans << "\n"; while(q--){ int x; cin >> x; char C; cin >> C; x--; if(s[x] == 'N') n--; else if(s[x] == 'C'){ c--; if(x%2==1) zgodz--; else zgodc--; } else{ z--; if(x%2==1) zgodc--; else zgodz--; } s[x] = C; if(s[x] == 'N') n++; else if(s[x] == 'C'){ c++; if(x%2==1) zgodz ++; else zgodc++; } else{ z++; if(x%2==1) zgodc++; else zgodz++; } //cout << n << " " << c << " " << z << "\n"; //cout << zgodz << " " << zgodc << "\n\n"; diff = (c - z)%3; ans = (dp[n][0] + dp[n][1] + dp[n][2] - dp[n][(-diff + 3)%3])%mod; if(n%2 == 1){ if(zgodz + n == N) ans = ((ans - 1)%mod + mod)%mod; if(zgodc + n == N) ans = ((ans - 1)%mod + mod)%mod; } cout << ans << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while (t--) solve(); 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 | #include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //Jakub Misiaszek //szablon czesciowo skopiowany z profilu https://codeforces.com/profile/Geothermal using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> pii; typedef pair<LL,LL> pll; typedef pair<LD,LD> pdd; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<LD> vld; typedef vector<LL> vll; typedef vector<pii> vpii; typedef vector<pll> vpll; template<class T> using pq = priority_queue<T>; template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define rep(i, a, b) for (int i=a; i<(b); i++) #define repd(i,a,b) for (int i = (a); i >= b; i--) #define sz(x) (int)(x).size() #define pb push_back #define st first #define nd second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define memo(x) memset(x, 0, sizeof(x)) #define debug(x) cerr << x << " " mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); LL losuj(LL a, LL b){ return a+rng()%(b-a+1);} const int mod = 1e9 + 7; void solve(){ int N, q; cin >> N >> q; int z = 0, c = 0, n = 0; string s; cin >> s; int zgodz = 0, zgodc = 0; for(int i = 0; i < N; i++){ if(s[i] == 'N') n++; else if(s[i] == 'C'){ c++; if(i%2==1){ //cout << i << "\n"; zgodz++; } else zgodc++; } else{ z++; if(i%2==1){ //cout << i << "\n"; zgodc++; } else zgodz++; } } //cout << n << " " << c << " " << z << "\n"; //cout << zgodz << " " << zgodc << "\n\n"; vector<vll> dp(N+1, vll(3,0)); dp[0][0] = 1; for(int i = 1; i <= N; i++){ dp[i][0] = (dp[i-1][1] + dp[i-1][2])%mod; dp[i][1] = (dp[i-1][0] + dp[i-1][2])%mod; dp[i][2] = (dp[i-1][0] + dp[i-1][1])%mod; //cout << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << "\n"; } LL diff = (c - z)%3; //cout << (-diff)%3 << "\n"; LL ans = (dp[n][0] + dp[n][1] + dp[n][2] - dp[n][(-diff + 3)%3]); if(n%2 == 1){ if(zgodz + n == N) ans = ((ans - 1)%mod + mod)%mod; if(zgodc + n == N) ans = ((ans - 1)%mod + mod)%mod; } cout << ans << "\n"; while(q--){ int x; cin >> x; char C; cin >> C; x--; if(s[x] == 'N') n--; else if(s[x] == 'C'){ c--; if(x%2==1) zgodz--; else zgodc--; } else{ z--; if(x%2==1) zgodc--; else zgodz--; } s[x] = C; if(s[x] == 'N') n++; else if(s[x] == 'C'){ c++; if(x%2==1) zgodz ++; else zgodc++; } else{ z++; if(x%2==1) zgodc++; else zgodz++; } //cout << n << " " << c << " " << z << "\n"; //cout << zgodz << " " << zgodc << "\n\n"; diff = (c - z)%3; ans = (dp[n][0] + dp[n][1] + dp[n][2] - dp[n][(-diff + 3)%3])%mod; if(n%2 == 1){ if(zgodz + n == N) ans = ((ans - 1)%mod + mod)%mod; if(zgodc + n == N) ans = ((ans - 1)%mod + mod)%mod; } cout << ans << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while (t--) solve(); return 0; } |