fork download
  1. #include <cmath>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <algorithm>
  8. #include <queue>
  9.  
  10. #define rep(i, l, r) for(int i = l; i <= r; i++)
  11. #define down(i, l, r) for(int i = l; i >= r; i--)
  12. #define MS 45
  13. #define MAX 1037471823
  14. #define Q 103
  15.  
  16. using namespace std;
  17.  
  18. int n, g[5][MS];
  19. long long f[5][MS];
  20. char s[12][12];
  21.  
  22. int main()
  23. {
  24. scanf("%d", &n);
  25. rep(i, 1, 6) scanf("%s", s[i]);
  26. f[1][1] = f[2][1] = f[3][1] = 1;
  27. down(i, 6, 1)
  28. {
  29. if (s[i][0] == 'A') g[1][1] = s[i][1]-'A'+1;
  30. if (s[i][0] == 'B') g[2][1] = s[i][1]-'A'+1;
  31. if (s[i][0] == 'C') g[3][1] = s[i][1]-'A'+1;
  32. }
  33. rep(j, 2, n) rep(i, 1, 3)
  34. if (g[g[i][j-1]][j-1] == i) g[i][j] = g[i][j-1], f[i][j] = f[i][j-1]+f[g[i][j-1]][j-1]+f[i][j-1]+2;
  35. else g[i][j] = g[g[i][j-1]][j-1], f[i][j] = f[i][j-1]+f[g[i][j-1]][j-1]+1;
  36. printf("%lld\n", f[1][n]);
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 3300KB
stdin
3
AB BC CA BA CB AC
stdout
7