|
| 1 | +- The idea behind remembering this identity is that every bit that is set in both integers is counted twice and every bit that is set in only one of them is counted once. |
| 2 | +- Use this identity to find the first $3$ elements in $6$ steps and then find the remaining array elements with $2$ steps a piece. |
| 3 | + |
| 4 | + |
| 5 | +cpp |
| 6 | +#include <iostream> |
| 7 | +#include <vector> |
| 8 | +#include <algorithm> |
| 9 | + |
| 10 | +#define all(v) (v).begin(), (v).end() |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +int ask(int i, int j, string S) |
| 14 | +{ |
| 15 | + int answer; |
| 16 | + cout << S << " " << i << " " << j << "\n"; |
| 17 | + cout.flush(); |
| 18 | + cin >> answer; |
| 19 | + |
| 20 | + return answer; |
| 21 | +} |
| 22 | + |
| 23 | +int main() |
| 24 | +{ |
| 25 | + int no_of_elements, k; |
| 26 | + cin >> no_of_elements >> k; |
| 27 | + |
| 28 | + vector <int> A(no_of_elements + 1); |
| 29 | + |
| 30 | + int and_12 = ask(1, 2, "and"); |
| 31 | + int or_12 = ask(1, 2, "or"); |
| 32 | + int sum_12 = and_12 + or_12; |
| 33 | + |
| 34 | + int and_23 = ask(2, 3, "and"); |
| 35 | + int or_23 = ask(2, 3, "or"); |
| 36 | + int sum_23 = and_23 + or_23; |
| 37 | + |
| 38 | + int and_31 = ask(3, 1, "and"); |
| 39 | + int or_31 = ask(3, 1, "or"); |
| 40 | + int sum_31 = and_31 + or_31; |
| 41 | + |
| 42 | + A[1] = (sum_12 - sum_23 + sum_31)/2; |
| 43 | + A[2] = sum_12 - A[1]; |
| 44 | + A[3] = sum_23 - A[2]; //cout << A[1] << " " << A[2] << " " << A[3] << "\n"; |
| 45 | + |
| 46 | + for(int i = 4; i <= no_of_elements; i++) |
| 47 | + { |
| 48 | + int and_1i = ask(1, i, "and"); |
| 49 | + int or_1i = ask(1, i, "or"); |
| 50 | + int sum_1i = and_1i + or_1i; |
| 51 | + |
| 52 | + A[i] = sum_1i - A[1];//cout << A[i] << "\n"; |
| 53 | + } |
| 54 | + |
| 55 | + sort(all(A)); |
| 56 | + |
| 57 | + cout << "finish " << A[k] << "\n"; |
| 58 | + return 0; |
| 59 | +} |
| 60 | +``` |
0 commit comments