import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger str2int(byte[] str, BigInteger radix) {
BigInteger d = BigInteger.valueOf(0);
for(int i = 0; i < str.length; i ++) {
d = d.multiply(radix);
d = d.add(BigInteger.valueOf((int)str[i]));
}
return d;
}
public static void main(String[] args) {
String[] N = new String[2];
int tag;
BigInteger radix;
Scanner s = new Scanner(System.in);
for(int i = 0; i < 2; i ++)
N[i] = s.next();
tag = s.nextInt();
tag --;
radix = s.nextBigInteger();
byte[][] str = new byte[2][];
for(int i = 0; i < 2; i ++) {
str[i] = new byte[N[i].length()];
for(int j = 0; j < str[i].length; j ++) {
char c = N[i].charAt(j);
if(c >= '0' && c <= '9')
str[i][j] = (byte)(c - '0');
else
str[i][j] = (byte)(c - 'a' + 10);
}
}
BigInteger[] d = new BigInteger[2];
d[tag] = str2int(str[tag], radix);
int sr = 0;
int rt = 1-tag;
for(int i = 0; i < str[rt].length; i ++) {
if(str[rt][i] > sr)
sr = str[rt][i];
}
BigInteger rl = BigInteger.valueOf(sr+1);
BigInteger rh = d[tag].add(BigInteger.ONE);
boolean flag = false;
while(rl.compareTo(rh) <= 0) {
BigInteger rm = rl.add(rh).divide(BigInteger.valueOf(2));
d[rt] = str2int(str[rt], rm);
if(d[rt].compareTo(d[tag]) == 0) {
flag = true;
radix = rm;
break;
}
else if(d[rt].compareTo(d[tag]) > 0) {
rh = rm.subtract(BigInteger.ONE);
}
else {
rl = rm.add(BigInteger.ONE);
}
}
if(flag)
System.out.println(radix);
else
System.out.println("Impossible");
}
}
1010. Radix (25) (JAVA)
最新推荐文章于 2021-11-26 11:16:44 发布