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

坑点:
  
1、所求的进制值不唯一,所以在使用二分法查找的时候,当两个数相等的时候不能停止查找,而应继续向进制数减小的方向去查找
2、我刚开始用的是直接从最小进制开始遍历,只有测试点10超时
3、最大进制值并不是36,我最初以为是36,其他人给的答案是max(各位数最大值加1,第一位数的十进制值)
#include<iostream>
#include<sstream>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstdio>
using namespace std;
//把第一个数字转化为对应的进制数
long long int toN(int a[],int n,int radix)
{
    long long int decimal=0;
    for(int i=0; i<n; i++)
    {
        decimal+=a[i]*pow(radix,n-i-1);
    }
    //cout<<decimal<<endl;
    return decimal;
}
//把第二个数据转化为对应的进制数,在转化过程中,如果
//已经大于待比较数字decimal或者已经溢出,则直接返回,比较好后的结果
long long int toN2(int a[],int n,int radix,long long int& compare)
{
    long long int decimal=0,temp=0;
    for(int i=0; i<n; i++)
    {   
        temp=decimal;
        decimal+=a[i]*pow(radix,n-i-1);
        if(decimal>compare||temp>decimal)//溢出判断
            return compare+1;
    }
    //cout<<decimal<<endl;
    return decimal;
}

void toArray(string str,int a[],int &maxValue)
{
    maxValue=0;
    for(int i=0; i<str.length(); i++)
    {
        if(str[i]>='0'&&str[i]<='9')
            a[i]=str[i]-'0';
        else
            a[i]=str[i]-'a'+10;
        //cout<<a[i]<<" ";
        maxValue=max(a[i],maxValue);
    }
    maxValue++;
}

int main()
{
    string n1,n2;
    int tag,radix;
    cin>>n1>>n2>>tag>>radix;
    if(tag==2)
        swap(n1,n2);
    int a[n1.length()];
    int b[n2.length()];
    int minRadix=0;
    toArray(n1,a,minRadix);
    long long int decimal=toN(a,n1.length(),radix);
    toArray(n2,b,minRadix);
    //cout<<minRadix<<endl;
    int flag=-1;
    long long int high=decimal>minRadix?decimal:minRadix;
    int low=minRadix;
    while(low<=high)
    {
        long long mid=(low+high)/2;
        long long int temp=toN2(b,n2.length(),mid,decimal);
        // cout<<mid<<endl;
        if(temp<decimal)
            low=mid+1;
        else if(temp==decimal)
        {
            flag=mid;
            high=mid-1;
        }
        else
        {
            high=mid-1;
        }
    }

    if(flag==-1)
        cout<<"Impossible";
    else
        cout<<flag;
    return 0;
}

 

posted on 2019-01-01 20:35  ZhangのBlog  阅读(789)  评论(2编辑  收藏  举报