#P13431. [GCJ 2009 #1A] Multi-base happiness

    ID: 15305 远端评测题 20000ms 1024MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>模拟2009记忆化搜索Google Code Jam

[GCJ 2009 #1A] Multi-base happiness

题目描述

Given an integer NN, replace it by the sum of the squares of its digits. A happy number is a number where, if you apply this process repeatedly, it eventually results in the number 11. For example, if you start with 8282:

8*8 + 2*2       = 64 + 4    = 68,  repeat:
6*6 + 8*8       = 36 + 64   = 100, repeat:
1*1 + 0*0 + 0*0 = 1 + 0 + 0 = 1 (happy! :)

Since this process resulted in 11, 8282 is a happy number.

Notice that a number might be happy in some bases, but not happy in others. For instance, the base 1010 number 8282 is not a happy number when written in base 33 (as 1000110001).

You are one of the world's top number detectives. Some of the bases got together (yes, they are organized!) and hired you for an important task: find out what's the smallest integer number that's greater than 11 and is happy in all the given bases.

输入格式

The first line of input gives the number of cases TT. TT test cases follow. Each case consists of a single line. Each line contains a space separated list of distinct integers, representing the bases. The list of bases is always in increasing order.

输出格式

For each test case, output:

Case #XX: KK

where XX is the test case number, starting from 1, and KK is the decimal representation of the smallest integer (greater than 1) which is happy in all of the given bases.

3
2 3
2 3 7
9 10
Case #1: 3
Case #2: 143
Case #3: 91

提示

Limits

  • 2all possible input bases102 \leq \text{all possible input bases} \leq 10

Small dataset(9 Pts)

  • 1T421 \leq T \leq 42
  • $2 \leq \text{number of bases on each test case} \leq 3$

Large dataset(18 Pts)

  • 1T5001 \leq T \leq 500
  • $2 \leq \text{number of bases on each test case} \leq 9$