1010. Radix (25)

1010. Radix (25)

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

#include <string>
#include <iostream>
#include <cmath>

using namespace std;

int char2num(char c){
  if(c < 'a')
    return c - '0';
  else
    return c - 'a' + 10;
}

int cal(string s){
  int num, tem;

  for(int i = 0; i < s.length(); i++){
    tem  = char2num(s[i]);
    if(num < tem)
      num = tem;
  }
  return num;
}
int min_radix(string s1, string s2, int tag){
  if(tag == 1)
    return cal(s2);
  else
    return cal(s1);
}

long long string2hex(string s, long long radix){
  long long num = 0;
  long long p = 0, tem = 0;
  for(int i = s.length() - 1; i >= 0; i--){
    tem = pow(radix, p++) * char2num(s[i]);
    //可能由于基底太大出现越界(tem 为负数)
    if(tem < 0)
      return -1;
    num += tem;
  }
  return num;
}
/*
* 二分查找基数,其中基数的范围应 >= 待测字符串所表示字符的大小上加+1( 因为基数大于以该基数表示的数中的任何数), <= 已知的另一字符串所表示的数字大小+1(因为两个数要能相等) 
*/
long long binary_search_radix(long long num, string s, long long minradix, long long maxradix){
  long long mid;
  long long tem, result = -1;
  while(minradix <= maxradix){
    mid = (minradix + maxradix) / 2;
    tem = string2hex(s, mid);

    if(tem > num || tem == -1){
      maxradix = mid - 1;
    }else if(tem < num){
      minradix = mid + 1;
    }else{
      result = mid;
      maxradix = mid - 1;
    }
  }
  return result;
}

int main(){
  string N1, N2;
  int tag;
  long long N1_hex, N2_hex, result, radix;
  cin >> N1 >> N2 >> tag >> radix;
  int minradix = min_radix(N1, N2, tag);
  if(tag == 1){
    N1_hex = string2hex(N1, radix);
    result = binary_search_radix(N1_hex, N2, minradix + 1, N1_hex + 1);
  }else{
    N2_hex = string2hex(N2, radix);
    result = binary_search_radix(N2_hex, N1, minradix + 1, N2_hex + 1);
  }
  if(result == -1)
    cout << "Impossible" << endl;
  else
    cout << result << endl;
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值