Mr. Pippo wants to start a new pizza shop. Everything about his pizzas is unique — the recipe is unique, the taste is unique, and even the shape of pizzas is unique. Instead of having a round shape like every other pizza, Mr. Pippo makes his pizzas in polygon shapes. For example, he can make his pizzas in a triangular shape or in a pentagonal shape.
Before serving a pizza, Mr. Pippo cuts it into triangular pieces. However, there are different ways he can cut the pizza. For example, a pentagonal pizza can be cut in five different ways as shown in the following figure. Each day, Mr. Pippo chooses a particular shape which can only be cut in an odd number of ways. Note that all the five cuts in the figure happen to be rotationally symmetric, but each of them is considered distinct.
Different ways a pentagonal pizza can be cut
Figure: Different ways a pentagonal pizza can be cut
Your task in this problem is to determine the shape of the pizza. Given the number of ways the pizza can be cut, you have to determine how many sides the pizza has.
Further clarification regarding the ways a pizza can be cut is given below:
A pizza can only be cut by connecting two vertices,
Two cuts on the pizza cannot cross each other, and
For an n-polygon there would be exactly (n-3) cuts which divide the pizza into (n-2) pieces.
Input Format
There will be up to 100 lines given where each line represents one test case. For each test case, the number of ways the pizza can be cut will be given. The number will be always odd and can be up to 308 digits long. The input is finished when end-of-file is reached.
Output Format
For each test case, print on a single line the number of sides the pizza has.
Sample Input
1
5
Sample Output
3
5
Explanation
For the first line of input, there is only one way to "cut" the pizza if the pizza is a triangle. This one way consists of no cuts at all.
For the second line of input, if there are five ways to cut the pizza, the pizza must be a pentagon, as shown in the figure in the problem description.
iterate through all vertices
this vertex look with vertex + 2 (not its neighbor)
verify it is not intersecting another segment from the segment list
if not add this new segment
++
there is faster algorithm but this gives you an idea.
We can see from the image you posted earlier that the 3 pieces result from a 5-sided pizza. But, your code outputs 3. Can you trace the logic of the program and figure out why?
@cire , i'm really stupid in math and geometry.
its not a homework, i just need to solve it to learn some formulas and increase my math/geometry skills.
i just need to solve it to learn some formulas and increase my math/geometry skills.
You won't be learning any formulas or increasing your math or geometry skills by solving this problem. On the other hand, you may increase your problem solving and reasoning skills if you'll take the time to work it out yourself.
Try to solve the inverse problem: Given a polygon of n sides, in how many was may be cutted.
When you perform a cut the polygon divides into two with a smaller number of sides, so you may use a recursive approach.
You'll notice that the answer grows quite fast and reach > 300 digits quite soon. So you could precalculate all those values and store them in a table.
Then to solve the original problem simply search the input in your table.