Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

char T[550005];
int salsa2[550005];
int salsaNext[550005];
int p[550005];

// Based on editorial solution
int D[13]
     [550005]; // one digit at a time, with digit 0 representing the ones place
int W[13][550005];
int lastUpdated[13][550005];

int N;

int salsa(int x) {
  int temp = x;
  int pow3 = 1;
  int res = 0;
  for (int i = 0; i < N; i++) {
    int k = temp % 3;
    temp /= 3;

    if (k == 1) {
      res += pow3 * 2;
    } else if (k == 2) {
      res += pow3;
    }
    pow3 *= 3;
  }
  return res;
}

void update(int n, int indx, int cntS) {
  if ((cntS & 1) != (lastUpdated[n][indx] & 1)) {
    // conduct salsa
    if (D[n][indx] == 1) {
      D[n][indx] = 2;
    } else if (D[n][indx] == 2) {
      D[n][indx] = 1;
    }
  }
  // updated the lastUpdated index
  lastUpdated[n][indx] = cntS;
}

int digits[15];
void printBase3(int x, int N) {
  for (int i = 0; i < N; i++) {
    digits[i] = x % 3;
    x /= 3;
  }
  for (int i = N - 1; i >= 0; i--) {
    printf("%d", digits[i]);
  }
}

int pow3[16];

int main() {
  scanf("%d", &N);

  scanf(" %s", T);

  int M = strlen(T);

  pow3[0] = 1;
  for (int i = 1; i <= N; i++) {
    pow3[i] = pow3[i - 1] * 3;
  }

  int pow3N = pow3[N];

  for (int i = 0; i < pow3N; i++) {
    salsa2[i] = salsa(i);
    // printf("salsa2[%d]=%d\\n", i, salsa2[i]);
  }

  // printf("ok");
  memset(W, 0, sizeof(W));
  for (int n = 1; n <= N; n++) {
    // printf("ok");
    for (int j = 0; j < pow3[N]; j++) {
      D[n][j] = (j % pow3[n]) / pow3[n - 1];
      // printf("D[%d][%d]=%d\\n", n, j, D[n][j]);
    }
    W[n][0] = pow3[n] - 1;
  }

  memset(lastUpdated, 0, sizeof(lastUpdated));
  int cntS2 = 0;
  for (int n = 1; n <= N; n++) {
    int cntS = 0;
    for (int i = 0; i < M; i++) {
      int indx0 = W[n - 1][i];
      int indx1 = (W[n - 1][i] + pow3[n - 1]) % pow3[n];
      int indx2 = (W[n - 1][i] + 2 * pow3[n - 1]) % pow3[n];
      // printf("W(%d, %d): %d %d %d cntS=%d\\n", n, i, indx0, indx1, indx2,
      // cntS);

      /*if(n == 0){
          if(T[i] == 'S'){
              W[n][i+1] = salsa2[W[n][i]];
          }else if(T[i] == 'R'){
              int temp = (W[n][i]-1);
              if(temp < 0){temp += pow3[n+1];}
              if(temp >= pow3[n+1]){temp -= pow3[n+1];}
              W[n][i+1] = temp;
          }
      }*/

      update(n, indx0, cntS + (T[i] == 'S'));
      update(n, indx1, cntS + (T[i] == 'S'));
      update(n, indx2, cntS + (T[i] == 'S'));
      if (D[n][indx0] == 2) {
        W[n][i] = indx0;
      } else if (D[n][indx1] == 2) {
        W[n][i] = indx1;
      } else if (D[n][indx2] == 2) {
        W[n][i] = indx2;
      }

      if (T[i] == 'R') {
        D[n][indx0] = (D[n][indx0] + 1) % 3;
        D[n][indx1] = (D[n][indx1] + 1) % 3;
        D[n][indx2] = (D[n][indx2] + 1) % 3;
        lastUpdated[n][indx0] = cntS;
        lastUpdated[n][indx1] = cntS;
        lastUpdated[n][indx2] = cntS;
      }

      if (T[i] == 'S') {
        cntS++;
      }

      // printf("W[%d][%d]=%d\\n", n, i+1, W[n][i+1]);
    }

    cntS2 = cntS;
  }

  for (int n = 1; n <= N; n++) {
    for (int i = 0; i < pow3[n]; i++) {
      update(n, i, cntS2);
    }
  }

  for (int i = 0; i < pow3[N]; i++) {
    int temp = 0;
    for (int n = 1; n <= N; n++) {
      temp += D[n][i % pow3[n]] * pow3[n - 1];
    }
    // printf("i=%d W[N-1][M]=%d temp=%d\\n", i, W[N-1][M], temp);
    if (i == W[N][M]) {
      if (temp != pow3[N] - 1) {
        // exit(1);
      }
    }
    printf("%d ", temp);
  }

  /*printf("\\n");
  for(int i = 0; i < pow3[N]; i ++){
      int temp = 0;
      for(int n = 0; n < N; n ++){
          temp += D[n][i%pow3[n+1]]*pow3[n];
      }
      printBase3(temp, N);
      printf("\\n");
  }*/

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

char T[550005];
int salsa2[550005];
int salsaNext[550005];
int p[550005];

// Based on editorial solution
int D[13]
     [550005]; // one digit at a time, with digit 0 representing the ones place
int W[13][550005];
int lastUpdated[13][550005];

int N;

int salsa(int x) {
  int temp = x;
  int pow3 = 1;
  int res = 0;
  for (int i = 0; i < N; i++) {
    int k = temp % 3;
    temp /= 3;

    if (k == 1) {
      res += pow3 * 2;
    } else if (k == 2) {
      res += pow3;
    }
    pow3 *= 3;
  }
  return res;
}

void update(int n, int indx, int cntS) {
  if ((cntS & 1) != (lastUpdated[n][indx] & 1)) {
    // conduct salsa
    if (D[n][indx] == 1) {
      D[n][indx] = 2;
    } else if (D[n][indx] == 2) {
      D[n][indx] = 1;
    }
  }
  // updated the lastUpdated index
  lastUpdated[n][indx] = cntS;
}

int digits[15];
void printBase3(int x, int N) {
  for (int i = 0; i < N; i++) {
    digits[i] = x % 3;
    x /= 3;
  }
  for (int i = N - 1; i >= 0; i--) {
    printf("%d", digits[i]);
  }
}

int pow3[16];

int main() {
  scanf("%d", &N);

  scanf(" %s", T);

  int M = strlen(T);

  pow3[0] = 1;
  for (int i = 1; i <= N; i++) {
    pow3[i] = pow3[i - 1] * 3;
  }

  int pow3N = pow3[N];

  for (int i = 0; i < pow3N; i++) {
    salsa2[i] = salsa(i);
    // printf("salsa2[%d]=%d\\n", i, salsa2[i]);
  }

  for (int n = 0; n < N; n++) {
    for (int j = 0; j < pow3[N]; j++) {
      D[n][j] = (j % pow3[n + 1]) / pow3[n];
      // printf("D[%d][%d]=%d\\n", n, j, D[n][j]);
    }
    W[n][0] = pow3[n + 1] - 1;
  }

  memset(lastUpdated, 0, sizeof(lastUpdated));
  int cntS2 = 0;
  for (int n = 0; n < N; n++) {
    int cntS = 0;
    for (int i = 0; i < M; i++) {
      if (T[i] == 'S') {
        W[n][i + 1] = salsa2[W[n][i]];
      } else if (T[i] == 'R') {
        int temp = (W[n][i] - 1);
        if (temp < 0) {
          temp += pow3[n + 1];
        }
        if (temp >= pow3[n + 1]) {
          temp -= pow3[n + 1];
        }
        W[n][i + 1] = temp;
      }

      // printf("W[%d][%d]=%d\\n", n, i+1, W[n][i+1]);

      int indx0 = W[n][i];
      int indx1 = (W[n][i] + pow3[n]) % pow3[n + 1];
      int indx2 = (W[n][i] + 2 * pow3[n]) % pow3[n + 1];
      // printf("W(%d, %d): %d %d %d cntS=%d\\n", n, i, indx0, indx1, indx2,
      // cntS);
      update(n, indx0, cntS + (T[i] == 'S'));
      update(n, indx1, cntS + (T[i] == 'S'));
      update(n, indx2, cntS + (T[i] == 'S'));
      if (T[i] == 'R') {
        D[n][indx0] = (D[n][indx0] + 1) % 3;
        D[n][indx1] = (D[n][indx1] + 1) % 3;
        D[n][indx2] = (D[n][indx2] + 1) % 3;
        lastUpdated[n][indx0] = cntS;
        lastUpdated[n][indx1] = cntS;
        lastUpdated[n][indx2] = cntS;
      }

      if (T[i] == 'S') {
        cntS++;
      }
    }

    cntS2 = cntS;
  }

  for (int n = 0; n < N; n++) {
    for (int i = 0; i < pow3[N]; i++) {
      update(n, i, cntS2);
    }
  }

  for (int i = 0; i < pow3[N]; i++) {
    int temp = 0;
    for (int n = 0; n < N; n++) {
      temp += D[n][i % pow3[n + 1]] * pow3[n];
    }
    // printf("i=%d W[N-1][M]=%d temp=%d\\n", i, W[N-1][M], temp);
    if (i == W[N - 1][M]) {
      if (temp != pow3[N] - 1) {
        exit(1);
      }
    }
    printf("%d ", temp);
  }

  /*printf("\\n");
  for(int i = 0; i < pow3[N]; i ++){
      int temp = 0;
      for(int n = 0; n < N; n ++){
          temp += D[n][i%pow3[n+1]]*pow3[n];
      }
      printBase3(temp, N);
      printf("\\n");
  }*/

  return 0;
}