#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 Elf, and the other parent is Elf, then their baby will be Elf. For example, if someone who is Elf and someone who is Elf have a baby, that baby will be Elf.
Vida is certain about one thing: 40 generations ago, she had different ancestors, and each one of them was Elf or Elf.
Vida says she's Elf. Tell her what is the minimum number of generations ago that there could have been a Elf in her family. If it is not possible for her to be Elf, tell her that she must be wrong!
输入格式
The first line of the input gives the number of test cases, . lines follow. Each contains a fraction of the form , where and are integers.
输出格式
For each test case, output one line containing "Case #: ", where is the test case number (starting from 1) and is the minimum number of generations ago a Elf in her family could have been if she is Elf. If it's impossible that Vida could be Elf, 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 Elf parent and a Elf parent. That means she could have had a Elf one generation ago, so the answer is .
In the second sample case, Vida could have had a Elf parent and a Elf parent. That means she could have had a Elf one generation ago, so the answer is .
In the third sample case, Vida could have had a Elf parent and a Elf parent. The Elf parent could have had a Elf parent and a Elf parent. That means she could have had a Elf two generations ago, so the answer is .
In the fourth sample case, it's impossible to be exactly Elf if your ancestors 40 generations ago were all Elf or 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
- .
Small dataset(8 Pts)
- Time limit:
603 seconds. - .
- and have no common factors. That means is a fraction in lowest terms.
Large dataset(12 Pts)
- Time limit:
1205 seconds. - .
- and may have common factors. is not guaranteed to be a fraction in lowest terms.