#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <math.h>
/// You may use:
// The number of students
int N;
// The probability any given student is positive
double P;
// This function performs a test on a subset of samples.
// Its argument is a vector of Booleans of length N,
// where the i-th element is true if the i-th sample should be added to the mix.
// It returns true if (and only if) at least one of the samples in the mix is positive.
bool test_students(std::vector<bool> mask) {
assert(mask.size() == (size_t)N);
std::string mask_str(N, ' ');
for (int i = 0; i < N; i++)
mask_str[i] = mask[i] ? '1' : '0';
printf("Q %s\n", mask_str.c_str());
fflush(stdout);
char answer;
scanf(" %c", &answer);
return answer == 'P';
}
/*
f(n) = argmin(
(f(k) + g(n - k)) * (k / n + (n - k) / n * (1 - (1 - P)^k)) +
)
*/
using namespace std;
vector<pair<double, int>> f, g;
void init() {
f.assign(N + 1, {100000.0, -1});
g.assign(N + 1, {100000.0, -1});
f[0] = {0, 0};
f[1] = {0, 0};
g[0] = {0, 0};
g[1] = {1, 1};
for (int n = 2; n <= N; n++) {
for (int k = 1; k <= n; k++) {
double p = double(k) / double(n) + double(n - k) / double(n) * (1.0 - pow(1.0 - P, k));
double exp = 1.0 +
p * (f[k].first + g[n - k].first) +
(1 - p) * (f[n - k].first);
if (exp < f[n].first) {
f[n] = {exp, k};
}
}
for (int k = 1; k <= n; k++) {
double p = 1.0 - pow(1.0 - P, k);
double exp = 1.0 +
p * (f[k].first + g[n - k].first) +
(1 - p) * (g[n - k].first);
if (exp < g[n].first) {
g[n] = {exp, k};
}
}
}
}
/*
1100010010
0001
*/
void solve(std::vector<bool>& result, int l, int r, bool containsPositive) {
int n = r - l;
if (n == 0)
return;
if (containsPositive && n == 1) {
result[l] = true;
return;
}
int k = containsPositive ? f[n].second : g[n].second;
vector<bool> mask(N);
for (int i = l; i < l + k; i++)
mask[i] = true;
bool res = test_students(mask);
if (res) {
solve(result, l, l + k, true);
solve(result, l + k, r, false);
}
else {
solve(result, l + k, r, containsPositive);
}
}
std::vector<bool> find_positive() {
std::vector<bool> answer(N);
if (P == 0.0)
return answer;
if (P == 1.0) {
for (int i = 0; i < N; i++)
answer[i] = true;
return answer;
}
init();
solve(answer, 0, N, false);
return answer;
}
int main() {
int T;
scanf("%d %lf %d", &N, &P, &T);
// You may perform any extra initialization here.
for (int i = 0; i < T; i++) {
std::vector<bool> answer = find_positive();
assert(answer.size() == (size_t)N);
std::string answer_str(N, ' ');
for (int j = 0; j < N; j++)
answer_str[j] = answer[j] ? '1' : '0';
printf("A %s\n", answer_str.c_str());
fflush(stdout);
char verdict;
scanf(" %c", &verdict);
if (verdict == 'W')
exit(0);
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'bool test_students(std::vector<bool>)':
Main.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf(" %c", &answer);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:127:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d %lf %d", &N, &P, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:143:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | scanf(" %c", &verdict);
| ~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |