#include "dzilib.h" #include <bits/stdc++.h> using namespace std; typedef long long int lli; lli bias; vector<lli> v[2]; map<lli, lli> rem_questions; lli smart_ask(lli x) { auto it = rem_questions.find(x); // fprintf(stderr, "asked for %lld\n", x); if (it == rem_questions.end()) { lli ans = Ask(x); rem_questions[x] = ans; return ans; } else { return it->second; } } bool none_divis(int rem, int n, lli div) { for (int i=0; i<n; i++) { if (v[rem][v[rem].size()-1-i] % div == 0) return false; } return true; } void solve(lli lim_q) { rem_questions.clear(); lli n = GetN(); lli c = GetC(); // query range bias = 1000000 + rand() % 1000000; // bias = 10; lli pow_offset = 0; lli div_pov = 1; for (lli p=0; p<50; p++) { // test 2^(p+1) // fprintf(stderr, "Checking pow %lld\n", p+1); // fprintf(stderr, "bias and offset: %lld %lld\n", bias, pow_offset); if ((1LL<<p) > 2*n) break; v[0].clear(); v[1].clear(); int res = -1; // fprintf(stderr, "base for asking: %lld\n", bias + pow_offset); int looplim = 100; if (p == 0) looplim = 20; for (int i=0; i<looplim; i+=2) { for (int rem=0; rem<2; rem++) { // if (rem_questions.size() == lim_q) // break; v[rem].push_back(smart_ask(bias + pow_offset + (1LL<<p)*(i+rem))); if (v[rem].back() < p+2) { res = 1 - rem; break; } if (v[rem].size() > 1 && v[1-rem].size() > 1) { if (none_divis(rem, 2, p+2)) { // if (v[rem][v[rem].size()-1] % (p+2) != 0 && v[rem][v[rem].size()-2] % (p+2) != 0) { res = 1-rem; // fprintf(stderr, "Found mistake in %d\n", rem); break; } } if (v[rem].size() >= 4 && v[1-rem].size() >= 4) { if (none_divis(rem, 4, p+3)) { // if (v[rem][v[rem].size()-1] % (p+2) != 0 && v[rem][v[rem].size()-2] % (p+2) != 0) { res = 1-rem; // fprintf(stderr, "Found mistake in %d\n", rem); break; } } if (v[rem].size() >= 8 && v[1-rem].size() >= 8) { // fprintf(stderr, "8_check invoked\n"); if (none_divis(rem, 8, p+4)) { // if (v[rem][v[rem].size()-1] % (p+2) != 0 && v[rem][v[rem].size()-2] % (p+2) != 0) { res = 1-rem; // fprintf(stderr, "Found mistake in %d\n", rem); break; } } } if (res != -1) break; } // for (auto x : v[0]) // fprintf(stderr, " %lld", x); // fprintf(stderr, "\n"); // for (auto x : v[1]) // fprintf(stderr, " %lld", x); // fprintf(stderr, "\n"); // if (rem_questions.size() == lim_q) // break; if (res == -1) { // check magnitude sort(v[0].begin(), v[0].end()); sort(v[1].begin(), v[1].end()); lli sums[2]; sums[0] = 0; sums[1] = 0; for (int i=0; i<7; i++) { for (int j=0; j<2; j++) { sums[j] += v[j][i]; } } // fprintf(stderr, "sums %lld %lld\n", sums[0], sums[1]); if (sums[0] > sums[1]) res = 0; else res = 1; } // fprintf(stderr, "res: %d\n", res); if (res == 1) pow_offset += (1LL<<p); div_pov = (1LL<<(p+1)); // fprintf(stderr, "pow_offset %lld\ndiv_pov %lld\n", pow_offset, div_pov); } // fprintf(stderr, "Answering: %lld\n\n\n\n", div_pov - pow_offset - bias); // for (lli i=1; true; i++) { // lli ans = div_pov*i - pow_offset - bias; // if (ans >= 0) // Answer(ans); // } Answer(div_pov - pow_offset - bias); } int main() { srand(time(0)); lli t = GetT(); lli q = GetQ(); for (int i=0; i<t; i++) { solve(q/t); } 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 | #include "dzilib.h" #include <bits/stdc++.h> using namespace std; typedef long long int lli; lli bias; vector<lli> v[2]; map<lli, lli> rem_questions; lli smart_ask(lli x) { auto it = rem_questions.find(x); // fprintf(stderr, "asked for %lld\n", x); if (it == rem_questions.end()) { lli ans = Ask(x); rem_questions[x] = ans; return ans; } else { return it->second; } } bool none_divis(int rem, int n, lli div) { for (int i=0; i<n; i++) { if (v[rem][v[rem].size()-1-i] % div == 0) return false; } return true; } void solve(lli lim_q) { rem_questions.clear(); lli n = GetN(); lli c = GetC(); // query range bias = 1000000 + rand() % 1000000; // bias = 10; lli pow_offset = 0; lli div_pov = 1; for (lli p=0; p<50; p++) { // test 2^(p+1) // fprintf(stderr, "Checking pow %lld\n", p+1); // fprintf(stderr, "bias and offset: %lld %lld\n", bias, pow_offset); if ((1LL<<p) > 2*n) break; v[0].clear(); v[1].clear(); int res = -1; // fprintf(stderr, "base for asking: %lld\n", bias + pow_offset); int looplim = 100; if (p == 0) looplim = 20; for (int i=0; i<looplim; i+=2) { for (int rem=0; rem<2; rem++) { // if (rem_questions.size() == lim_q) // break; v[rem].push_back(smart_ask(bias + pow_offset + (1LL<<p)*(i+rem))); if (v[rem].back() < p+2) { res = 1 - rem; break; } if (v[rem].size() > 1 && v[1-rem].size() > 1) { if (none_divis(rem, 2, p+2)) { // if (v[rem][v[rem].size()-1] % (p+2) != 0 && v[rem][v[rem].size()-2] % (p+2) != 0) { res = 1-rem; // fprintf(stderr, "Found mistake in %d\n", rem); break; } } if (v[rem].size() >= 4 && v[1-rem].size() >= 4) { if (none_divis(rem, 4, p+3)) { // if (v[rem][v[rem].size()-1] % (p+2) != 0 && v[rem][v[rem].size()-2] % (p+2) != 0) { res = 1-rem; // fprintf(stderr, "Found mistake in %d\n", rem); break; } } if (v[rem].size() >= 8 && v[1-rem].size() >= 8) { // fprintf(stderr, "8_check invoked\n"); if (none_divis(rem, 8, p+4)) { // if (v[rem][v[rem].size()-1] % (p+2) != 0 && v[rem][v[rem].size()-2] % (p+2) != 0) { res = 1-rem; // fprintf(stderr, "Found mistake in %d\n", rem); break; } } } if (res != -1) break; } // for (auto x : v[0]) // fprintf(stderr, " %lld", x); // fprintf(stderr, "\n"); // for (auto x : v[1]) // fprintf(stderr, " %lld", x); // fprintf(stderr, "\n"); // if (rem_questions.size() == lim_q) // break; if (res == -1) { // check magnitude sort(v[0].begin(), v[0].end()); sort(v[1].begin(), v[1].end()); lli sums[2]; sums[0] = 0; sums[1] = 0; for (int i=0; i<7; i++) { for (int j=0; j<2; j++) { sums[j] += v[j][i]; } } // fprintf(stderr, "sums %lld %lld\n", sums[0], sums[1]); if (sums[0] > sums[1]) res = 0; else res = 1; } // fprintf(stderr, "res: %d\n", res); if (res == 1) pow_offset += (1LL<<p); div_pov = (1LL<<(p+1)); // fprintf(stderr, "pow_offset %lld\ndiv_pov %lld\n", pow_offset, div_pov); } // fprintf(stderr, "Answering: %lld\n\n\n\n", div_pov - pow_offset - bias); // for (lli i=1; true; i++) { // lli ans = div_pov*i - pow_offset - bias; // if (ans >= 0) // Answer(ans); // } Answer(div_pov - pow_offset - bias); } int main() { srand(time(0)); lli t = GetT(); lli q = GetQ(); for (int i=0; i<t; i++) { solve(q/t); } return 0; } |