PAT1010(二分查找求二进制)

来源:互联网 发布:excel数据链接更新 编辑:程序博客网 时间:2024/06/05 22:30
  1. 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

此题的坑点挺多:
1>如果单纯的暴力,没有给明确的进制要求,所以一定会爆int,因为是十位,所以不会爆long long,题目的描述是:给定一个确定进制的数,问是否存在某一个进制使得的数字等于给定的数字;题目没有给定最大进制数,采用long long合适。
2>错误做法:从最小进制下界从二开始暴力求,不是,题意中给出最小进制下界是所有位最大的数加一
3>就是下界确定也不一定能过,测试点7会超时,这样就可以想到二分来进行找适合的二进制数,上界最大也只可能是已知的那个数 + 1;
4>步骤:
用long long
找最小下界,最大上界,二分查找匹配进制,输出

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<cmath>using namespace std;map<char,int>mp;long long num1,num2;//害怕函数库的函数进制大时结果会不准确long long Pow(int n,long long m){    if(m == 0){        return 1;    }    else{        long long x = 1;        for(long long i = 1;i <= m;++i)        {            x *= n;        }        return x;    }}//将字符串按进制转换成十进制long long CharTolonglong(char *s,long long radix){    long long p = 0;    for(int i = strlen(s) - 1;i >= 0;--i)    {        p += mp[s[i]] * Pow(radix,strlen(s) - 1 - i);        if(Pow(radix,strlen(s) - 1 - i) < 0){            return -1;        }        //计算进制的过程中会溢出,溢出返回-1        if(p < 0){            return -1;        }    }    return p;}int main(){    char s1[12];    char s2[13];    for(int i = 0;i <= 9;++i)    {        mp['0' + i] = i;    }    for(int i = 10;i <= 35;++i)    {        mp['a' + i - 10] = i;    }    scanf("%s %s",s1,s2);    long long tag,radix;    cin >> tag >> radix;    char s[13];    long long left = 0,right = 0;    if(tag == 1){       //找二分的时候的最小上界和最大上界        for(int i = strlen(s2) - 1;i >= 0;--i)        {            if(left < mp[s2[i]]){                left = mp[s2[i]];            }        }        left++;        num1 = CharTolonglong(s1,radix);        strcpy(s,s2);        right = num1 + 1;    }    if(tag == 2){        for(int i = strlen(s1) - 1;i >= 0;--i)        {            if(left < mp[s1[i]]){                left = mp[s1[i]];            }        }        left++;        num1 = CharTolonglong(s2,radix);        strcpy(s,s1);        right = num1 + 1;    }    //cout << left << " " << right << endl;    long long ans = 0;    while(left <= right)    {        long long mid = (left + right) / 2;        num2 = CharTolonglong(s,mid);        if(num2 < 0 || num2 > num1){            right = mid - 1;        }        else{            if(num2 < num1){                left = mid + 1;            }            else{                ans = mid;                break;            }        }    }    if(!ans){        cout << "Impossible" << endl;    }    else{        cout << ans << endl;    }    return 0;}