This page is still under construction.

Parts of this page are still being built. What you see may change.

Counting Ugly Expressions

Interview

Time limit5sMemory limit512 MB

Summary
Insert +, -, or nothing between adjacent digits of a digit string, count how many of the 3^(D-1) expressions evaluate to a number divisible by 2, 3, 5, or 7.
Level

Medium4 of 10

Topics
Brute force, Recursion, Number theory, Implementation
Solved
No attempts yet

Problem

A number is called ugly if it is divisible by at least one of the one-digit primes 2, 3, 5, and 7. So 14 is ugly and 13 is not, and 39 is ugly and 121 is not. The number 0 is ugly. A negative number can be ugly too, for example -14 and -39.

You are given a string of decimal digits, something like this.

123456

You may insert a plus sign or a minus sign between two adjacent digits to build an expression.

1 + 234 - 5 + 6 = 236

The value of this expression is 236, which is ugly.

123 + 4 - 56 = 71

The value of this expression is 71, which is not ugly.

Counting the expressions is easy. Between each two adjacent digits you choose a plus sign, a minus sign, or nothing, so a string of DD digits produces 3D−13^{D-1} 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.

Among the 3D−13^{D-1} expressions, count how many evaluate to an ugly number.

Input

The first line contains the number of test cases NN. Each of the next NN lines contains one string of decimal digits.

Limits

  • 0≤N≤1000 \le N \le 100
  • Each string is non-empty and contains only the characters 0 through 9.
  • Each string is at most 13 characters long.

Output

For each test case, print one line in this format.

Case #X: Y

Here XX is the test case number starting from 1, and YY is the number of expressions whose value is an ugly number.

Examples5

  1. Example 1

    Input
    4
    1
    9
    011
    12345
    
    Expected output
    Case #1: 0
    Case #2: 1
    Case #3: 6
    Case #4: 64
    
  2. Example 2

    Input
    10
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Expected output
    Case #1: 1
    Case #2: 0
    Case #3: 1
    Case #4: 1
    Case #5: 1
    Case #6: 1
    Case #7: 1
    Case #8: 1
    Case #9: 1
    Case #10: 1
    
  3. Example 3

    Input
    8
    00
    09
    10
    11
    13
    70
    77
    99
    
    Expected output
    Case #1: 3
    Case #2: 3
    Case #3: 1
    Case #4: 2
    Case #5: 2
    Case #6: 3
    Case #7: 3
    Case #8: 3
    
  4. Example 4

    Input
    3
    01023
    0102
    1023
    
    Expected output
    Case #1: 75
    Case #2: 18
    Case #3: 25
    
  5. Example 5

    Input
    5
    7
    49
    121
    13
    14
    
    Expected output
    Case #1: 1
    Case #2: 2
    Case #3: 6
    Case #4: 2
    Case #5: 3