#P16488. [GKS 2014 #C] Broken Calculator

    ID: 16473 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>动态规划 DP2014Google Kick Start

[GKS 2014 #C] Broken Calculator

题目描述

Alice is a smart student who is very good at math. She is attending a math class. In this class, the teacher is teaching the students how to use a calculator. The teacher will tell an integer to all of the students, and the students must type that exact number into their calculators. If someone fails to type the number, he or she will be punished for failing such an easy task!

Unfortunately, at the beginning of the class, Alice finds that her calculator is broken! She finds that some of the number buttons are totally broken, and only the "multiply" and "equals" operator buttons are available to use. So she can only use these buttons to get the number quickly.

For instance, the teacher may say the number "6060", while Alice's calculator can only type "11", "22" and "55". She could push the following buttons:

  • Button "1515" (22 clicks)
  • Button "multiply" (11 click)
  • Button "22" (11 click)
  • Button "multiply" (11 click)
  • Button "22" (11 click)
  • Button "equals" (11 click)

This method requires 77 button clicks. However, if Alice uses "12×5=12\times 5=", only 55 clicks are needed. Of course Alice wants to get the integer as fast as possible, so she wants to minimize the number of button clicks. Your task is to help her find a way to get the required number quickly.

输入格式

The first line of the input gives a number TT, the number of integers the teacher says. TT test cases follow.

Each case contains two lines. The first line contains ten numbers each of which is only 00 or 11. the ith number (starting from 00) is "11" if the number i can be clicked, or "00" if it is broken. The second line contains only one number X\mathbf{X}, the integer the teacher tells everyone.

输出格式

For each test case, output one line containing "Case #x: yy", where xx is the test case number (starting from 11) and yy is the minimum number of button clicks needed, or "Impossible" if it is not possible to produce the number.

3
0 1 1 0 0 1 0 0 0 0
60
1 1 1 1 1 1 1 1 1 1
128
0 1 0 1 0 1 0 1 0 1
128
Case #1: 5
Case #2: 4
Case #3: Impossible

提示

The first sample case is explained in problem statement.

In the second case, all digits are available, so Alice can just press "11", "22", "88" and then "equals" to get the result. Please note that she still needs to press "equals" in the last step, even though there are no calculations.

For the last case, it's impossible since Alice cannot input any even numbers.

Limits

1T1001 \le T \le 100.

Small dataset (Test Set 1 - Visible)

1X1001 \le X \le 100.

Large dataset (Test Set 2 - Hidden)

1X1061 \le X \le 10^6.