Skip to content

Commit 158ccbc

Browse files
Add files via upload
1 parent 6290a1d commit 158ccbc

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
- Use queries of the first kind to find the maximum element using binary search.
2+
- Find random indices and get the values and sort them
3+
- The difference between any two consecutive elements will be a multiple of $g$
4+
- If we choose them randomly enough, there will two that are coprime, for example $3g$ and $5g$ so their GCD will give us $g$
5+
6+
The probability of failing to find 2 coprime differences among a sequence of n elements among k is of the order 10^{-9}
7+
------
8+
9+
10+
mt19937 rng(time(NULL));
11+
12+
int find_maximum(int &queries)
13+
{
14+
int left = 0, right = 1e9 + 5;
15+
16+
//Always L < Max <= R
17+
while(right - left > 1)
18+
{
19+
int has_higher;
20+
int mid = (left + right)/2;
21+
22+
queries--;
23+
24+
cout << "> " << mid << "\n"; cout.flush();
25+
26+
cin >> has_higher;
27+
28+
if(has_higher)
29+
left = mid;
30+
else
31+
right = mid;
32+
}
33+
34+
return right;
35+
}
36+
37+
int find_difference(int &queries, int greatest, int no_of_elements)
38+
{
39+
int remaining_range = no_of_elements - 1;
40+
vector <int> A;
41+
42+
vector <int> asked(no_of_elements + 1, false);
43+
44+
while(queries > 0 && remaining_range > 0)
45+
{
46+
int question_index = uniform_int_distribution<int>(1, no_of_elements)(rng);
47+
48+
if(asked[question_index])
49+
{
50+
continue;
51+
}
52+
53+
cout << "? " << question_index << "\n"; cout.flush();
54+
55+
asked[question_index] = true;
56+
57+
int answer;
58+
cin >> answer;
59+
A.push_back(answer);
60+
61+
queries--;
62+
remaining_range--;
63+
}
64+
65+
sort(all(A));
66+
67+
if(A[A.size() - 1] != greatest)
68+
A.push_back(greatest);
69+
70+
int difference = A[1] - A[0];
71+
72+
for(int i = 1; i < A.size(); i++)
73+
difference = __gcd(difference, A[i] - A[i - 1]);
74+
75+
return difference;
76+
}
77+
78+
int main()
79+
{
80+
int no_of_elements;
81+
cin >> no_of_elements;
82+
83+
const int MAX_QUERIES = 60;
84+
int queries = MAX_QUERIES;
85+
86+
int maximum = find_maximum(queries);
87+
int difference = find_difference(queries, maximum, no_of_elements);
88+
89+
int minimum = maximum - difference*(no_of_elements - 1);
90+
91+
cout << "! " << minimum << " " << difference << "\n"; cout.flush();
92+
93+
return 0;
94+
}

0 commit comments

Comments
 (0)