Mr. Pippo's Pizza
题目连接:
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pippos-pizza
Description
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
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
For each test case, print on a single line the number of sides the pizza has.
Sample Input
1
5
Sample Output
3
5
Hint
题意
给你把正n边形全部切成三角形的方案数,让你求这个是正多少边形。
题解
推公式,可以发现答案是
(2n-4)!/(n-1)!(n-2)!
代码
from __future__ import print_function
from math import factorial
# get first line of input
ways_of_cutting = int(raw_input())
# check if there is a remaining line of input (should return "None" and exit if EOF reached)
while ways_of_cutting:
# print("ways_of_cutting: {}".format(ways_of_cutting))
if ways_of_cutting == 1:
print(3)
elif ways_of_cutting == 2:
print(4)
elif ways_of_cutting == 5:
print(5)
else:
for n in range(5, 1000):
if factorial(2 * (n - 2)) / (factorial(n - 1) * factorial(n - 2)) == ways_of_cutting:
print(n)
break
# get next line of input
try:
line = raw_input()
except:
break
ways_of_cutting = int(line)