1010. Radix (25) (JAVA)

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");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值