TopCoder problem "CharmingTickets" used in SRM 382 (Division I Level Three)



Problem Statement

    

A ticket number that contains exactly 2*K digits is called charming if and only if at least one of the following conditions is satisfied:

  • The sum of the first K digits is equal to the sum of the last K digits.
  • The sum of all the digits at positions with odd indices is equal to the sum of all the digits at positions with even indices.

Also, you think that some digits are better than others, so a charming number must contain only digits that you consider to be good. These digits are given in the String good. Determine the number of different charming numbers containing exactly 2*K digits. Return this number modulo 999983. Remember that ticket numbers may contain leading zeroes.

 

Definition

    
Class:CharmingTickets
Method:count
Parameters:int, String
Returns:int
Method signature:int count(int K, String good)
(be sure your method is public)
    
 

Constraints

-K will be between 1 and 1000, inclusive.
-good will contain between 1 and 10 characters, inclusive.
-good will contain only digits ('0' - '9').
-All characters in good will be distinct.
 

Examples

0)
    
1
"0123456789"
Returns: 10
Only "XX" numbers are charming.
1)
    
2
"21"
Returns: 8
Only 1111, 1122, 1212, 1221, 2112, 2121, 2211, 2222 are charming numbers.
2)
    
2
"0987654321"
Returns: 1240
3)
    
137
"0123456789"
Returns: 630063

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8316

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10805&pm=8316

Writer:

dkorduban

Testers:

PabloGilberto , Olexiy , marek.cygan , ivan_metelsky

Problem categories:

Dynamic Programming, Math