1044. Lucky Tickets. Easy!
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Background
The public transport administration of Ekaterinburg is anxious about the fact that passengers don’t like to pay for passage doing their best to avoid the fee. All the measures that had been taken (hard currency premiums for all of the chiefs, increase in conductors’ salaries, reduction of number of buses) were in vain. An advisor especially invited from the Ural State University says that personally he doesn’t buy tickets because he rarely comes across the lucky ones (a ticket is lucky if the sum of the first three digits in its number equals to the sum of the last three ones). So, the way out is found — of course, tickets must be numbered in sequence, but the number of digits on a ticket may be changed. Say, if there were only two digits, there would have been ten lucky tickets (with numbers 00, 11, …, 99). Maybe under the circumstances the ratio of the lucky tickets to the common ones is greater? And what if we take four digits? A huge work has brought the long-awaited result: in this case there will be 670 lucky tickets. But what to do if there are six or more digits?
Problem
So you are to save public transport of our city. Write a program that determines a number of lucky tickets for the given number of digits. By the way, there can’t be more than nine digits on one ticket.
Input
contains a positive even integer not greater than 9.
Output
should contain a number of tickets such that the sum of the first half of digits is equal to the sum of the second half of digits.
Sample
input | output |
---|---|
4 | 670 |
Problem Author: Stanislav Vasilyev
Problem Source: Ural State University Internal Contest October'2000 Students Session
Problem Source: Ural State University Internal Contest October'2000 Students Session
import math
class InputReader():
def nextInt(self):
return int(input().strip())
def nextInts(self):
ints = []
str = input().strip().split()
for s in str:
if (s is None) or (len(s) == 0):
continue
ints.append(int(s))
return ints
def nextFloats(self):
floats = []
str = input().strip().split()
for s in str:
if (s is None) or (len(s) == 0):
continue
floats.append(float(s))
return floats
class Task():
def dfs(self, k , pos , sum):
if pos == 0 :
if self.cnt[k].get(sum) :
self.cnt[k][sum] += 1
else :
self.cnt[k][sum] = 1
return
for i in range(0 , 10):
self.dfs(k , pos-1 , sum+i)
def solve(self):
self.cnt = [{} for i in range(0,5)]
for k in range(1 , 5):
self.dfs(k , k , 0)
self.dp = [0]*5
for i in range(1 , 5):
for j in self.cnt[i].values():
self.dp[i] += j ** 2
if __name__ == '__main__':
reader = InputReader()
task = Task()
task.solve()
print(task.dp[int(reader.nextInt()/2)])