A coding problem. The trick is, some part of computation can be reused from last iteration. Check out my code:
#include <cmath> #include <cstdio> #include <vector> #include <bitset> #include <map> #include <set> #include <string> #include <iostream> #include <algorithm> using namespace std; #define MAX_CNT 2000002 int main() { int n, k; cin >> n >> k; std::bitset<MAX_CNT> b; string in; getline(cin, in); getline(cin, in); size_t len = in.length(); for (int i = 0; i < len; i++) b[i] = in[len - i - 1] - '0'; std::bitset<MAX_CNT> r; string rs; unsigned char lstXor = 0; for (int i = 0; i < n; i++) { size_t cnt = std::min(k - 1, i); unsigned char c = b[i]; if (i > 0) lstXor ^= r[i - 1]; if (i >= k) lstXor ^= r[i - k]; r[i] = c ^ lstXor; rs += r[i] ? '1' : '0'; } std::reverse(rs.begin(), rs.end()); cout << rs << endl; return 0; }