zoukankan      html  css  js  c++  java
  • 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
    ",ub);
                }
                else puts("Impossible");
        }
        return 0;
    }
  • 相关阅读:
    数据恢复:模拟2个逻辑坏块
    Oracle Internal Event:10201 consistent read undo application诊断事件
    Enterprise Manager Cloud Control 12.1.0.1 (12c)安装指南
    Script:GoldenGate For Oracle数据库预检查脚本
    OGG常见初始化方案
    expdp+compression性能测试
    了解AMDU工具生成的MAP文件
    何时会发生db file sequential read等待事件?
    Oracle发布公共云Public Cloud
    Oracle Internal Research深入研究Oracle内部原理
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/8538946.html
Copyright © 2011-2022 走看看