1010. Radix (25)

1010. Radix (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代

题目大意:给定两个数n1和n2,接着给一个tag和radix如果tag等于1表示n1为radix进制的数,如果tag = 2表示n2为radix进制的数,问是否存在一个进制使得n1等于n2。

解题思路:

按照题目意思,不管tag为几都需要将一个数转化为radix进制的数,那么当tag等于2的时候就可以直接交换n1和n2,不管tag是几都将n1 转化为radix进制数。接下来就是处理n2了,我们知道如果一个一个进制去试的话肯定是会超时的,那么就可以用到我们以前学过的二分查找了,依次判断中间进制的数是否符合。那么我们如何判断找到可能的最小进制和最大进制呢?我们一起来确定一下,比如说二进制,允许出现最大的数是1,十进制允许出现的最大数是9,。那么我们只需要判断n2里面出现的最大的数加上一不就是最小进制了吗?现在我们再来确定一下最大进制。就比如说,我们前面求出的n1在radix进制数下得到的结果是100,n2是1,那么肯定是符合的因为在以101为进制数的情况下成立,那么可以看出最大的可能进制数和n1在radix进制数下得到的结果有关了,那么是不是就是得到的结果加一呢?肯定是怒能就这么确定的,因为可能出现这种情况。5 a 1 10那么n1得到的值就是5了,而,a所需要的最小进制为11.所以我们应该取两者的最大值。

下面是详细代码


#include<cstring>
#include<string>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int map[200];
long long inf = (1LL << 63)-1;
int findlow(string a){
 int max = 0;
  for(int i = 0;i < a.size();i ++){
    if(max < map[a[i]]){
      max = map[a[i]];
    }
  }
  return max+1;//最小进制
}
long long getnum(string a,long long radix,long long num1){
 long long num = 0;
  for(int i = 0;i < a.size();i++){
    num = num*radix+map[a[i]];
    if(num>num1){//一定要在这里判断一下,要不然会有几个点过不去,可能num越界了
      return -1;
    }
  }
  return num;
}
int cmp(string a,long long radix,long long num1){
 long long num = getnum(a,radix,num1);
  if(num < 0){
    return 1;
  }
  if(num < num1){
    return -1;
  }
  if(num1==num)return 0;
}
long long binarysearch(string a,long long left,long long right,long long num1){
 long long mid;
  while(left <= right){
  mid = (left+right)/2;
    int flag = cmp(a,mid,num1);
    if(flag == -1){
      left = mid+1;
    }else if(flag == 1){
      right = mid-1;
    }else{
      return mid;
    }
  }
  return -1;
}
int main(){
  string n1,n2;
//  freopen("input.txt","r",stdin);
  long long tag,radix;
   cin>>n1>>n2>>tag>>radix;
  for(char ch = '0';ch <= '9';ch++){
    map[ch] = ch - '0';//直接将字符和数对应起来,省的每次都要判断一下,麻烦。
  }
  for(char ch = 'a';ch <= 'z';ch++){
    map[ch] = ch - 'a' + 10;
  }
  if(tag == 2){
    swap(n1,n2);
  }
  long long num1 = getnum(n1,radix,inf);//数据一定是long long
  long long low = findlow(n2);
  long long high = max(num1+1,low);//最大进制
  long long ans = binarysearch(n2,low,high,num1);
  if(ans == -1) printf("Impossible");
    else printf("%lld", ans);
  return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值