http://pat.zju.edu.cn/contests/pat-practise/1010

来源:互联网 发布:java.io jar包 编辑:程序博客网 时间:2024/05/01 00:09

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
穷举法,会超时。一个点过不了。
[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<cstdlib>  
  9. #include<string>  
  10. using namespace std;  
  11. char s1[15];  
  12. char s2[15];  
  13. long long getvalue(char* s, int radix){  
  14.     long long value = 0;  
  15.     int len = strlen(s);  
  16.     int num;  
  17.     for(int i=0;i<len;++i){  
  18.         if(s[i]>='0' && s[i]<='9')  
  19.             num = s[i]-'0';  
  20.         else  
  21.             num = s[i]-'a'+10;  
  22.         value = value*radix + num;  
  23.     }  
  24.       
  25.     return value;  
  26. }  
  27. int findlowradix(char * s){  
  28.     int len = strlen(s);  
  29.     int ans = 2;  
  30.     for(int i=0;i<len;++i){  
  31.         if(s[i]>='0' && s[i]<='9')  
  32.             ans = s[i] - '0' + 1;  
  33.         else  
  34.             ans = s[i] - 'a' + 11;  
  35.     }  
  36.     return ans;  
  37. }  
  38. int main(){  
  39.   
  40.     freopen("in.txt""r", stdin);  
  41.     int tag, radix;  
  42.     scanf("%s %s %d %d", s1, s2, &tag, &radix);  
  43.     char * a = s1;  
  44.     char * b = s2;  
  45.     if(tag==2){  
  46.         char * c = a;  
  47.         a = b;  
  48.         b = c;  
  49.     }  
  50.     long long v = getvalue(a, radix);  
  51.     bool flag  = false;  
  52.     int r = findlowradix(b);  
  53.     for(;r<=3000;++r){  
  54.         long long res = getvalue(b, r);  
  55.         if(res==v){  
  56.             printf("%d\n", r);  
  57.             flag = true;  
  58.             break;  
  59.         }  
  60.     }  
  61.     if(!flag)  
  62.         printf("Impossible\n");  
  63.       
  64.     fclose(stdin);  
  65.     return 0;  
  66. }  
  67.       
原创粉丝点击