Count Lucky Tickets
Problem statement
A ticket is a string of exactly n decimal digits, where n is even. Leading zeros are allowed, so 0000 is one four-digit ticket.
A ticket is lucky when the sum of its first n / 2 digits equals the sum of its last n / 2 digits.
Return the exact number of lucky tickets of length n. Different digit strings count separately. Do not take a modulus.
Function
countLuckyTickets(n: int) → longExamples
Example 1
n = 2return = 10Each half has one digit, so the two digits must be equal. The lucky tickets are 00, 11, 22, ..., 99, giving 10 tickets.
Example 2
n = 4return = 670A two-digit half has digit-sum counts 1, 2, ..., 9, 10, 9, ..., 2, 1 for sums from 0 through 18. Pairing two halves with the same sum gives 2 * (1^2 + 2^2 + ... + 9^2) + 10^2 = 670 tickets.
Constraints
- For this exercise, assume
2 <= n <= 16. - The number of digits is even.
- The answer fits in a signed 64-bit integer.