pat 甲级 1010. Radix (25)

1010. 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

题意:已知其中一个数的字码以及进制和另一个数的字码,推断另一个数的进制。
思路:首先有可能出现的最大进制不是36,可能会很大,所以直接暴力搜索的话很可能会卡时,所以需要二分搜索。这样我们只需确定另一个数字进制出现的可能范围[max_radix,min_radix]内进行二分搜索就行。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30+5
#define M_MAX 2001
typedef unsigned long long ll;
int tag,radix;
struct Number {
    string number;
    ll base;
}num[2];
bool known , unknown;
ll sum1, sum2;
ll translate_to_10(string s,ll base) {//转化成十进制
    ll sum = 0;
    for (int i = 0; i < s.size();i++) {
        if (s[i] >= '0'&&s[i] <= '9') {
            sum = sum*base + s[i] - '0';
        }
        else if (s[i] >= 'a'&&s[i] <= 'z') {
            sum = sum*base + s[i] - 'a' + 10;
        }
    }
    return sum;
}

bool C(ll x) {
    ll sum = translate_to_10(num[unknown].number, x);
    if (sum<0)return true;//太大导致数值溢出
    if (sum >= sum1)return true;
    else return false;
}

int main() {
    while (cin>>num[0].number>>num[1].number>>tag>>radix) {
            known = !(tag & 1),unknown=!known;
            num[known].base = radix;
            sum1 = translate_to_10(num[known].number, num[known].base);
            int strart_base = 2; string s = num[unknown].number;
            for (int i = 0; i < s.size(); i++) {//确定未知数至少是多少进制
                if (s[i] >= '2'&&s[i] <= '9'&&strart_base < s[i] - '0'+1)
                    strart_base = s[i] - '0'+1;
                else if (s[i] >= 'a'&&s[i] <= 'z'&&strart_base < s[i] - 'a' + 10+1)
                    strart_base = s[i] - 'a' + 10+1;
            }
            ll lb = strart_base-1, ub = sum1 + 1;
            if (translate_to_10(num[unknown].number, lb) > sum1) { puts("Impossible"); continue; }//剪枝
            while (ub-lb>1) {
                ll mid = (lb + ub) >> 1;
                if (C(mid))ub = mid;
                else lb = mid;
            }
            if (translate_to_10(num[unknown].number, ub) == sum1){
                printf("%lld\n",ub);
            }
            else puts("Impossible");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/ZefengYao/p/8538946.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值