#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (c it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <typename A, typename B>
using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>;
using namespace __gnu_pbds;
template <typename T> using ordered_set =
__gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
// ordered_set<int> s; s.insert(1); s.insert(2);
// s.order_of_key(1); // Out: 0.
// *s.find_by_order(1); // Out: 2.
using ld = long double;
using ll = long long;
constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;
void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }
template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }
class BigInteger {
// Performs binary arithmetic modulo 2**MAX_BIT.
public:
using Uint = uint64_t;
using Uint2 = __uint128_t;
static constexpr int MAX_BIT = 10'112; // 64 * 158
static constexpr int GROUP_SIZE = 64;
static constexpr int MAX_GROUP = MAX_BIT / GROUP_SIZE;
static_assert(MAX_BIT % 2 == 0);
static_assert(MAX_BIT % GROUP_SIZE == 0);
static_assert(GROUP_SIZE % 2 == 0);
BigInteger() {
fill(groups, groups + MAX_GROUP, 0);
}
explicit BigInteger(Uint x) : BigInteger() {
groups[0] = x;
}
uint64_t GetBit(int b) const {
assert(0 <= b and b < MAX_BIT);
return (groups[b / GROUP_SIZE] >> (b % GROUP_SIZE)) & 1;
}
void SetBit(int b, uint64_t value) {
assert(0 <= b and b < MAX_BIT);
assert(0 <= value and value <= 1);
value <<= (b % GROUP_SIZE);
uint64_t& group = groups[b / GROUP_SIZE];
group ^= value;
value ^= group & value;
group ^= value;
}
bool Increment() {
for (int i = 0; i < MAX_GROUP; i++) {
groups[i]++;
if (groups[i] != 0) {
return false;
}
}
return true;
}
void Negate() {
for (int i = 0; i < MAX_GROUP; i++) {
groups[i] = ~groups[i];
}
Increment();
}
void Sub(const BigInteger& other) {
Negate();
Add(other);
Negate();
}
bool Add(const BigInteger& other) {
Uint carry = 0;
for (int i = 0; i < MAX_GROUP; i++) {
const Uint sum = groups[i] + carry;
carry = 0;
if (sum < groups[i]) {
carry = 1;
}
const Uint sum2 = sum + other.groups[i];
if (sum2 < sum) {
carry = 1;
}
groups[i] = sum2;
}
return carry > 0;
}
bool Mul2() {
Uint carry = 0;
for (int i = 0; i < MAX_GROUP; i++) {
const Uint new_carry = groups[i] >> (GROUP_SIZE - 1);
groups[i] <<= 1;
groups[i] |= carry;
carry = new_carry;
}
return carry > 0;
}
void Xor(const BigInteger& other) {
for (int i = 0; i < MAX_GROUP; i++) {
groups[i] ^= other.groups[i];
}
}
void ShiftLeft(int n) {
const int group_shift = n / GROUP_SIZE;
const int shift_in_group = n % 64;
for (int i = MAX_GROUP - 1; i >= 0; i--) {
Uint high = 0;
if (i - group_shift >= 0) {
high = groups[i - group_shift] << shift_in_group;
}
Uint low = 0;
if (i - group_shift - 1 >= 0 and shift_in_group > 0) {
low = groups[i - group_shift - 1] >> (GROUP_SIZE - shift_in_group);
}
groups[i] = high | low;
}
}
void ShiftRight(int n) {
const int group_shift = n / GROUP_SIZE;
const int shift_in_group = n % 64;
for (int i = 0; i < MAX_GROUP; i++) {
Uint high = 0;
if (i + group_shift + 1 < MAX_GROUP and shift_in_group > 0) {
high = groups[i + group_shift + 1] << (GROUP_SIZE - shift_in_group);
}
Uint low = 0;
if (i + group_shift < MAX_GROUP) {
low = groups[i + group_shift] >> shift_in_group;
}
groups[i] = high | low;
}
}
int GetLeadingOne() const {
for (int i = MAX_GROUP - 1; i >= 0; i--) {
if (groups[i]) {
return i * GROUP_SIZE + 63 - __builtin_clzll(groups[i]);
}
}
return -1;
}
void Mod(const BigInteger& other) {
*this = Divide(other);
}
BigInteger Divide(const BigInteger& other) {
// Divides this number by "other".
// Returns the remainder module "other".
static vector<BigInteger> powers;
powers.clear();
powers.reserve(MAX_BIT);
powers.push_back(other);
while (powers.back() <= *this) {
powers.push_back(powers.back());
powers.back().Mul2();
}
BigInteger result;
for (int i = (int) powers.size() - 2; i >= 0; i--) {
if (powers[i] <= *this) {
Sub(powers[i]);
result.SetBit(i, 1);
}
}
swap(result, *this);
return result;
}
void Mul(const BigInteger& other) {
BigInteger total;
BigInteger high;
BigInteger low;
for (int i = 0; i < MAX_GROUP; i++) {
high = BigInteger();
low = BigInteger();
const Uint2 x = groups[i];
for (int j = 0; i + j < MAX_GROUP; j++) {
const Uint2 product = x * other.groups[j];
if (i + j + 1 < MAX_GROUP) {
high.groups[i + j + 1] = (product >> GROUP_SIZE);
}
low.groups[i + j] = product;
}
total.Add(high);
total.Add(low);
}
*this = total;
}
bool operator<(const BigInteger& other) const {
for (int i = MAX_GROUP - 1; i >= 0; i--) {
if (groups[i] != other.groups[i]) {
return groups[i] < other.groups[i];
}
}
return false;
}
bool operator<=(const BigInteger& other) const {
return !(other < *this);
}
bool operator>(const BigInteger& other) const {
return other < *this;
}
bool operator>=(const BigInteger& other) const {
return other <= *this;
}
bool operator==(const BigInteger& other) const {
for (int i = 0; i < MAX_GROUP; i++) {
if (groups[i] != other.groups[i]) {
return false;
}
}
return true;
}
bool operator!=(const BigInteger& other) const {
return !(*this == other);
}
bool IsZero() const {
for (int i = 0; i < MAX_GROUP; i++) {
if (groups[i] != 0) {
return false;
}
}
return true;
}
friend ostream& operator<<(ostream& deb, const BigInteger& i) {
int pos = BigInteger::MAX_BIT - 1;
while (pos > 0 and i.GetBit(pos) == 0) {
pos--;
}
while (pos >= 0) {
deb << i.GetBit(pos--);
}
return deb;
}
private:
Uint groups[MAX_GROUP];
};
debug& operator<<(debug& deb, const BigInteger& i) {
#ifdef LOCAL
int pos = BigInteger::MAX_BIT - 1;
while (pos > 0 and i.GetBit(pos) == 0) {
pos--;
}
while (pos >= 0) {
deb << i.GetBit(pos--);
}
#endif
return deb;
}
class ExtendedGcd {
public:
// Computes (x, y) such that x * a + y * b = GCD(a, b).
void Run(const BigInteger& a, const BigInteger& b, const BigInteger& modulo) {
stack.emplace_back(a, b, BigInteger());
ExpandStack();
gcd = stack.back().b;
UnwindStack(modulo);
}
const BigInteger& GetGcd() const {
return gcd;
}
const BigInteger& GetX() const {
return x;
}
const BigInteger& GetY() const {
return y;
}
private:
void ExpandStack() {
while (!stack.back().a.IsZero()) {
StackEntry& last = stack.back();
last.floor_b_div_a = last.b;
BigInteger remainder = last.floor_b_div_a.Divide(last.a);
stack.emplace_back(remainder, last.a, BigInteger());
}
}
void UnwindStack(const BigInteger& modulo) {
x = BigInteger();
y = BigInteger(1);
stack.pop_back();
while (!stack.empty()) {
StackEntry& entry = stack.back();
BigInteger& total = entry.floor_b_div_a;
total.Mul(x);
total.Mod(modulo);
if (y >= total) {
y.Sub(total);
} else {
y.Add(modulo);
y.Sub(total);
}
swap(x, y);
stack.pop_back();
}
}
struct StackEntry {
BigInteger a;
BigInteger b;
BigInteger floor_b_div_a;
};
vector<StackEntry> stack;
BigInteger gcd;
BigInteger x;
BigInteger y;
};
// log(2) = 0 . 693 147 180 559
constexpr ll log_2 = 693'147'180'559;
// log(3) = 1 . 098 612 288 668
constexpr ll log_3 = 1'098'612'288'668;
constexpr ll log_denom = 1'000'000'000'000;
constexpr int MAX_N = 5'000;
constexpr ll ENTROPY_THRESHOLD = (log_2 + 1) * MAX_N;
constexpr int MAX_POWER_2 = ENTROPY_THRESHOLD / log_2 + 10;
constexpr int MAX_POWER_3 = ENTROPY_THRESHOLD / log_3 + 10;
class SecretGenerator {
public:
SecretGenerator(int n_, bool enabled_)
: n(n_)
, enabled(enabled_)
, rng(0x0F5442CC7F8151A7) {}
BigInteger GetSecret() {
BigInteger result;
if (enabled) {
auto dist = uniform_int_distribution<int>(0, 1);
auto skip_dist = uniform_int_distribution<int>(0, 10);
for (int i = 0; i < n; i++) {
for (int j = skip_dist(rng); j >= 0; j--) {
dist(rng);
}
result.SetBit(i, dist(rng));
}
}
return result;
}
private:
int n;
bool enabled;
mt19937_64 rng;
};
class Preprocessing {
public:
void Run() {
powers_of_2.reserve(MAX_POWER_2);
powers_of_2.push_back(BigInteger(1));
for (int i = 1; i < MAX_POWER_2; i++) {
powers_of_2.push_back(powers_of_2.back());
powers_of_2.back().Mul2();
}
powers_of_3.reserve(MAX_POWER_3);
powers_of_3.push_back(BigInteger(1));
for (int i = 1; i < MAX_POWER_3; i++) {
powers_of_3.push_back(powers_of_3.back());
powers_of_3.back().Mul2();
powers_of_3.back().Add(powers_of_3[i - 1]);
}
}
const BigInteger& GetPowerOf2(int p) const {
assert(0 <= p and p < MAX_POWER_2);
return powers_of_2[p];
}
const BigInteger& GetPowerOf3(int p) const {
assert(0 <= p and p < MAX_POWER_3);
return powers_of_3[p];
}
private:
vector<BigInteger> powers_of_2;
vector<BigInteger> powers_of_3;
};
class MyNumber {
public:
explicit MyNumber(int n_)
: n(n_)
, number()
, taken_2(0)
, taken_3(0)
, digits_2(MAX_POWER_2, 0)
, digits_3(MAX_POWER_3, 0) {}
void Read(const BigInteger& secret) {
assert(n <= (int) digits_2.size());
string s;
cin >> s;
assert((int) s.size() == n);
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
number.SetBit(n - 1 - i, 0);
} else if (s[i] == '1') {
number.SetBit(n - 1 - i, 1);
} else {
assert(false);
}
}
number.Xor(secret);
debug() << imie(number);
}
void Preprocess(const Preprocessing& preprocessing) {
BigInteger tmp = number;
for (int i = 0; i < MAX_POWER_2; i++) {
digits_2[i] = number.GetBit(i);
}
for (int i = MAX_POWER_3 - 1; i >= 0; i--) {
const BigInteger& p = preprocessing.GetPowerOf3(i);
while (tmp >= p) {
digits_3[i]++;
tmp.Sub(p);
}
assert(0 <= digits_3[i] and digits_3[i] <= 2);
}
assert(tmp.IsZero());
taken_2 = 0;
taken_3 = 0;
#ifdef LOCAL
/*
debug() << "MyData{";
debug() << " " imie(number);
debug() << " " imie(digits_2);
debug() << " " imie(digits_3);
debug() << "}";
*/
#endif
}
int TakeDigit2() {
return digits_2[taken_2++];
}
int TakeDigit3() {
if (!pushed_digits_3.empty()) {
const int result = pushed_digits_3.back();
pushed_digits_3.pop_back();
return result;
}
return digits_3[taken_3++];
}
void PushDigit3(int digit) {
assert(0 <= digit and digit <= 2);
assert(pushed_digits_3.empty());
pushed_digits_3.push_back(digit);
}
ll SharedEntropy() const {
if (!pushed_digits_3.empty()) {
return (taken_2 - 2) * log_2 + taken_3 * log_3;
}
return taken_2 * log_2 + taken_3 * log_3;
}
private:
int n;
BigInteger number;
int taken_2;
int taken_3;
vector<int> digits_2;
vector<int> digits_3;
vector<int> pushed_digits_3;
};
class TheirNumber {
public:
TheirNumber(int n_, const Preprocessing& preprocessing_)
: n(n_)
, preprocessing(preprocessing_)
, next_weird_digit_3(false)
, digs_2(0)
, value_2()
, digs_3(0)
, value_3() {}
void PrepareForWeirdDigit3() {
assert(!next_weird_digit_3);
next_weird_digit_3 = true;
}
void NewDigit3(int digit) {
assert(0 <= digit and digit < 3);
if (next_weird_digit_3) {
next_weird_digit_3 = false;
NewDigit2(digit % 2);
NewDigit2(digit / 2);
return;
}
for (int i = 0; i < digit; i++) {
value_3.Add(preprocessing.GetPowerOf3(digs_3));
}
digs_3++;
}
void NewDigit2(int digit) {
assert(0 <= digit and digit < 2);
if (digit) {
value_2.Add(preprocessing.GetPowerOf2(digs_2));
}
digs_2++;
}
ll GatheredEntropy() const {
return digs_2 * log_2 + digs_3 * log_3;
}
BigInteger RecoverNumber(const BigInteger& secret) {
//cerr << imie(digs_2) imie(digs_3) << endl;
assert(GatheredEntropy() >= ENTROPY_THRESHOLD);
const BigInteger& p2 = preprocessing.GetPowerOf2(digs_2);
const BigInteger& p3 = preprocessing.GetPowerOf3(digs_3);
BigInteger modulo = p2;
modulo.Mul(p3);
assert(modulo >= preprocessing.GetPowerOf2(n));
ExtendedGcd egcd;
egcd.Run(p2, p3, modulo);
assert(egcd.GetGcd() == BigInteger(1));
BigInteger r2 = p3;
r2.Mul(egcd.GetY());
r2.Mod(modulo);
BigInteger r3 = p2;
r3.Mul(egcd.GetX());
r3.Mod(modulo);
BigInteger v2 = value_2;
v2.Mul(r2);
v2.Mod(modulo);
BigInteger v3 = value_3;
v3.Mul(r3);
v3.Mod(modulo);
BigInteger total = v2;
total.Add(v3);
if (total >= modulo) {
total.Sub(modulo);
}
total.Xor(secret);
return total;
}
private:
int n;
const Preprocessing& preprocessing;
bool next_weird_digit_3;
int digs_2;
BigInteger value_2;
int digs_3;
BigInteger value_3;
};
enum class AgentType {
ALGOSIA,
BAJTEK,
};
enum class Move {
PAPIER,
KAMIEN,
NOZYCE,
};
Move MoveFromChar(char c) {
if (c == 'P') {
return Move::PAPIER;
}
if (c == 'K') {
return Move::KAMIEN;
}
if (c == 'N') {
return Move::NOZYCE;
}
assert(false);
}
Move MoveFromDigit(int digit) {
if (digit == 0) {
return Move::PAPIER;
}
if (digit == 1) {
return Move::KAMIEN;
}
if (digit == 2) {
return Move::NOZYCE;
}
assert(false);
}
char MoveToChar(Move move) {
for (char c : {'P', 'K', 'N'}) {
if (MoveFromChar(c) == move) {
return c;
}
}
assert(false);
}
int MoveToDigit(Move move) {
for (int digit = 0; digit < 3; digit++) {
if (MoveFromDigit(digit) == move) {
return digit;
}
}
assert(false);
}
int ScoreGame(Move move_neg, Move move_pos) {
// Returns -1 if move_neg wins, 1 if move_pos wins, 0 if draw.
if (move_neg == move_pos) {
return 0;
}
const int digit_neg = MoveToDigit(move_neg);
const int digit_pos = MoveToDigit(move_pos);
if ((digit_neg + 1) % 3 == digit_pos) {
return -1;
}
return 1;
}
class Agent {
public:
Agent(
AgentType agent_type_,
int n_,
const Preprocessing& preprocessing_,
SecretGenerator& secret_generator_
) : n(n_)
, agent_type(agent_type_)
, preprocessing(preprocessing_)
, secret_generator(secret_generator_)
, my_number(n_)
, their_number(n_, preprocessing_) {}
void Run() {
BigInteger secret = secret_generator.GetSecret();
debug() << imie(secret);
my_number.Read(secret);
my_number.Preprocess(preprocessing);
while (my_number.SharedEntropy() < ENTROPY_THRESHOLD or their_number.GatheredEntropy() < ENTROPY_THRESHOLD) {
RunFromTieToTie();
}
const BigInteger result = their_number.RecoverNumber(secret);
debug() << imie(result);
cout << "! ";
for (int i = n - 1; i >= 0; i--) {
cout << result.GetBit(i);
}
cout << endl;
}
private:
void RunFromTieToTie() {
const Move my_move = MoveFromDigit(my_number.TakeDigit3());
cout << MoveToChar(my_move) << endl;
const Move their_move = ReadTheirMove();
their_number.NewDigit3(MoveToDigit(their_move));
const int score = ScoreGame(their_move, my_move);
if (score == 0) {
// Still tie.
return;
}
if (score == -1) {
// I lost.
return RunFromImbalanceToTie(false);
}
if (score == 1) {
// I won.
return RunFromImbalanceToTie(true);
}
assert(false);
}
void RunFromImbalanceToTie(const bool am_i_winning) {
while (true) {
const bool should_i_share = ShouldIShare();
Move my_move;
if (should_i_share) {
// Move::PAPIER or Move::KAMIEN
const int d0 = my_number.TakeDigit2();
const int d1 = my_number.TakeDigit2();
if (d0 == 1 and d1 == 1) {
if (am_i_winning) {
my_move = Move::PAPIER;
} else {
my_move = Move::KAMIEN;
}
} else {
my_number.PushDigit3(d1 * 2 + d0);
if (am_i_winning) {
my_move = Move::KAMIEN;
} else {
my_move = Move::PAPIER;
}
}
} else if (am_i_winning) {
my_move = Move::KAMIEN;
} else {
my_move = Move::PAPIER;
}
cout << MoveToChar(my_move) << endl;
const Move their_move = ReadTheirMove();
const int score = ScoreGame(their_move, my_move);
if (!should_i_share) {
if (score == 0) {
their_number.NewDigit2(1);
their_number.NewDigit2(1);
} else {
their_number.PrepareForWeirdDigit3();
}
} else if (am_i_winning) {
assert(their_move == Move::PAPIER);
} else {
assert(their_move == Move::KAMIEN);
}
if (score == 0) {
// Nothing changes.
continue;
}
if (score == 1) {
assert(!am_i_winning);
// Back to tie.
return;
}
if (score == -1) {
assert(am_i_winning);
// Back to tie.
return;
}
assert(false);
}
}
bool ShouldIShare() const {
const ll shared_entropy = my_number.SharedEntropy();
const ll gathered_entropy = their_number.GatheredEntropy();
if (shared_entropy == gathered_entropy) {
// Doesn't matter, resolve to Algosia.
return agent_type == AgentType::ALGOSIA;
}
if (shared_entropy < gathered_entropy) {
// I shared less than I gathered, I need to share now.
return true;
}
// I shared more than I gathered, I need to gather now.
return false;
}
Move ReadTheirMove() {
char c;
cin >> c;
return MoveFromChar(c);
}
int n;
AgentType agent_type;
const Preprocessing& preprocessing;
SecretGenerator& secret_generator;
MyNumber my_number;
TheirNumber their_number;
};
AgentType ReadAgentType() {
string who;
cin >> who;
if (who == "Algosia") {
return AgentType::ALGOSIA;
}
if (who == "Bajtek") {
return AgentType::BAJTEK;
}
assert(false);
}
int ReadInt() {
int n;
cin >> n;
return n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Preprocessing preprocessing;
preprocessing.Run();
const AgentType agent_type = ReadAgentType();
const int n = ReadInt();
const int t = ReadInt();
SecretGenerator secret_generator(n, true);
for (int i = 0; i < t; i++) {
Agent agent(agent_type, n, preprocessing, secret_generator);
agent.Run();
}
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 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } class BigInteger { // Performs binary arithmetic modulo 2**MAX_BIT. public: using Uint = uint64_t; using Uint2 = __uint128_t; static constexpr int MAX_BIT = 10'112; // 64 * 158 static constexpr int GROUP_SIZE = 64; static constexpr int MAX_GROUP = MAX_BIT / GROUP_SIZE; static_assert(MAX_BIT % 2 == 0); static_assert(MAX_BIT % GROUP_SIZE == 0); static_assert(GROUP_SIZE % 2 == 0); BigInteger() { fill(groups, groups + MAX_GROUP, 0); } explicit BigInteger(Uint x) : BigInteger() { groups[0] = x; } uint64_t GetBit(int b) const { assert(0 <= b and b < MAX_BIT); return (groups[b / GROUP_SIZE] >> (b % GROUP_SIZE)) & 1; } void SetBit(int b, uint64_t value) { assert(0 <= b and b < MAX_BIT); assert(0 <= value and value <= 1); value <<= (b % GROUP_SIZE); uint64_t& group = groups[b / GROUP_SIZE]; group ^= value; value ^= group & value; group ^= value; } bool Increment() { for (int i = 0; i < MAX_GROUP; i++) { groups[i]++; if (groups[i] != 0) { return false; } } return true; } void Negate() { for (int i = 0; i < MAX_GROUP; i++) { groups[i] = ~groups[i]; } Increment(); } void Sub(const BigInteger& other) { Negate(); Add(other); Negate(); } bool Add(const BigInteger& other) { Uint carry = 0; for (int i = 0; i < MAX_GROUP; i++) { const Uint sum = groups[i] + carry; carry = 0; if (sum < groups[i]) { carry = 1; } const Uint sum2 = sum + other.groups[i]; if (sum2 < sum) { carry = 1; } groups[i] = sum2; } return carry > 0; } bool Mul2() { Uint carry = 0; for (int i = 0; i < MAX_GROUP; i++) { const Uint new_carry = groups[i] >> (GROUP_SIZE - 1); groups[i] <<= 1; groups[i] |= carry; carry = new_carry; } return carry > 0; } void Xor(const BigInteger& other) { for (int i = 0; i < MAX_GROUP; i++) { groups[i] ^= other.groups[i]; } } void ShiftLeft(int n) { const int group_shift = n / GROUP_SIZE; const int shift_in_group = n % 64; for (int i = MAX_GROUP - 1; i >= 0; i--) { Uint high = 0; if (i - group_shift >= 0) { high = groups[i - group_shift] << shift_in_group; } Uint low = 0; if (i - group_shift - 1 >= 0 and shift_in_group > 0) { low = groups[i - group_shift - 1] >> (GROUP_SIZE - shift_in_group); } groups[i] = high | low; } } void ShiftRight(int n) { const int group_shift = n / GROUP_SIZE; const int shift_in_group = n % 64; for (int i = 0; i < MAX_GROUP; i++) { Uint high = 0; if (i + group_shift + 1 < MAX_GROUP and shift_in_group > 0) { high = groups[i + group_shift + 1] << (GROUP_SIZE - shift_in_group); } Uint low = 0; if (i + group_shift < MAX_GROUP) { low = groups[i + group_shift] >> shift_in_group; } groups[i] = high | low; } } int GetLeadingOne() const { for (int i = MAX_GROUP - 1; i >= 0; i--) { if (groups[i]) { return i * GROUP_SIZE + 63 - __builtin_clzll(groups[i]); } } return -1; } void Mod(const BigInteger& other) { *this = Divide(other); } BigInteger Divide(const BigInteger& other) { // Divides this number by "other". // Returns the remainder module "other". static vector<BigInteger> powers; powers.clear(); powers.reserve(MAX_BIT); powers.push_back(other); while (powers.back() <= *this) { powers.push_back(powers.back()); powers.back().Mul2(); } BigInteger result; for (int i = (int) powers.size() - 2; i >= 0; i--) { if (powers[i] <= *this) { Sub(powers[i]); result.SetBit(i, 1); } } swap(result, *this); return result; } void Mul(const BigInteger& other) { BigInteger total; BigInteger high; BigInteger low; for (int i = 0; i < MAX_GROUP; i++) { high = BigInteger(); low = BigInteger(); const Uint2 x = groups[i]; for (int j = 0; i + j < MAX_GROUP; j++) { const Uint2 product = x * other.groups[j]; if (i + j + 1 < MAX_GROUP) { high.groups[i + j + 1] = (product >> GROUP_SIZE); } low.groups[i + j] = product; } total.Add(high); total.Add(low); } *this = total; } bool operator<(const BigInteger& other) const { for (int i = MAX_GROUP - 1; i >= 0; i--) { if (groups[i] != other.groups[i]) { return groups[i] < other.groups[i]; } } return false; } bool operator<=(const BigInteger& other) const { return !(other < *this); } bool operator>(const BigInteger& other) const { return other < *this; } bool operator>=(const BigInteger& other) const { return other <= *this; } bool operator==(const BigInteger& other) const { for (int i = 0; i < MAX_GROUP; i++) { if (groups[i] != other.groups[i]) { return false; } } return true; } bool operator!=(const BigInteger& other) const { return !(*this == other); } bool IsZero() const { for (int i = 0; i < MAX_GROUP; i++) { if (groups[i] != 0) { return false; } } return true; } friend ostream& operator<<(ostream& deb, const BigInteger& i) { int pos = BigInteger::MAX_BIT - 1; while (pos > 0 and i.GetBit(pos) == 0) { pos--; } while (pos >= 0) { deb << i.GetBit(pos--); } return deb; } private: Uint groups[MAX_GROUP]; }; debug& operator<<(debug& deb, const BigInteger& i) { #ifdef LOCAL int pos = BigInteger::MAX_BIT - 1; while (pos > 0 and i.GetBit(pos) == 0) { pos--; } while (pos >= 0) { deb << i.GetBit(pos--); } #endif return deb; } class ExtendedGcd { public: // Computes (x, y) such that x * a + y * b = GCD(a, b). void Run(const BigInteger& a, const BigInteger& b, const BigInteger& modulo) { stack.emplace_back(a, b, BigInteger()); ExpandStack(); gcd = stack.back().b; UnwindStack(modulo); } const BigInteger& GetGcd() const { return gcd; } const BigInteger& GetX() const { return x; } const BigInteger& GetY() const { return y; } private: void ExpandStack() { while (!stack.back().a.IsZero()) { StackEntry& last = stack.back(); last.floor_b_div_a = last.b; BigInteger remainder = last.floor_b_div_a.Divide(last.a); stack.emplace_back(remainder, last.a, BigInteger()); } } void UnwindStack(const BigInteger& modulo) { x = BigInteger(); y = BigInteger(1); stack.pop_back(); while (!stack.empty()) { StackEntry& entry = stack.back(); BigInteger& total = entry.floor_b_div_a; total.Mul(x); total.Mod(modulo); if (y >= total) { y.Sub(total); } else { y.Add(modulo); y.Sub(total); } swap(x, y); stack.pop_back(); } } struct StackEntry { BigInteger a; BigInteger b; BigInteger floor_b_div_a; }; vector<StackEntry> stack; BigInteger gcd; BigInteger x; BigInteger y; }; // log(2) = 0 . 693 147 180 559 constexpr ll log_2 = 693'147'180'559; // log(3) = 1 . 098 612 288 668 constexpr ll log_3 = 1'098'612'288'668; constexpr ll log_denom = 1'000'000'000'000; constexpr int MAX_N = 5'000; constexpr ll ENTROPY_THRESHOLD = (log_2 + 1) * MAX_N; constexpr int MAX_POWER_2 = ENTROPY_THRESHOLD / log_2 + 10; constexpr int MAX_POWER_3 = ENTROPY_THRESHOLD / log_3 + 10; class SecretGenerator { public: SecretGenerator(int n_, bool enabled_) : n(n_) , enabled(enabled_) , rng(0x0F5442CC7F8151A7) {} BigInteger GetSecret() { BigInteger result; if (enabled) { auto dist = uniform_int_distribution<int>(0, 1); auto skip_dist = uniform_int_distribution<int>(0, 10); for (int i = 0; i < n; i++) { for (int j = skip_dist(rng); j >= 0; j--) { dist(rng); } result.SetBit(i, dist(rng)); } } return result; } private: int n; bool enabled; mt19937_64 rng; }; class Preprocessing { public: void Run() { powers_of_2.reserve(MAX_POWER_2); powers_of_2.push_back(BigInteger(1)); for (int i = 1; i < MAX_POWER_2; i++) { powers_of_2.push_back(powers_of_2.back()); powers_of_2.back().Mul2(); } powers_of_3.reserve(MAX_POWER_3); powers_of_3.push_back(BigInteger(1)); for (int i = 1; i < MAX_POWER_3; i++) { powers_of_3.push_back(powers_of_3.back()); powers_of_3.back().Mul2(); powers_of_3.back().Add(powers_of_3[i - 1]); } } const BigInteger& GetPowerOf2(int p) const { assert(0 <= p and p < MAX_POWER_2); return powers_of_2[p]; } const BigInteger& GetPowerOf3(int p) const { assert(0 <= p and p < MAX_POWER_3); return powers_of_3[p]; } private: vector<BigInteger> powers_of_2; vector<BigInteger> powers_of_3; }; class MyNumber { public: explicit MyNumber(int n_) : n(n_) , number() , taken_2(0) , taken_3(0) , digits_2(MAX_POWER_2, 0) , digits_3(MAX_POWER_3, 0) {} void Read(const BigInteger& secret) { assert(n <= (int) digits_2.size()); string s; cin >> s; assert((int) s.size() == n); for (int i = 0; i < n; i++) { if (s[i] == '0') { number.SetBit(n - 1 - i, 0); } else if (s[i] == '1') { number.SetBit(n - 1 - i, 1); } else { assert(false); } } number.Xor(secret); debug() << imie(number); } void Preprocess(const Preprocessing& preprocessing) { BigInteger tmp = number; for (int i = 0; i < MAX_POWER_2; i++) { digits_2[i] = number.GetBit(i); } for (int i = MAX_POWER_3 - 1; i >= 0; i--) { const BigInteger& p = preprocessing.GetPowerOf3(i); while (tmp >= p) { digits_3[i]++; tmp.Sub(p); } assert(0 <= digits_3[i] and digits_3[i] <= 2); } assert(tmp.IsZero()); taken_2 = 0; taken_3 = 0; #ifdef LOCAL /* debug() << "MyData{"; debug() << " " imie(number); debug() << " " imie(digits_2); debug() << " " imie(digits_3); debug() << "}"; */ #endif } int TakeDigit2() { return digits_2[taken_2++]; } int TakeDigit3() { if (!pushed_digits_3.empty()) { const int result = pushed_digits_3.back(); pushed_digits_3.pop_back(); return result; } return digits_3[taken_3++]; } void PushDigit3(int digit) { assert(0 <= digit and digit <= 2); assert(pushed_digits_3.empty()); pushed_digits_3.push_back(digit); } ll SharedEntropy() const { if (!pushed_digits_3.empty()) { return (taken_2 - 2) * log_2 + taken_3 * log_3; } return taken_2 * log_2 + taken_3 * log_3; } private: int n; BigInteger number; int taken_2; int taken_3; vector<int> digits_2; vector<int> digits_3; vector<int> pushed_digits_3; }; class TheirNumber { public: TheirNumber(int n_, const Preprocessing& preprocessing_) : n(n_) , preprocessing(preprocessing_) , next_weird_digit_3(false) , digs_2(0) , value_2() , digs_3(0) , value_3() {} void PrepareForWeirdDigit3() { assert(!next_weird_digit_3); next_weird_digit_3 = true; } void NewDigit3(int digit) { assert(0 <= digit and digit < 3); if (next_weird_digit_3) { next_weird_digit_3 = false; NewDigit2(digit % 2); NewDigit2(digit / 2); return; } for (int i = 0; i < digit; i++) { value_3.Add(preprocessing.GetPowerOf3(digs_3)); } digs_3++; } void NewDigit2(int digit) { assert(0 <= digit and digit < 2); if (digit) { value_2.Add(preprocessing.GetPowerOf2(digs_2)); } digs_2++; } ll GatheredEntropy() const { return digs_2 * log_2 + digs_3 * log_3; } BigInteger RecoverNumber(const BigInteger& secret) { //cerr << imie(digs_2) imie(digs_3) << endl; assert(GatheredEntropy() >= ENTROPY_THRESHOLD); const BigInteger& p2 = preprocessing.GetPowerOf2(digs_2); const BigInteger& p3 = preprocessing.GetPowerOf3(digs_3); BigInteger modulo = p2; modulo.Mul(p3); assert(modulo >= preprocessing.GetPowerOf2(n)); ExtendedGcd egcd; egcd.Run(p2, p3, modulo); assert(egcd.GetGcd() == BigInteger(1)); BigInteger r2 = p3; r2.Mul(egcd.GetY()); r2.Mod(modulo); BigInteger r3 = p2; r3.Mul(egcd.GetX()); r3.Mod(modulo); BigInteger v2 = value_2; v2.Mul(r2); v2.Mod(modulo); BigInteger v3 = value_3; v3.Mul(r3); v3.Mod(modulo); BigInteger total = v2; total.Add(v3); if (total >= modulo) { total.Sub(modulo); } total.Xor(secret); return total; } private: int n; const Preprocessing& preprocessing; bool next_weird_digit_3; int digs_2; BigInteger value_2; int digs_3; BigInteger value_3; }; enum class AgentType { ALGOSIA, BAJTEK, }; enum class Move { PAPIER, KAMIEN, NOZYCE, }; Move MoveFromChar(char c) { if (c == 'P') { return Move::PAPIER; } if (c == 'K') { return Move::KAMIEN; } if (c == 'N') { return Move::NOZYCE; } assert(false); } Move MoveFromDigit(int digit) { if (digit == 0) { return Move::PAPIER; } if (digit == 1) { return Move::KAMIEN; } if (digit == 2) { return Move::NOZYCE; } assert(false); } char MoveToChar(Move move) { for (char c : {'P', 'K', 'N'}) { if (MoveFromChar(c) == move) { return c; } } assert(false); } int MoveToDigit(Move move) { for (int digit = 0; digit < 3; digit++) { if (MoveFromDigit(digit) == move) { return digit; } } assert(false); } int ScoreGame(Move move_neg, Move move_pos) { // Returns -1 if move_neg wins, 1 if move_pos wins, 0 if draw. if (move_neg == move_pos) { return 0; } const int digit_neg = MoveToDigit(move_neg); const int digit_pos = MoveToDigit(move_pos); if ((digit_neg + 1) % 3 == digit_pos) { return -1; } return 1; } class Agent { public: Agent( AgentType agent_type_, int n_, const Preprocessing& preprocessing_, SecretGenerator& secret_generator_ ) : n(n_) , agent_type(agent_type_) , preprocessing(preprocessing_) , secret_generator(secret_generator_) , my_number(n_) , their_number(n_, preprocessing_) {} void Run() { BigInteger secret = secret_generator.GetSecret(); debug() << imie(secret); my_number.Read(secret); my_number.Preprocess(preprocessing); while (my_number.SharedEntropy() < ENTROPY_THRESHOLD or their_number.GatheredEntropy() < ENTROPY_THRESHOLD) { RunFromTieToTie(); } const BigInteger result = their_number.RecoverNumber(secret); debug() << imie(result); cout << "! "; for (int i = n - 1; i >= 0; i--) { cout << result.GetBit(i); } cout << endl; } private: void RunFromTieToTie() { const Move my_move = MoveFromDigit(my_number.TakeDigit3()); cout << MoveToChar(my_move) << endl; const Move their_move = ReadTheirMove(); their_number.NewDigit3(MoveToDigit(their_move)); const int score = ScoreGame(their_move, my_move); if (score == 0) { // Still tie. return; } if (score == -1) { // I lost. return RunFromImbalanceToTie(false); } if (score == 1) { // I won. return RunFromImbalanceToTie(true); } assert(false); } void RunFromImbalanceToTie(const bool am_i_winning) { while (true) { const bool should_i_share = ShouldIShare(); Move my_move; if (should_i_share) { // Move::PAPIER or Move::KAMIEN const int d0 = my_number.TakeDigit2(); const int d1 = my_number.TakeDigit2(); if (d0 == 1 and d1 == 1) { if (am_i_winning) { my_move = Move::PAPIER; } else { my_move = Move::KAMIEN; } } else { my_number.PushDigit3(d1 * 2 + d0); if (am_i_winning) { my_move = Move::KAMIEN; } else { my_move = Move::PAPIER; } } } else if (am_i_winning) { my_move = Move::KAMIEN; } else { my_move = Move::PAPIER; } cout << MoveToChar(my_move) << endl; const Move their_move = ReadTheirMove(); const int score = ScoreGame(their_move, my_move); if (!should_i_share) { if (score == 0) { their_number.NewDigit2(1); their_number.NewDigit2(1); } else { their_number.PrepareForWeirdDigit3(); } } else if (am_i_winning) { assert(their_move == Move::PAPIER); } else { assert(their_move == Move::KAMIEN); } if (score == 0) { // Nothing changes. continue; } if (score == 1) { assert(!am_i_winning); // Back to tie. return; } if (score == -1) { assert(am_i_winning); // Back to tie. return; } assert(false); } } bool ShouldIShare() const { const ll shared_entropy = my_number.SharedEntropy(); const ll gathered_entropy = their_number.GatheredEntropy(); if (shared_entropy == gathered_entropy) { // Doesn't matter, resolve to Algosia. return agent_type == AgentType::ALGOSIA; } if (shared_entropy < gathered_entropy) { // I shared less than I gathered, I need to share now. return true; } // I shared more than I gathered, I need to gather now. return false; } Move ReadTheirMove() { char c; cin >> c; return MoveFromChar(c); } int n; AgentType agent_type; const Preprocessing& preprocessing; SecretGenerator& secret_generator; MyNumber my_number; TheirNumber their_number; }; AgentType ReadAgentType() { string who; cin >> who; if (who == "Algosia") { return AgentType::ALGOSIA; } if (who == "Bajtek") { return AgentType::BAJTEK; } assert(false); } int ReadInt() { int n; cin >> n; return n; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); Preprocessing preprocessing; preprocessing.Run(); const AgentType agent_type = ReadAgentType(); const int n = ReadInt(); const int t = ReadInt(); SecretGenerator secret_generator(n, true); for (int i = 0; i < t; i++) { Agent agent(agent_type, n, preprocessing, secret_generator); agent.Run(); } return 0; } |
English