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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <numeric>
#include <vector>

#define NP 3628800
using Permutacja = std::array<unsigned char, 10>;
Permutacja permutacje[NP];

const unsigned long MASK = 298432032299594ul;
const int SUMY[7] = { 2, 5, 3, 7, 6, 1, 4 };

class Macierz
{
  bool b[10][10];

public:
  Macierz() {
    memset(b, 0, sizeof b);
  }

  bool& operator[](int i, int j) {
    return b[i][j];
  }
};

Macierz zakoduj(unsigned long x)
{
  Macierz m;
  std::bitset<64> bits(x ^ MASK);
  for (int i=0; i<7; ++i) {
    for (int j=0; j<7; ++j) {
      m[i,j] = bits[i*7+j];
    }
  }
  
  for (int i=0; i<7; ++i) {
    unsigned suma = 0;
    for (int j=0; j<7; ++j) {
      suma += SUMY[j] * m[i,j];
    }
    std::bitset<3> bitsum((suma % 7) + 1);
    m[i,7] = bitsum[0];
    m[i,8] = bitsum[1];
    m[i,9] = bitsum[2];
  }

  for (int j=0; j<7; ++j) {
    unsigned suma = 0;
    for (int i=0; i<7; ++i) {
      suma += SUMY[i] * m[i,j];
    }
    std::bitset<3> bitsum((suma % 7) + 1);
    m[7,j] = bitsum[0];
    m[8,j] = bitsum[1];
    m[9,j] = bitsum[2];
  }

  return m;
}

unsigned long odkoduj(Macierz m)
{
  std::vector<unsigned> votes(NP, 0);
  std::vector<unsigned> empties(NP, 0);

  int ip_k=0, ip_w=0;

  for (int ip=0; ip<NP; ++ip) {
    int votes=0, empties=0;
    for (int i=0; i<10; ++i) {
      const Permutacja& p = permutacje[ip];
      unsigned suma = 0;
      for (int j=0; j<7; ++j) {
        suma += SUMY[j] * m[i,p[j]];
      }
      unsigned check = (((m[i,p[9]] << 1) | m[i,p[8]]) << 1) | m[i,p[7]];
      if (check == (suma % 7) + 1) {
        votes++;
      }
      if (m[i,p[7]] == 0 && m[i,p[8]] == 0 && m[i,p[9]] == 0) {
        empties++;
      }
    }
    if (votes >= 7 && empties == 3) {
      ip_k = ip;
      break;
    }
  }

  for (int ip=0; ip<NP; ++ip) {
    int votes=0, empties=0;
    for (int j=0; j<10; ++j) {
      const Permutacja& p = permutacje[ip];
      unsigned suma = 0;
      for (int i=0; i<7; ++i) {
        suma += SUMY[i] * m[p[i],j];
      }
      unsigned check = (((m[p[9],j] << 1) | m[p[8],j]) << 1) | m[p[7],j];
      if (check == (suma % 7) + 1) {
        votes++;
      }
      if (m[p[7],j] == 0 && m[p[8],j] == 0 && m[p[9],j] == 0) {
        empties++;
      }
    }
    if (votes >= 7 && empties == 3) {
      ip_w = ip;
      break;
    }
  }
  
  const Permutacja& pw = permutacje[ip_w];
  const Permutacja& pk = permutacje[ip_k];
  
  std::bitset<64> bits(0lu);
  for (int i=0; i<7; ++i) {
    for (int j=0; j<7; ++j) {
      bits[i*7+j] = m[pw[i],pk[j]];
    }
  }

  return bits.to_ulong() ^ MASK;
}


void wypisz(Macierz m)
{
  for (int i=0; i<10; ++i) {
    for (int j=0; j<10; ++j) {
      putchar(m[i,j] ? '1' : '0');
    }
    putchar('\n');
    fflush(stdout);
  }
}

Macierz wczytaj()
{
  Macierz m;
  for (int i=0; i<10; ++i) {
    for (int j=0; j<10; ++j) {
      m[i,j] = getchar()=='1';
    }
    getchar(); // newline
  }
  return m;
}

int main()
{
  std::iota(permutacje[0].begin(), permutacje[0].end(), 0);
  for (int ip=1; ip<NP; ++ip) {
    permutacje[ip] = permutacje[ip-1];
    std::next_permutation(permutacje[ip].begin(), permutacje[ip].end());
  }
 
  const char X = getchar();
  while (getchar() != '\n') ;
  
  unsigned long M;
  int T;
  scanf("%lu %d\n", &M, &T);
  
  while (T-->0) {
    if (X == 'A') {
      unsigned long x;
      scanf("%lu", &x);
      wypisz(zakoduj(x));
    } else {
      Macierz m = wczytaj();
      printf("%lu\n", odkoduj(m));
      fflush(stdout);
    }
  }
}