#P13253. [GCJ 2014 #1C] Part Elf

[GCJ 2014 #1C] Part Elf

题目描述

Vida says she's part Elf: that at least one of her ancestors was an Elf. But she doesn't know if it was a parent (1 generation ago), a grandparent (2 generations ago), or someone from even more generations ago. Help her out!

Being part Elf works the way you probably expect. People who are Elves, Humans and part-Elves are all created in the same way: two parents get together and have a baby. If one parent is AB\frac{A}{B} Elf, and the other parent is CD\frac{C}{D} Elf, then their baby will be (A/B+C/D)2\frac{(A/B + C/D)}{2} Elf. For example, if someone who is 01\frac{0}{1} Elf and someone who is 12\frac{1}{2} Elf have a baby, that baby will be 14\frac{1}{4} Elf.

Vida is certain about one thing: 40 generations ago, she had 2402^{40} different ancestors, and each one of them was 11\frac{1}{1} Elf or 01\frac{0}{1} Elf.

Vida says she's PQ\frac{P}{Q} Elf. Tell her what is the minimum number of generations ago that there could have been a 11\frac{1}{1} Elf in her family. If it is not possible for her to be PQ\frac{P}{Q} Elf, tell her that she must be wrong!

输入格式

The first line of the input gives the number of test cases, TT. TT lines follow. Each contains a fraction of the form PQ\frac{P}{Q}, where PP and QQ are integers.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the test case number (starting from 1) and yy is the minimum number of generations ago a 11\frac{1}{1} Elf in her family could have been if she is PQ\frac{P}{Q} Elf. If it's impossible that Vida could be PQ\frac{P}{Q} Elf, yy should be the string "impossible" (without the quotes).

5
1/2
3/4
1/4
2/23
123/31488
Case #1: 1
Case #2: 1
Case #3: 2
Case #4: impossible
Case #5: 8

提示

Note that the fifth sample case does not meet the limits for the Small input. Even if you don't solve it correctly, you might still have solved the Small input correctly.

Explanation of sample cases

In the first sample case, Vida could have a 11\frac{1}{1} Elf parent and a 01\frac{0}{1} Elf parent. That means she could have had a 11\frac{1}{1} Elf one generation ago, so the answer is 11.

In the second sample case, Vida could have had a 11\frac{1}{1} Elf parent and a 12\frac{1}{2} Elf parent. That means she could have had a 11\frac{1}{1} Elf one generation ago, so the answer is 11.

In the third sample case, Vida could have had a 01\frac{0}{1} Elf parent and a 12\frac{1}{2} Elf parent. The 12\frac{1}{2} Elf parent could have had a 11\frac{1}{1} Elf parent and a 01\frac{0}{1} Elf parent. That means she could have had a 11\frac{1}{1} Elf two generations ago, so the answer is 22.

In the fourth sample case, it's impossible to be exactly 223\frac{2}{23} Elf if your ancestors 40 generations ago were all 01\frac{0}{1} Elf or 11\frac{1}{1} Elf.

Note

Yes, Vida has a lot of ancestors. If that is the part of the problem that seems the most unrealistic to you, please re-read the part about Elves.

Limits

  • 1T1001 \leq T \leq 100.

Small dataset(8 Pts)

  • Time limit: 60 3 seconds.
  • 1P<Q10001 \leq P < Q \leq 1000.
  • PP and QQ have no common factors. That means P/QP/Q is a fraction in lowest terms.

Large dataset(12 Pts)

  • Time limit: 120 5 seconds.
  • 1P<Q10121 \leq P < Q \leq 10^{12}.
  • PP and QQ may have common factors. P/QP/Q is not guaranteed to be a fraction in lowest terms.