1010

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


题意:给定两个数,其中一个知道进制,求另一个的进制,使得两个数相等。
思路:(看别人代码和分析才懂 -_-!!)
二分查找区间[maxdigit(N2)+1, N1+1]
①N1,N2相同:
都为0,radix2 = 1or2 ?,查找区间[1,1] OK?
都为1,radix2 = 2, 查找区间[2,2] OK
其他,radix2 = maxdigit(N2) + 1,查找区间[maxdigit(N2)+1, N1+1] OK
②N1,N2不同:
N1 = 0, Impossible, 查找区间[maxdigit(N2)+1, 1] (直接返回-1) OK
N1不为0:
maxdigit(N2)+1 < N1+1时,Impossible,[maxdigit(N2)+1, N1+1]返回-1 OK
maxdigit(N2)+1 >= N1+1时, 二分查找[maxdigit(N2)+1, N1+1],
为什么:如果N2是一个一位数,那么无论是什么进制都没有用,如果N2是10,那么radix2正好等于N1,如果N2再大点,radix2等于N1+1肯定大于N1了。OK

#include <stdio.h>
#include <string.h>

long long ToDecimal(char N[], long long radix);
int MaxDigit(char N[]);
int cmp(char N[], long long radix, long long N1);
long long BinSearch(char N[], long long low, long long up, long long N1);

char N1[11];
char N2[11];
char tmp[11];
long long map[256];
int tag;
int radix;

int main()
{
    char c;
    for(c = '0'; c <= '9'; c++)
    {
        map[c] = c - '0';
    }
    for(c = 'a'; c <= 'z'; c++)
    {
        map[c] = c - 'a' + 10;
    }
    scanf("%s %s %d %d", N1, N2, &tag, &radix);
    if(tag == 2)
    {
        strcpy(tmp, N1);
        strcpy(N1, N2);
        strcpy(N2, tmp);
    }
    long long n1 = ToDecimal(N1, radix); //N1换成十进制 
    long long low = MaxDigit(N2) + 1; 
    long long up = n1 + 1; 
    long long ret = BinSearch(N2, low, up, n1);
    if(ret == -1)
        printf("Impossible");
    else
        printf("%lld", ret);

    return 0;
}


long long ToDecimal(char N[], long long radix)
{
    long long value = 0;
    int len = strlen(N);
    int i;
    for(i=0;i<len;i++)
    {
        value = value * radix + map[N[i]];
    }
    return value;
}

int MaxDigit(char N[])
{
    int max = -1;
    int len = strlen(N);
    int i;
    for(i=0;i<len;i++)
    {
        if(map[N[i]] > max)
            max = map[N[i]];
    }
    return max;
}

int cmp(char N[], long long radix, long long N1)
{
    int ret;
    long long value;
    value = ToDecimal(N, radix);
    if(value <0 || value > N1)
        ret = 1;
    else if(N1 > value)
        ret = -1;
    else
        ret = 0;
    return ret;
}

long long BinSearch(char N[], long long low, long long up, long long N1)
{
    long long mid;
    long long value = -1;
    while(low <= up)
    {
        mid = (low + up) / 2;
        int flag;
        flag = cmp(N, mid, N1);
        if(flag == 0)
        {
            value = mid;
            break;
        }
        else if(flag == -1)
            low = mid +1;
        else
            up = mid -1;
    }
    return value;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值