To solve Задача 11. Угадайте многоугольник (Problem 11: Guess the Polygon) in C++, we'll need to implement an interactive solution that can efficiently determine the number of sides n and the radius R of a regular polygon based on the ratio of its area to perimeter.
Understanding the Problem
Given a regular n-gon inscribed in a circle of integer radius R, the area to perimeter ratio can be derived as:
[
\text{Area} = \frac{n R^2 \sin\left(\frac{2\pi}{n}\right)}{2}
]
[
\text{Perimeter} = 2n R \sin\left(\frac{\pi}{n}\right)
]
[
\text{Area / Perimeter} = \frac{R}{2} \cdot \cos\left(\frac{\pi}{n}\right)
]
Our goal is to determine both n and R by querying the ratio using comparisons against values of the form p / 2^q, where p and q are integers.
Approach
-
Precompute Possible Ratios: Since 3 ≤ n ≤ 100 and 1 ≤ R ≤ 100, there are only 10,000 possible (n, R) pairs. For each pair, compute the ratio ratio = (R / 2) * cos(π / n).
-
Sorting Ratios: Sort all (n, R) pairs based on their computed ratios. This will allow us to perform a binary search by comparing against queried values.
-
Interactive Binary Search: For each scenario:
- Initialize the search range over the sorted list of possible ratios.
- Perform binary search by selecting a middle ratio, querying it, and adjusting the search range based on the response (
<, >, =).
- Continue until the exact
(n, R) pair is identified within the allowed number of queries (14).
-
Handling Interaction: Ensure that after every output (query or answer), the output buffer is flushed to maintain synchronization with the judge.
Sample C++ Code
Below is a C++ implementation of the described approach. Note that this code assumes interaction with a judge following the problem's protocol. Make sure to handle input/output operations carefully and flush the output after each query or answer.
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
// Structure to hold n, R, and the computed ratio
struct Polygon {
int n;
int R;
double ratio;
};
// Function to compute (R / 2) * cos(pi / n)
double compute_ratio(int n, int R) {
return (R / 2.0) * cos(M_PI / n);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
// Precompute all possible (n, R) and their ratios
vector<Polygon> polygons;
for(int n = 3; n <= 100; ++n){
for(int R = 1; R <= 100; ++R){
Polygon p;
p.n = n;
p.R = R;
p.ratio = compute_ratio(n, R);
polygons.push_back(p);
}
}
// Sort the polygons based on their ratio
sort(polygons.begin(), polygons.end(), [&](const Polygon &a, const Polygon &b) -> bool{
return a.ratio < b.ratio;
});
while(t--){
// Initialize binary search boundaries
int left = 0;
int right = polygons.size() -1;
int answer_n = -1;
int answer_R = -1;
// To handle floating precision and grouping
while(left <= right){
// Choose the middle element
int mid = left + (right - left) / 2;
double mid_ratio = polygons[mid].ratio;
// To create p / 2^q, we can represent mid_ratio as p / 2^q
// Find q such that p is integer, and p / 2^q is close to mid_ratio
// To simplify, let's choose q = 20, and p = round(mid_ratio * 2^q)
// This ensures p / 2^q approximates the ratio with precision ~1e-6
int q = 20;
long long p = round(mid_ratio * (1LL << q));
if(p > (1LL << 31)-1){
p = (1LL << 31)-1;
}
// Make the query
cout << "? " << p << " " << q << "\n";
cout.flush();
// Read the response
char response;
cin >> response;
if(response == '='){
// Found the exact ratio
answer_n = polygons[mid].n;
answer_R = polygons[mid].R;
break;
}
else if(response == '<'){
// p / 2^q is less than the actual ratio, need to search higher
left = mid +1;
}
else if(response == '>'){
// p / 2^q is greater than the actual ratio, need to search lower
right = mid -1;
}
else{
// Unexpected response, handle error
// Exit or continue
break;
}
}
if(answer_n == -1 && answer_R == -1){
// If exact match wasn't found, find the smallest polygon with ratio >= queried value
// This part can be enhanced based on further interaction responses
// For now, pick the closest
answer_n = polygons[left].n;
answer_R = polygons[left].R;
}
// Output the answer
cout << "! " << answer_n << " " << answer_R << "\n";
cout.flush();
// Read the judge's response
string judge_response;
cin >> judge_response;
if(judge_response != "gg"){
// If not correct, terminate
exit(0);
}
}
return 0;
}
Important Notes
-
Precision Handling: The ratio (R / 2) * cos(π / n) is a floating-point number. To mitigate precision issues, the code scales the ratio by a large power of two (2^20 in this case) to convert it into an integer p for querying.
-
Binary Search: The sorted list of ratios allows efficient narrowing down of possible (n, R) pairs using binary search within the allowed number of queries.
-
Flushing Output: After every cout operation (queries and answers), it's crucial to flush the output buffer using cout.flush() to ensure synchronization with the judge.
-
Handling Responses: The code assumes that the judge responds with one of <, >, or = after each query. It adjusts the search range based on the response.
-
Edge Cases: If an exact match isn't found (= never returned), the code currently picks the closest higher ratio. This part can be further refined based on additional interaction logic or multiple queries to narrow down.
-
Error Handling: If an unexpected response is received, the program exits to avoid undefined behavior, as per the problem's protocol.
Conclusion
This solution efficiently narrows down the possible (n, R) pairs by leveraging the mathematical relationship between the polygon's properties and performing a binary search through interactive queries. Ensure thorough testing and debugging with the actual judge to handle all edge cases and interactions correctly.