PAT1010(二分查找求二进制)

  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;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值