#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <cctype>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <numeric>
#include <cmath>
using namespace std;
typedef vector <int > VI;
typedef vector < VI > VVI;
typedef long long LL;
typedef vector < LL > VLL;
typedef vector < double > VD;
typedef vector < string > VS;
typedef pair<int,int> PII;
typedef vector <PII> VPII;
typedef istringstream ISS;
#define ALL(x) x.begin(),x.end()
#define REP(i,n) for (int i=0; i<(n); ++i)
#define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var)
#define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var)
#define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it)
#define PB push_back
#define PF push_front
#define MP(a,b) make_pair(a,b)
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
void dod(int b, int *t, int a) {
int i = 0;
while(true) {
int w = t[i] + a;
t[i] = w % b;
a = w / b;
if (a == 0) break;
i++;
}
}
void mul(int b, int n, int *t, int a) {
int carry = 0;
REP(i, n) {
int w = t[i] * a + carry;
t[i] = w % b;
carry = w / b;
}
}
void change_base(int b1, int b2, int n1, int n2, int *t1, int *t2) {
REP(i, n2) t2[i] = 0;
FORD(i, n1-1, 0) {
mul(b2, n2, t2, b1);
dod(b2, t2, t1[i]);
}
}
int compn2(int n) {
return ceill((long double)n / log2l(3.0));
}
int who;
int n;
const int N = 5001;
int maski[2][N];
int *moja, *jego;
int ja[N], on[N], ja3[N], on3[N];
char dupa[] = {'P', 'N', 'K'};
char syg2char(int a) { return dupa[a]; }
int char2syg(char a) { REP(i,3) if (dupa[i] == a) return i; return 0;}
int syg2res(int x, int y) {
return (4 + x - y) % 3 - 1;
}
void deb_wypisz_str(const char *pref, int *x, int len) {
#ifdef DEBUGPRINT
char t[N];
REP(i, len) t[i] = '0' + x[i];
t[len] = 0;
fprintf(stderr, "%s%s\n", pref, t);
#endif
}
void solve(void) {
int n2 = compn2(n);
change_base(2, 3, n, n2, ja, ja3);
deb_wypisz_str("MOJ2: ", ja, n);
deb_wypisz_str("MOJ3: ", ja3, n2);
int poz_ja = 0, poz_on = 0, stan = 0;
while (poz_ja < n2 || poz_on < n2) {
if (stan == 0) {
int syg = (poz_ja < n2 ? ja3[poz_ja] : 0); poz_ja++;
printf("%c\n", syg2char(syg)); fflush(stdout);
char zwr[10]; scanf("%s", zwr);
int syg_on = char2syg(zwr[0]);
if (poz_on < n2) on3[poz_on++] = syg_on;
stan += syg2res(syg, syg_on);
} else { // wyrownujemy
int syg = (stan > 0) ? 0 : 1;
printf("%c\n", syg2char(syg)); fflush(stdout);
char zwr[10]; scanf("%s", zwr);
stan = 0;
}
}
change_base(3, 2, n2, n, on3, on);
deb_wypisz_str("ON 2: ", on, n);
deb_wypisz_str("ON 3: ", on3, n2);
}
void deb_wypisz_maski(void) {
deb_wypisz_str("MOJA: ", moja, n);
deb_wypisz_str("JEGO: ", jego, n);
}
int main(){
srandom(541);
char t[N]; scanf("%s", t);
if (t[0] == 'A') who = 0; else who = 1;
int te;
scanf("%d%d", &n, &te);
while (te--) {
REP(a, 2) REP(i, n) maski[a][i] = random()%2;
moja = maski[who];
jego = maski[1-who];
deb_wypisz_maski();
scanf("%s", t);
REP(i, n) ja[i] = ((t[i]-'0') ^ moja[i]);
solve();
REP(i, n) t[i] = '0' + (on[i] ^ jego[i]);
t[n] = 0;
printf("! %s\n", t);
fflush(stdout);
}
return 0;
}
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 | #include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() void dod(int b, int *t, int a) { int i = 0; while(true) { int w = t[i] + a; t[i] = w % b; a = w / b; if (a == 0) break; i++; } } void mul(int b, int n, int *t, int a) { int carry = 0; REP(i, n) { int w = t[i] * a + carry; t[i] = w % b; carry = w / b; } } void change_base(int b1, int b2, int n1, int n2, int *t1, int *t2) { REP(i, n2) t2[i] = 0; FORD(i, n1-1, 0) { mul(b2, n2, t2, b1); dod(b2, t2, t1[i]); } } int compn2(int n) { return ceill((long double)n / log2l(3.0)); } int who; int n; const int N = 5001; int maski[2][N]; int *moja, *jego; int ja[N], on[N], ja3[N], on3[N]; char dupa[] = {'P', 'N', 'K'}; char syg2char(int a) { return dupa[a]; } int char2syg(char a) { REP(i,3) if (dupa[i] == a) return i; return 0;} int syg2res(int x, int y) { return (4 + x - y) % 3 - 1; } void deb_wypisz_str(const char *pref, int *x, int len) { #ifdef DEBUGPRINT char t[N]; REP(i, len) t[i] = '0' + x[i]; t[len] = 0; fprintf(stderr, "%s%s\n", pref, t); #endif } void solve(void) { int n2 = compn2(n); change_base(2, 3, n, n2, ja, ja3); deb_wypisz_str("MOJ2: ", ja, n); deb_wypisz_str("MOJ3: ", ja3, n2); int poz_ja = 0, poz_on = 0, stan = 0; while (poz_ja < n2 || poz_on < n2) { if (stan == 0) { int syg = (poz_ja < n2 ? ja3[poz_ja] : 0); poz_ja++; printf("%c\n", syg2char(syg)); fflush(stdout); char zwr[10]; scanf("%s", zwr); int syg_on = char2syg(zwr[0]); if (poz_on < n2) on3[poz_on++] = syg_on; stan += syg2res(syg, syg_on); } else { // wyrownujemy int syg = (stan > 0) ? 0 : 1; printf("%c\n", syg2char(syg)); fflush(stdout); char zwr[10]; scanf("%s", zwr); stan = 0; } } change_base(3, 2, n2, n, on3, on); deb_wypisz_str("ON 2: ", on, n); deb_wypisz_str("ON 3: ", on3, n2); } void deb_wypisz_maski(void) { deb_wypisz_str("MOJA: ", moja, n); deb_wypisz_str("JEGO: ", jego, n); } int main(){ srandom(541); char t[N]; scanf("%s", t); if (t[0] == 'A') who = 0; else who = 1; int te; scanf("%d%d", &n, &te); while (te--) { REP(a, 2) REP(i, n) maski[a][i] = random()%2; moja = maski[who]; jego = maski[1-who]; deb_wypisz_maski(); scanf("%s", t); REP(i, n) ja[i] = ((t[i]-'0') ^ moja[i]); solve(); REP(i, n) t[i] = '0' + (on[i] ^ jego[i]); t[n] = 0; printf("! %s\n", t); fflush(stdout); } return 0; } |
English