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 N​1​​ and N​2​​, 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

 前提:首先为了表达方便 a为已经给了进制的数,b是求进制的数
总体思路:当求出a的10进制的数值后,对剩下的b,我们就需要代入进制进行尝试了
需要注意的点1:需要注意的点:long long int是必须的,int太小了。另外肯定要使用二分法。
需要注意的点2:对于两位数及以上的位的数,找到满足进制就是最小进制了,但是对于一位数,我们就需要小心了,答案不唯一如测试点8 8 1 10输出为9 。
需要注意的点3:对于二分法的上下限,下限应该是a中每一位数字中最大的那个数再加上1(AC代码中输出进制可以是1)。上限 可以直接是a的10进制数值(这里就又有个问题,题目给出的数字转换为10进制后,应该是不超过long long int范围的,反正经过测试没超过,但是你把 上限的数值当成进制带入b很可能超过了long long int范围了成负数了,所以直接用负数判断)。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <sstream>


#include <climits>

using namespace std;
const int inf=600*600;

long long out(string stra,long long  jinzhi) {
    long  long outNum=0;
    long  long wei=1;
    for(int i=stra.size()-1; i>=0; i--) {
        int num=stra[i]-'0';
        if(isalpha(stra[i]))
            num=stra[i]-'a'+10;
        outNum+=num*wei;
        wei*=jinzhi;
    }
    return outNum;
}

int main() {

    string stra,strb;
    cin>>stra>>strb;
    int sign;
    cin>>sign;
    int jinzhi;
    cin>>jinzhi;
    if(sign==2) {
        string str=strb;
        strb=stra;
        stra=str;
    }
    long long outNum=out(stra,jinzhi);
//outNum是已知进制数字转换为10进制对应的数值
    int strbMax=-1;//标记数字 所有位数上最大的那个数字,比如123 就是3
    for(int i=0; i<strb.size(); i++) {
        int num=strb[i]-'0';
        if(isalpha(strb[i]))
            num=strb[i]-'a'+10;
        strbMax=max(strbMax,num);
    }

    long long a=strbMax+1;//下限肯定需要加1否则就该进位了
    long long b=max(a,outNum);//注意的第一个点,防止就一位数字 一位数字outnum 比a还小可能
    //比如 8 8 1 10   ,我们下限是9 但是outnum是8,这个时候我们上界应该是9的
    //对于两位以上数字,找到一样的数值缩对应进制就是最小的进制了,因为就一种情况才满足
    while(a<=b) {
        long long i=(a+b)/2;
        long long get=out(strb,i);
        if(get==outNum) {
            cout<<i;
            return 0;
        } else if(get>outNum||get<0) {//这里也要注意,get可能超过Long long范围
            b=i-1;
        } else if(get<outNum) {
            a=i+1;
        }

    }

    cout<<"Impossible";

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值