#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define irep(i,a,b) for(int i = a; i >= b; i--)
typedef long long ll;
typedef long double ld;
//typedef __int128 int128;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<ll,ll> pl;
//BIBLIOTECZKA - https://github.com/tonowak/acmlib/blob/master/code/math/bignum/main.cpp
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
struct Num {
static constexpr int digits_per_elem = 9, base = int(1e9);
int sign = 0;
vector<int> x;
Num& shorten() {
while(ssize(x) and x.back() == 0)
x.pop_back();
for(int a : x)
assert(0 <= a and a < base);
if(x.empty())
sign = 0;
return *this;
}
Num(string s) {
sign = ssize(s) and s[0] == '-' ? s.erase(s.begin()), -1 : 1;
for(int i = ssize(s); i > 0; i -= digits_per_elem)
if(i < digits_per_elem)
x.emplace_back(stoi(s.substr(0, i)));
else
x.emplace_back(stoi(s.substr(i - digits_per_elem, digits_per_elem)));
shorten();
}
Num() {}
Num(ll s) : Num(to_string(s)) {}
};
string to_string(const Num& n) {
stringstream s;
s << (n.sign == -1 ? "-" : "") << (ssize(n.x) ? n.x.back() : 0);
for(int i = ssize(n.x) - 2; i >= 0; --i)
s << setfill('0') << setw(n.digits_per_elem) << n.x[i];
return s.str();
}
ostream& operator<<(ostream &o, const Num& n) {
return o << to_string(n).c_str();
}
auto operator<=>(const Num& a, const Num& b) {
if(a.sign != b.sign or ssize(a.x) != ssize(b.x))
return ssize(a.x) * a.sign <=> ssize(b.x) * b.sign;
for(int i = ssize(a.x) - 1; i >= 0; --i)
if(a.x[i] != b.x[i])
return a.x[i] * a.sign <=> b.x[i] * b.sign;
return strong_ordering::equal;
}
bool operator==(const Num& a, const Num& b) {
return a.x == b.x and a.sign == b.sign;
}
Num abs(Num n) { n.sign &= 1; return n; }
Num operator+(Num a, Num b) {
int mode = a.sign * b.sign >= 0 ? a.sign |= b.sign, 1 : abs(b) > abs(a) ? swap(a, b), -1 : -1, carry = 0;
for(int i = 0; i < max(ssize((mode == 1 ? a : b).x), ssize(b.x)) or carry; ++i) {
if(mode == 1 and i == ssize(a.x))
a.x.emplace_back(0);
a.x[i] += mode * (carry + (i < ssize(b.x) ? b.x[i] : 0));
carry = a.x[i] >= a.base or a.x[i] < 0;
a.x[i] -= mode * carry * a.base;
}
return a.shorten();
}
Num operator-(Num a) { a.sign *= -1; return a; }
Num operator-(Num a, Num b) { return a + -b; }
Num operator*(Num a, int b) {
assert(abs(b) < a.base);
int carry = 0;
for(int i = 0; i < ssize(a.x) or carry; ++i) {
if(i == ssize(a.x))
a.x.emplace_back(0);
ll cur = a.x[i] * ll(abs(b)) + carry;
a.x[i] = int(cur % a.base);
carry = int(cur / a.base);
}
if(b < 0)
a.sign *= -1;
return a.shorten();
}
Num operator*(const Num& a, const Num& b) {
Num c;
c.x.resize(ssize(a.x) + ssize(b.x));
REP(i, ssize(a.x))
for(int j = 0, carry = 0; j < ssize(b.x) or carry; ++j) {
ll cur = c.x[i + j] + a.x[i] * ll(j < ssize(b.x) ? b.x[j] : 0) + carry;
c.x[i + j] = int(cur % a.base);
carry = int(cur / a.base);
}
c.sign = a.sign * b.sign;
return c.shorten();
}
Num operator/(Num a, int b) {
assert(b != 0 and abs(b) < a.base);
int carry = 0;
for(int i = ssize(a.x) - 1; i >= 0; --i) {
ll cur = a.x[i] + carry * ll(a.base);
a.x[i] = int(cur / abs(b));
carry = int(cur % abs(b));
}
if(b < 0)
a.sign *= -1;
return a.shorten();
}
// zwraca a * pow(a.base, b)
Num shift(Num a, int b) {
vector v(b, 0);
a.x.insert(a.x.begin(), v.begin(), v.end());
return a.shorten();
}
Num operator/(Num a, Num b) {
assert(ssize(b.x));
int s = a.sign * b.sign;
Num c;
a = abs(a);
b = abs(b);
for(int i = ssize(a.x) - ssize(b.x); i >= 0; --i) {
if (a < shift(b, i)) continue;
int l = 0, r = a.base - 1;
while (l < r) {
int m = (l + r + 1) / 2;
if (shift(b * m, i) <= a)
l = m;
else
r = m - 1;
}
c = c + shift(l, i);
a = a - shift(b * l, i);
}
c.sign = s;
return c.shorten();
}
template<typename T>
Num operator%(const Num& a, const T& b) { return a - ((a / b) * b); }
Num nwd(const Num& a, const Num& b) { return b == Num() ? a : nwd(b, a % b); }
//KONIEC BIBLIOTECZKI
const int max_n = 5007;
const double l = 0.631;
vi perm[max_n][2]; //na kazdym miejscu losowa permutacja!!!
Num pot[max_n][4];
//jak za wolne to fft xDDD
//ale pewnie okej czasowo bo base 1e9 :)
Num to_num(string s, int base){
//cerr << "to_num, s: " << s << " b: " << b << endl;
//expected O(n)
Num ans = 0;
//mozemy traktowac s od lewej do prawej!!!
rep(i,0,s.length()-1){
Num x = int(s[i]-'0');
ans = ans+x*pot[i][base];
}
//cerr << "koniec petli" << endl;
return ans;
}
string change_base(Num n, int base){
//cerr << "change_base: " << base << endl;
//Expected O(n^2)
string ans;
Num zero = 0;
Num jeden = 1;
Num dwa = 2;
if(n == 0) return "0"; //cokolwiek bedzie dzialac
int ind = 0;
while(pot[ind][base] <= n)
ind++;
//teraz jestesmy > n!!!
while(ind > 0){
ind--; //powinno byc w O(n)
//if(pot == zero) break;
int ile = 0;
while(pot[ind][base] <= n){
n = n-pot[ind][base];
ile++;
}
ans += ile+'0';
}
reverse(ans.begin(),ans.end());
//znowu od lewej!!!
return ans;
}
char typ(int x){
if(x == 0) return 'P';
if(x == 1) return 'K';
if(x == 2) return 'N';
return '0';
}
bool win(char c1, char c2){
//P,K,N
//0 > 1
//1 > 2
//2 > 0
if(c1 == 'P'){
if(c2 == 'K') return 1;
else return 0;
}
if(c1 == 'K'){
if(c2 == 'N') return 1;
else return 0;
}
if(c1 == 'N'){
if(c2 == 'P') return 1;
else return 0;
}
cerr << "BLAD WIN" << endl;
return -1; //placeholder
}
int main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
srand(3145687);
string name;
cin >> name;
int t_name = (name == "Algosia");
pot[0][2] = 1;
pot[0][3] = 1;
Num dwa = 2;
Num trzy = 3;
rep(i,1,max_n-1){
pot[i][2] = pot[i-1][2]*dwa;
pot[i][3] = pot[i-1][3]*trzy;
}
//cerr << "skonczone pot\n";
int n,t; cin >> n >> t;
while(t--){
string s; cin >> s;
//bool f_debug = t_name;
bool f_debug = 0;
if(f_debug) cerr << "\n\n\nTEST NOWY\n\n\n" << endl;
if(f_debug) cerr << "start, s: " << s << " t_name: " << t_name << endl;
//stworzyc te permutacje trojkowe!!!
vi vec = {0,1,2};
rep(i,0,max_n-1){
random_shuffle(vec.begin(),vec.end());
perm[i][0] = vec;
random_shuffle(vec.begin(),vec.end());
perm[i][1] = vec;
}
if(f_debug) cerr << "po permutacji\n";
Num x = to_num(s,2);
if(f_debug) cerr << "zmienione x" << endl;
string t1 = change_base(x,3);
if(f_debug) cerr << "zmienione x i t1" << endl;
//wazne!!! albo +1 albo +2
int dl = double(n)*l+1;
if(t1.length() > dl){
cerr << "OKROPNY STRASZNY BLAD DLUGOSCI" << endl;
return 0;
}
while(t1.length() < dl) t1 += '0';
//na tym poziomie dodac zera z tylu jak za male!!!
//jak to zrobic???
if(f_debug) cerr << "\nname: " << name << " s: " << s << " t1: " << t1 << endl;
//pamietac, ze chcemy wygrac na koncu !!!
string t2; //od lewej do prawej
rep(i,0,dl-1){
int val = t1[i]-'0';
char c1 = typ(perm[i][t_name][val]);
if(f_debug) cerr << "\nname: " << name << " i: " << i << " val: " << val << " c1: " << c1 << endl;
cout << c1 << endl;
char c2; cin >> c2;
if(f_debug) cerr << "c2: " << c2 << endl;
//on ma taka sama permutacje!
rep(j,0,2){
if(f_debug) cerr << "j: " << j << " perm[i][j]: " << perm[i][t_name^1][j] << " typ: "
<< typ(perm[i][t_name^1][j]) << '\n';
if(typ(perm[i][t_name^1][j]) == c2){
//if(f_debug)
t2 += char(j+'0');
}
}
if(f_debug) cerr << "t2: " << t2 << endl;
if(c1 == c2) continue;
//ujemny bilans zawsze 0-P, dodatni bilans - zawsze 1-K
if(win(c1,c2)){
if(f_debug) cerr << "nasz win, dajemy K" << endl;
cout << 'K' << endl;
}
else{
if(f_debug) cerr << "win przeciwnika, dajemy P" << endl;
cout << 'P' << endl;
}
char temp; cin >> temp;
//naprawilismy bilans :)
}
if(f_debug) cerr << "\npo petli" << " t2: " << t2 << endl;
//dopychac zerami na koncu!!!
Num final_val = to_num(t2,3);
string ans = change_base(final_val,2);
while(ans.length() < n)
ans += '0';
if(f_debug) cerr << "\nname: " << name << " t2: " << t2 << " ans: " << ans << endl;
cout << "! " << ans << endl;
}
return 0;
}
//g++ -O3 -static -Wall .cpp -std=c++17
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define rep(i,a,b) for(int i = a; i <= b; i++) #define irep(i,a,b) for(int i = a; i >= b; i--) typedef long long ll; typedef long double ld; //typedef __int128 int128; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; //BIBLIOTECZKA - https://github.com/tonowak/acmlib/blob/master/code/math/bignum/main.cpp using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) struct Num { static constexpr int digits_per_elem = 9, base = int(1e9); int sign = 0; vector<int> x; Num& shorten() { while(ssize(x) and x.back() == 0) x.pop_back(); for(int a : x) assert(0 <= a and a < base); if(x.empty()) sign = 0; return *this; } Num(string s) { sign = ssize(s) and s[0] == '-' ? s.erase(s.begin()), -1 : 1; for(int i = ssize(s); i > 0; i -= digits_per_elem) if(i < digits_per_elem) x.emplace_back(stoi(s.substr(0, i))); else x.emplace_back(stoi(s.substr(i - digits_per_elem, digits_per_elem))); shorten(); } Num() {} Num(ll s) : Num(to_string(s)) {} }; string to_string(const Num& n) { stringstream s; s << (n.sign == -1 ? "-" : "") << (ssize(n.x) ? n.x.back() : 0); for(int i = ssize(n.x) - 2; i >= 0; --i) s << setfill('0') << setw(n.digits_per_elem) << n.x[i]; return s.str(); } ostream& operator<<(ostream &o, const Num& n) { return o << to_string(n).c_str(); } auto operator<=>(const Num& a, const Num& b) { if(a.sign != b.sign or ssize(a.x) != ssize(b.x)) return ssize(a.x) * a.sign <=> ssize(b.x) * b.sign; for(int i = ssize(a.x) - 1; i >= 0; --i) if(a.x[i] != b.x[i]) return a.x[i] * a.sign <=> b.x[i] * b.sign; return strong_ordering::equal; } bool operator==(const Num& a, const Num& b) { return a.x == b.x and a.sign == b.sign; } Num abs(Num n) { n.sign &= 1; return n; } Num operator+(Num a, Num b) { int mode = a.sign * b.sign >= 0 ? a.sign |= b.sign, 1 : abs(b) > abs(a) ? swap(a, b), -1 : -1, carry = 0; for(int i = 0; i < max(ssize((mode == 1 ? a : b).x), ssize(b.x)) or carry; ++i) { if(mode == 1 and i == ssize(a.x)) a.x.emplace_back(0); a.x[i] += mode * (carry + (i < ssize(b.x) ? b.x[i] : 0)); carry = a.x[i] >= a.base or a.x[i] < 0; a.x[i] -= mode * carry * a.base; } return a.shorten(); } Num operator-(Num a) { a.sign *= -1; return a; } Num operator-(Num a, Num b) { return a + -b; } Num operator*(Num a, int b) { assert(abs(b) < a.base); int carry = 0; for(int i = 0; i < ssize(a.x) or carry; ++i) { if(i == ssize(a.x)) a.x.emplace_back(0); ll cur = a.x[i] * ll(abs(b)) + carry; a.x[i] = int(cur % a.base); carry = int(cur / a.base); } if(b < 0) a.sign *= -1; return a.shorten(); } Num operator*(const Num& a, const Num& b) { Num c; c.x.resize(ssize(a.x) + ssize(b.x)); REP(i, ssize(a.x)) for(int j = 0, carry = 0; j < ssize(b.x) or carry; ++j) { ll cur = c.x[i + j] + a.x[i] * ll(j < ssize(b.x) ? b.x[j] : 0) + carry; c.x[i + j] = int(cur % a.base); carry = int(cur / a.base); } c.sign = a.sign * b.sign; return c.shorten(); } Num operator/(Num a, int b) { assert(b != 0 and abs(b) < a.base); int carry = 0; for(int i = ssize(a.x) - 1; i >= 0; --i) { ll cur = a.x[i] + carry * ll(a.base); a.x[i] = int(cur / abs(b)); carry = int(cur % abs(b)); } if(b < 0) a.sign *= -1; return a.shorten(); } // zwraca a * pow(a.base, b) Num shift(Num a, int b) { vector v(b, 0); a.x.insert(a.x.begin(), v.begin(), v.end()); return a.shorten(); } Num operator/(Num a, Num b) { assert(ssize(b.x)); int s = a.sign * b.sign; Num c; a = abs(a); b = abs(b); for(int i = ssize(a.x) - ssize(b.x); i >= 0; --i) { if (a < shift(b, i)) continue; int l = 0, r = a.base - 1; while (l < r) { int m = (l + r + 1) / 2; if (shift(b * m, i) <= a) l = m; else r = m - 1; } c = c + shift(l, i); a = a - shift(b * l, i); } c.sign = s; return c.shorten(); } template<typename T> Num operator%(const Num& a, const T& b) { return a - ((a / b) * b); } Num nwd(const Num& a, const Num& b) { return b == Num() ? a : nwd(b, a % b); } //KONIEC BIBLIOTECZKI const int max_n = 5007; const double l = 0.631; vi perm[max_n][2]; //na kazdym miejscu losowa permutacja!!! Num pot[max_n][4]; //jak za wolne to fft xDDD //ale pewnie okej czasowo bo base 1e9 :) Num to_num(string s, int base){ //cerr << "to_num, s: " << s << " b: " << b << endl; //expected O(n) Num ans = 0; //mozemy traktowac s od lewej do prawej!!! rep(i,0,s.length()-1){ Num x = int(s[i]-'0'); ans = ans+x*pot[i][base]; } //cerr << "koniec petli" << endl; return ans; } string change_base(Num n, int base){ //cerr << "change_base: " << base << endl; //Expected O(n^2) string ans; Num zero = 0; Num jeden = 1; Num dwa = 2; if(n == 0) return "0"; //cokolwiek bedzie dzialac int ind = 0; while(pot[ind][base] <= n) ind++; //teraz jestesmy > n!!! while(ind > 0){ ind--; //powinno byc w O(n) //if(pot == zero) break; int ile = 0; while(pot[ind][base] <= n){ n = n-pot[ind][base]; ile++; } ans += ile+'0'; } reverse(ans.begin(),ans.end()); //znowu od lewej!!! return ans; } char typ(int x){ if(x == 0) return 'P'; if(x == 1) return 'K'; if(x == 2) return 'N'; return '0'; } bool win(char c1, char c2){ //P,K,N //0 > 1 //1 > 2 //2 > 0 if(c1 == 'P'){ if(c2 == 'K') return 1; else return 0; } if(c1 == 'K'){ if(c2 == 'N') return 1; else return 0; } if(c1 == 'N'){ if(c2 == 'P') return 1; else return 0; } cerr << "BLAD WIN" << endl; return -1; //placeholder } int main(){ //ios::sync_with_stdio(0); //cin.tie(0); srand(3145687); string name; cin >> name; int t_name = (name == "Algosia"); pot[0][2] = 1; pot[0][3] = 1; Num dwa = 2; Num trzy = 3; rep(i,1,max_n-1){ pot[i][2] = pot[i-1][2]*dwa; pot[i][3] = pot[i-1][3]*trzy; } //cerr << "skonczone pot\n"; int n,t; cin >> n >> t; while(t--){ string s; cin >> s; //bool f_debug = t_name; bool f_debug = 0; if(f_debug) cerr << "\n\n\nTEST NOWY\n\n\n" << endl; if(f_debug) cerr << "start, s: " << s << " t_name: " << t_name << endl; //stworzyc te permutacje trojkowe!!! vi vec = {0,1,2}; rep(i,0,max_n-1){ random_shuffle(vec.begin(),vec.end()); perm[i][0] = vec; random_shuffle(vec.begin(),vec.end()); perm[i][1] = vec; } if(f_debug) cerr << "po permutacji\n"; Num x = to_num(s,2); if(f_debug) cerr << "zmienione x" << endl; string t1 = change_base(x,3); if(f_debug) cerr << "zmienione x i t1" << endl; //wazne!!! albo +1 albo +2 int dl = double(n)*l+1; if(t1.length() > dl){ cerr << "OKROPNY STRASZNY BLAD DLUGOSCI" << endl; return 0; } while(t1.length() < dl) t1 += '0'; //na tym poziomie dodac zera z tylu jak za male!!! //jak to zrobic??? if(f_debug) cerr << "\nname: " << name << " s: " << s << " t1: " << t1 << endl; //pamietac, ze chcemy wygrac na koncu !!! string t2; //od lewej do prawej rep(i,0,dl-1){ int val = t1[i]-'0'; char c1 = typ(perm[i][t_name][val]); if(f_debug) cerr << "\nname: " << name << " i: " << i << " val: " << val << " c1: " << c1 << endl; cout << c1 << endl; char c2; cin >> c2; if(f_debug) cerr << "c2: " << c2 << endl; //on ma taka sama permutacje! rep(j,0,2){ if(f_debug) cerr << "j: " << j << " perm[i][j]: " << perm[i][t_name^1][j] << " typ: " << typ(perm[i][t_name^1][j]) << '\n'; if(typ(perm[i][t_name^1][j]) == c2){ //if(f_debug) t2 += char(j+'0'); } } if(f_debug) cerr << "t2: " << t2 << endl; if(c1 == c2) continue; //ujemny bilans zawsze 0-P, dodatni bilans - zawsze 1-K if(win(c1,c2)){ if(f_debug) cerr << "nasz win, dajemy K" << endl; cout << 'K' << endl; } else{ if(f_debug) cerr << "win przeciwnika, dajemy P" << endl; cout << 'P' << endl; } char temp; cin >> temp; //naprawilismy bilans :) } if(f_debug) cerr << "\npo petli" << " t2: " << t2 << endl; //dopychac zerami na koncu!!! Num final_val = to_num(t2,3); string ans = change_base(final_val,2); while(ans.length() < n) ans += '0'; if(f_debug) cerr << "\nname: " << name << " t2: " << t2 << " ans: " << ans << endl; cout << "! " << ans << endl; } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |
English