PAT 甲级 1010. Radix

来源:互联网 发布:软件著作权发表日期 编辑:程序博客网 时间:2024/06/08 08:38

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

题意:给定两个数字串,每个串最多10位,给定其中一个数字串的进制数,求另一个数字的进制数,不存在就输出Imposiialbe

思路:起初太年轻了,没有考虑到特殊样例,所以错误以为进制最大是36,然后错了好久,后来发现了这样的一组样例 2147483647 1 1 10

这样的话只能2分了答案了,因为需要找到最小符合的进制数,像是这样的数据 9 9 1 10,所以2分找到了答案还不行,还需要继续往前面找;

我最后的时候卡了一组小样例,因为实现姿势不好,所以会超longlong,于是就用了 unsigned long long

#include<bits/stdc++.h>using namespace std;typedef unsigned long long LL;LL minn,maxn;int main(){    string num[2];    int tag,radix;    cin >> num[0] >> num[1] >> tag >> radix;    int l[2];    l[0] = num[0].length();    l[1] = num[1].length();    tag--;    LL ans[2] = {0};    for(int i = 0;i < l[tag];i++)    {        if(isdigit(num[tag][i]))            ans[tag] = ans[tag] * radix + (num[tag][i] - '0');        else            ans[tag] = ans[tag] * radix + (num[tag][i] - 'a' + 10);    }    minn = 0;    for(int i = 0;i < l[!tag];i++)    {        if(isdigit(num[!tag][i]))            minn = max(minn,LL(num[!tag][i] - '0'+1));        else            minn = max(minn,LL(num[!tag][i] - 'a'+10+1));    }    maxn = max(ans[tag]+1,minn);    LL mid,L = minn,R = maxn;    while(L < R)    {        //cout << L << " " << R <<endl;        ans[!tag] = 0;        mid = (L + R)/2;        bool ok = false;        for(int i = 0;i < l[!tag];i++)        {            if(isdigit(num[!tag][i]))                ans[!tag] = ans[!tag] * mid + (num[!tag][i] - '0');            else                ans[!tag] = ans[!tag] * mid + (num[!tag][i] - 'a' +10);            if(ans[!tag] > ans[tag])            {                break;            }        }        //cout << L << " " <<R << " " << ans[!tag] <<endl;        if(ans[!tag] < ans[tag])            L = mid + 1;        else if(ans[!tag] == ans[tag])            R = mid;        else            R = mid - 1;    }    ans[!tag] = 0;    for(int i = 0;i < l[!tag];i++)        {            if(isdigit(num[!tag][i]))                ans[!tag] = ans[!tag] * L + (num[!tag][i] - '0');            else                ans[!tag] = ans[!tag] * L + (num[!tag][i] - 'a' +10);        }    //cout << L << " " <<ans[!tag] <<endl;    if(ans[!tag] != ans[tag])        printf("Impossible\n");    else        printf("%d\n",L);    return 0;}


原创粉丝点击