fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> circle_of_life(vector<int>& state, long long T) {
  6. int N = state.size();
  7. vector<int> result = state;
  8. vector<int> temp(N);
  9.  
  10. for (long long step = 1; T > 0; step <<= 1) {
  11. if (T & step) {
  12. for (int i = 0; i < N; i++) {
  13. temp[i] = result[(i - step + N) % N] ^ result[(i + step) % N];
  14. }
  15. result = temp;
  16. T -= step;
  17. }
  18. }
  19. return result;
  20. }
  21.  
  22. int main() {
  23. int N;
  24. long long T;
  25. cin >> N >> T;
  26. string initialState;
  27. cin >> initialState;
  28.  
  29. vector<int> state(N);
  30. for (int i = 0; i < N; i++) {
  31. state[i] = initialState[i] - '0';
  32. }
  33.  
  34. vector<int> finalState = circle_of_life(state, T);
  35.  
  36. for (int cell : finalState) {
  37. cout << cell;
  38. }
  39. cout << endl;
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 5288KB
stdin
5 3
01011
stdout
10100