1264
Enter the number 1 to 10 in the grid 5 in two rows.
Requirements: The number of adjacent grid in the right greater than left, below is greater than the top of.
Two kinds, fill method is qualified as shown in FIG.
Please calculate a total number of possible solutions.
Output
Please output the integer, do not print any extra content
note
Large lower right
algorithm
- For the convenience of the algorithm, the numbers are -1, then the first 0,9 OK in the upper left and lower right corner.
- To regard it as a row, i and i + 5 to Comparison
- array [4] can not handle
- 0-3, Comparative positions i, i + 1, and i, i + 5 Size
- 5-8, Comparative positions i, i + 1 Size
answer
Answer: 42
from itertools import permutations
def f(data):
for i in range(9):
if i == 4:
continue
if not i//5:
if data[i] < data[i+5] and data[i] < data[i+1]:
continue
else:
break
else:
if data[i] < data[i+1]:
continue
else:
break
if i == 8:
return True
array = [i for i in range(10)]
res = 0
for i in permutations(array[1:-1]):
array[1:-1] = i
if f(array):
res += 1
print(res)
1276
[Blue Bridge Cup 2015 finals] robot breeding
X galaxies robot can automatically replicate itself. They use a year's time can replicate the two themselves, and then lose the ability to replicate.
X galaxies year will elect a new born robot sent into space. That is, if X 5 Galaxy original robot,
after 1 year Total is: 5 + 9 = 14
Number of 2 years is: 5 + 9 + 17 = 31
If the total number of robots has been detected through s n years later, you initially able to calculate how many robots do?
Input
plurality of sets of test data input
for each set of test data input line and two numbers n s, separated by a space, meaning as above. n is not greater than 100, s is not more than 50 bits.
Output
For each test, the required output line, an integer representing the number of the first robot.
Sample input the Copy
2 31 is
97 2218388550399401452619230609499
sample output the Copy
. 5
. 8
note
Has the maximum value of s 50, Map () can not be used in long, however python2.2 later, when stored int overflow, it is automatically converted into a long type value
algorithm
Forgot year n + x, and remember to make up
Extension: If n is known, s (n is the year, x is the initial number), find the total number of years after the robot n s
- Recursive well calculated from s
answer
def f(n,s):
up = s + 2**(n+1)-2-n
return up//(2**(n+1)-1)
while True:
n, s = map(int,input().split())
print(f(n,s))
Extended Solution:
def f(n,x):
if n == 0:
return x
return f(n-1,2*x-1)+x
while True:
n, s = map(int,input().split())
print(f(n,x))