#P13464. [GCJ 2008 #1C] Ugly Numbers

    ID: 15339 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>动态规划 DP数学2008数论Google Code Jam

[GCJ 2008 #1C] Ugly Numbers

题目描述

Once upon a time in a strange situation, people called a number ugly if it was divisible by any of the one-digit primes (22, 33, 55 or 77). Thus, 1414 is ugly, but 1313 is fine. 3939 is ugly, but 121121 is not. Note that 00 is ugly. Also note that negative numbers can also be ugly; 14-14 and 39-39 are examples of such numbers.

One day on your free time, you are gazing at a string of digits, something like:

123456123456

You are amused by how many possibilities there are if you are allowed to insert plus or minus signs between the digits. For example you can make

1+2345+6=2361 + 234 - 5 + 6 = 236

which is ugly. Or

123+456=71123 + 4 - 56 = 71

which is not ugly.

It is easy to count the number of different ways you can play with the digits: Between each two adjacent digits you may choose put a plus sign, a minus sign, or nothing. Therefore, if you start with DD digits there are 3D13^{D-1} expressions you can make.

Note that it is fine to have leading zeros for a number. If the string is "01023", then "01023", "0+1-02+3" and "01-023" are legal expressions.

Your task is simple: Among the 3D13^{D-1} expressions, count how many of them evaluate to an ugly number.

输入格式

The first line of the input file contains the number of cases, NN. Each test case will be a single line containing a non-empty string of decimal digits.

输出格式

For each test case, you should output a line

Case #X: Y

where XX is the case number, starting from 11, and YY is the number of expressions that evaluate to an ugly number.

4
1
9
011
12345
Case #1: 0
Case #2: 1
Case #3: 6
Case #4: 64

提示

Limits

  • 0N1000 \leq N \leq 100.
  • The string in each test case will be non-empty and will contain only characters '0' through '9'.

Small dataset (Test set 1 - Visible)

  • Each string is no more than 13 characters long.

Large dataset (Test set 2 - Hidden)

  • Each string is no more than 40 characters long.