浙大PAT1010


1010. Radix (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.

Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible


题目网址:http://pat.zju.edu.cn/contests/pat-a-practise/1010

这题关键就是大数据的处理和二分法的查找。

用Python语言很容易就做出来了,因为基本不用考虑溢出的问题,需要用到二分查找,否则会有大数据超时。

 1 line = raw_input().split()
 2 if line[2]=='1':
 3     num1 = list(line[0])
 4     num2 = list(line[1])
 5 else:
 6     num1 = list(line[1])
 7     num2 = list(line[0])
 8 radix1 = int(line[3]) # the radix of num1
 9 decNum1 = 0
10 for num in num1:
11     decNum1 *= radix1
12     if '0'<=num<='9':
13         decNum1 += int(num)
14     else:
15         decNum1 += ord(num) - ord('a') + 10
16 
17 num2_min = min(num2)
18 if '0'<=num2_min<='9':
19     radix2_min = int(num2_min) + 1
20 else:
21     radix2_min = ord(num2_min) - ord('a') + 10 + 1
22 
23 radix2_max = decNum1 + 1
24 
25 while radix2_max >= radix2_min:
26     decNum2 = 0
27     radix2 = (radix2_max + radix2_min)/2
28     for num in num2:
29         decNum2 *= radix2
30         if '0'<=num<='9':
31             decNum2 += int(num)
32         else:
33             decNum2 += ord(num) - ord('a') + 10
34     if decNum2 > decNum1:
35         radix2_max = radix2 - 1
36     elif decNum2 < decNum1:
37         radix2_min = radix2 + 1
38     else:
39         print radix2
40         break
41 else: print "Impossible"

 

用C语言就没那么容易了,用64位的long long来表示数据,但是还是存在溢出的危险,于是需要对溢出做特殊处理。

 1 #include <stdio.h>
 2 #define LONG long long int
 3 
 4 // 将字符串s按进制radix转化为十进制整数
 5 LONG toDecimal(char* s,LONG radix)
 6 {
 7     int i;
 8     LONG result = 0;
 9     for(i=0;s[i]!=0;i++)
10     {
11         result *= radix;
12         if(s[i]>='0' && s[i]<='9')
13             result += s[i] - '0';
14         else
15             result += s[i] - 'a' + 10;
16         if(result < 0) return -1; // overflow
17     }
18     return result;
19 }
20 
21 // 求出目标数的最小进制
22 LONG getMinRadix(char* s)
23 {
24     LONG result = 0;
25     int i;
26     for(i=0;s[i]!=0;i++)
27     {
28         if(result<s[i])
29             result = s[i];
30     }
31     if(result>='0' && result<='9')
32         return result - '0' + 1;
33     else return result - 'a' + 10 + 1;
34 }
35 
36 int main()
37 {
38     char s1[11],s2[11];
39     LONG tag,radixSrc;
40     scanf("%s%s%lld%lld",s1,s2,&tag,&radixSrc);
41     LONG decNumSrc;
42     char *sNumSrc,*sNumTar;
43     if(tag==1)
44     {
45         sNumSrc = s1;
46         sNumTar = s2;
47     }
48     else
49     {
50         sNumSrc = s2;
51         sNumTar = s1;
52     }
53     
54     decNumSrc = toDecimal(sNumSrc,radixSrc);
55     if(decNumSrc == -1)    return 0; // 源操作数溢出
56 
57     LONG minRadixTar = getMinRadix(sNumTar);
58     LONG maxRadixTar = decNumSrc + 1;
59 
60     LONG radixTar;
61     while(maxRadixTar>=minRadixTar)
62     {
63         radixTar = (maxRadixTar + minRadixTar)/2;    
64         LONG decNumTar = toDecimal(sNumTar,radixTar);
65         if(decNumTar ==-1 || decNumTar > decNumSrc)
66             maxRadixTar = radixTar - 1;
67         else if(decNumTar < decNumSrc)
68             minRadixTar = radixTar + 1;
69         else
70         {
71             printf("%lld\n",radixTar);
72             return 0;
73         }
74     }
75     if(maxRadixTar < minRadixTar)
76     {
77         printf("Impossible\n");
78     }
79     return 0;
80 }

 对溢出做了特殊处理后,终于通过全部case。但是此例有一个问题,那就是源操作数(tag标志的那个数)不能超过long long的表示范围,否则要是在那里就发生溢出的话,整个程序就没有任何意义了。关于这一点,还不能完善的解决,欢迎各位大神的完美解决方案。

 

用Java也实现了一下,感觉跟用C语言却别不大,但效率要低一些了,还是要注意特殊处理溢出的问题,数据类型用64位的long。

 1 import java.util.Scanner;
 2 public class Main
 3 {
 4     public static long toDecimal(String s,long radix)
 5     {
 6         int len = s.length();
 7         byte[] str = s.getBytes();
 8         long result = 0;
 9         for(int i=0;i<len;i++)
10         {
11             result *= radix;
12             if(str[i]>='0' && str[i]<='9')
13                 result += str[i] - '0';
14             else
15                 result += str[i] - 'a' + 10;
16             if(result < 0)    return -1; // overflow
17         }
18         return result;
19     }
20     
21     public static long getMinRadix(String s)
22     {
23         int len = s.length();
24         byte[] str = s.getBytes();
25         long result = 0;
26         for(int i=0;i<len;i++)
27         {
28             if(str[i] > result)
29                 result = str[i];
30         }
31         if(result>='0' && result<='9')
32             return result - '0' + 1;
33         return result - 'a' + 10 + 1;
34     }
35 
36     public static void main(String[] argv)
37     {
38         Scanner in = new Scanner(System.in);
39         String sNum1 = in.next();
40         String sNum2 = in.next();
41         long tag = in.nextInt();
42         long radixSrc = in.nextInt();
43         long nNumSrc;
44         String sNumSrc,sNumTar;
45         if(tag==1)
46         {
47             sNumSrc = sNum1;
48             sNumTar = sNum2;
49         }
50         else
51         {
52             sNumSrc = sNum2;
53             sNumTar = sNum1;
54         }
55         nNumSrc = toDecimal(sNumSrc,radixSrc);
56         if(nNumSrc == -1)    return; // source number overflow
57 
58         long minRadixTar = getMinRadix(sNumTar);
59         long maxRadixTar = nNumSrc + 1;
60         long radixTar = 0;
61         while(maxRadixTar>=minRadixTar)
62         {
63             radixTar = (minRadixTar + maxRadixTar)/2;
64             long nNumTar = toDecimal(sNumTar,radixTar);
65             if(nNumTar==-1 || nNumTar > nNumSrc)
66                 maxRadixTar = radixTar - 1;
67             else if(nNumTar < nNumSrc)
68                 minRadixTar = radixTar + 1;
69             else
70             {
71                 System.out.println(radixTar);
72                 return;
73             }
74         }
75         if(maxRadixTar < minRadixTar)
76         {
77             System.out.println("Impossible");
78         }
79     }
80 }

大家如果有更好的解决方法,欢迎讨论。

 

 

posted @ 2013-03-29 12:27  mzorro  阅读(363)  评论(0编辑  收藏  举报