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:
1 |
6 110 1 10 |
Sample Output 1:
1 |
2 |
Sample Input 2:
1 |
1 ab 1 2 |
Sample Output 2:
1 |
Impossible |
未AC,待完善……
似乎是因为数据的上限问题……
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include<stdio.h> #include<string.h> long long powi(long long base,int power) { int i; long long result=1; if(power==0) return 1; for(i=0;i<power;i++) { result*=base; } return result; } int getnum(char c) { if(c>='0'&&c<='9') return c-'0'; return c-'a'+10; } int main() { int i,j,tag; long long radix,result_radix=0,temp=0; char num[2][11]; long long num1=0,num2=-1; scanf("%s%s%d%d",num[0],num[1],&tag,&radix); tag-=1; for(i=strlen(num[tag])-1,j=0;i>=0;i--,j++) { num1+=getnum(num[tag][i])*powi(radix,j); } for(i=0;num[!tag][i]!='\0';i++) { temp=getnum(num[!tag][i]); if(temp>result_radix) result_radix=temp; } result_radix+=1; do { if(result_radix>36) { printf("Impossible\n"); return 0; } num2=0; for(i=strlen(num[!tag])-1,j=0;i>=0;i--,j++) { num2+=getnum(num[!tag][i])*powi(result_radix,j); } result_radix++; } while(num1!=num2); printf("%d\n",result_radix-1); } |