Ugly Numbers (Large)
Time limit5sMemory limit512 MB
Count expressions formed by inserting plus, minus, or nothing between digits whose evaluated value is divisible by 2, 3, 5, or 7.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Number theory, Combinatorics
- Solved
- No attempts yet
Problem
A number is called ugly if it is divisible by one of the one-digit primes 2, 3, 5 or 7. So 14 is ugly, but 13 is not. 39 is ugly, but 121 is not. 0 is ugly. Negative numbers can be ugly too. -14 and -39 are examples.
You are given one string of decimal digits, for example:
123456
Between each two adjacent digits you may put a plus sign, a minus sign, or nothing at all. For example, you can build
1 + 234 - 5 + 6 = 236
and the result 236 is ugly. On the other hand,
123 + 4 - 56 = 71
gives 71, which is not ugly.
Counting the expressions is easy. Between each two adjacent digits you pick one of plus, minus, or nothing, so a string of digits gives expressions.
A number may have leading zeros. If the string is "01023", then "01023", "0+1-02+3" and "01-023" are all legal expressions.
Count how many of the expressions evaluate to an ugly number.
Input
The first line contains the number of test cases . Each of the following lines contains one non-empty string of decimal digits.
Limits
- Each string is non-empty and contains only the characters '0' through '9'.
- Each string is at most 40 characters long.
Output
For each test case, print one line in the form
Case #X: Y
where is the test case number starting from 1, and is the number of expressions that evaluate to an ugly number.