Xtreme9.0 - 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邊形全部切成三角形的方案數(shù),讓你求這個(gè)是正多少邊形。
題解
推公式,可以發(fā)現(xià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 inputtry:line = raw_input()except:breakways_of_cutting = int(line)轉(zhuǎn)載于:https://www.cnblogs.com/qscqesze/p/5958569.html
總結(jié)
以上是生活随笔為你收集整理的Xtreme9.0 - Mr. Pippo's Pizza 数学的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 同轴电缆、双绞线和光纤光缆有什么区别?
- 下一篇: 温度传感器的工作原理简介