pat1010

package AdvancedLevelPractice;

import java.util.Scanner;

public class pat1010 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		
		String N1 = scanner.next();
		String N2 = scanner.next();
		long tag = scanner.nextInt();
		long radix = scanner.nextInt();
		
		String tmpString;
		if(tag == 2){
			tmpString = N1;
			N1 = N2;
			N2 = tmpString;
		}
		
		long  N1Decimal = getDecimal(N1,radix);
		long low = getLow(N2);
		long high = N1Decimal;
		
		
		long radix2 = -1;
		if(low >= high){
			radix2 = compare(N1Decimal, N2, low);
		}else {
			radix2 = getResults(radix2, N1Decimal, N2, low, high);
		}

		if(radix2 == -1){
			System.out.print("Impossible");
		}
		else {
			System.out.print(radix2);
		}
		
		scanner.close();
		
	}
	private static long compare(long n1Decimal, String n2, long radix) {
		// TODO Auto-generated method stub
		
		while(true){
			long sum = 0;
			long h = 1;
			
			for(int i = n2.length() - 1; i >= 0; i--){
				char ch = n2.charAt(i);

				if(ch <= '9'){
					sum += (ch - '0') * h;
				}
				else {
					sum += (ch - 'a' + 10) * h;
				}

				h *= radix;
				if(sum > n1Decimal){
					return -1;
				}
			}
			if(sum == n1Decimal){
				return radix;
			}
			radix++;
		}
	}
	private static long getResults(long radix2, long n1Decimal, String n2, long low, long high) {
		// TODO Auto-generated method stub
		long radix = (low + high) / 2;
		//System.out.println(low+"\t"+high+"\t"+"radix = "+radix);
		
		long sum = 0;
		long h = 1;
		
		
		for(int i = n2.length() - 1; i >= 0; i--){
			char ch = n2.charAt(i);

			if(ch <= '9'){
				sum += (ch - '0') * h;
			}
			else {
				sum += (ch - 'a' + 10) * h;
			}
			
			if(sum > n1Decimal || sum < 0){
				high = radix - 1;
				return getResults(radix2, n1Decimal, n2, low, high);
			}
		
			h *= radix;
		}

		if(sum == n1Decimal){
			radix2 = radix;
			high = radix - 1;
			return getResults(radix2, n1Decimal, n2, low, high);
		}
		
		if(low >= high){
			return radix2;
		}

		if (sum < n1Decimal) {
			low = radix + 1;
			return getResults(radix2, n1Decimal, n2, low, high);
		}
		
		return radix2;	
	
	}

	private static long getLow(String n2) {
		// TODO Auto-generated method stub
		
		long radix = 0;
		long tmp;
		for(int i = n2.length() - 1; i >= 0; i--){
			char ch = n2.charAt(i);
			if(ch <= '9'){
				tmp = ch - '0';
			}
			else {
				tmp = ch - 'a' + 10;
			}
			if(tmp > radix){
				radix = tmp;
			}
		}
		radix++;
		
		return radix;
	}

	private static long getDecimal(String n1, long radix) {
		// TODO Auto-generated method stub
		
		long sum = 0;
		long h = 1;
		for(int i = n1.length() - 1; i >= 0; i--){

			char ch = n1.charAt(i);
			if(ch <= '9'){
				sum += (ch - '0') * h;
			}
			else {
				sum += (ch - 'a' + 10) * h;
			}
			h *= radix;
		}
		return sum;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值