zoukankan      html  css  js  c++  java
  • All about that base

    The base (or radix) of a positional numeral system is the number of symbols that can be used to represent a number in that system. The base 1010 system (also known as decimal) uses 1010 distinct symbols: 0,1,dots ,90,1,,9. For example, we interpret the number 7234572345 as:

    7 imes10^4+2 imes10^3+3 imes10^2+4 imes10^1+5 imes10^0.7×104+2×103+3×102+4×101+5×100.

    This example illustrates that in base 1010 the symbol at place Pgeq0P0 (starting from the right) is multiplied by 10^P10P to get its value. More generally, in base BB we use BB symbols to represent 0,dots,B-10,,B1, and the symbol at the P ext{th}Pth place is multiplied by B^PBP to get its value.

     

    Other bases commonly used in computation include base 22 (or binary, using symbols 00 and 11), base 88 (or octal, using symbols 00–77), and base 1616 (or hexadecimal, using symbols 00–99 and aa–ff). In bases higher than 1010, letters represent the higher values. Thus in hexadecimal aa–ff represent the decimal values 1010–1515, and in bases geq3636 the letter zz represents the decimal value 3535.

    Your job is to determine the bases in which given arithmetic expressions are valid. We define an expression as valid in base BB if two conditions are true. First, all the operands used are interpretable in base BB as having values in the decimal range [1,2^{32}-1][1,2321]. Second, the expression is true. Any arbitrary expression might be valid in zero, one, or more bases. In this problem we will only consider bases 11–3636, where base 11 is unary.

    Note that following the convention listed above, unary would consist of a single symbol: 00. In this problem, unary numbers use the symbol 11 rather than 00 (think "tally marks").  ext{E.g.}E.g., 111111 in unary is equivalent to the decimal number 33 and 11111111111111 in unary is equivalent to the decimal number 77.

    Input

    Input for this problem starts with a line containing an integer 0leq Nleq200N20. The following NN lines each contain an arithmetic expression with the following form:

    X op Y = ZX op Y = Z

    where XX, YY , and ZZ are positive, whole numbers consisting of 11 to 100100 symbols from the set 00–99 and aa–zz, and opop is one of the four operators ++, -−, *∗, //. For each statement there is at least one base 1leq Bleq 361B36 such that XX, YY , and ZZ can all be interpreted in base BB as having values in the decimal range [1,2^{32}-1][1,2321].

     

    Output

    For each expression, list the bases in which the expression is valid (sorted in ascending base order) or the word "invalid" if the expression not valid in any of the bases 11–3636. Use symbols 11–99, then aa–zz, then 00 to represent bases 11–3636 (with the last symbol, 00, representing base 3636).

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入

    8
    6ef + d1 = 7c0
    3 / 2 = 1
    444 / 2 = 222
    10111 * 11 = 1000101
    10111 * 11 = 111221
    5k - 1z = 46
    1111111111 - 1111111 = 111
    2048 - 512 = 1536

    样例输出

    g
    invalid
    56789abcdefghijklmnopqrstuvwxyz0
    2
    3456789abcdefghijklmnopqrstuvwxyz0
    invalid
    1
    a

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define ull             unsigned long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1e9 + 7;
    const int N = 10000;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    ull a, b, c, flag; 
    char op;
    string A, B, C;
    vector<int> valid; 
    inline int c2i(char x) 
    {
        if (x >= '0'&&x <= '9') 
            return x - '0';
        return x - 'a' + 10; 
    }
    inline char i2c(int x) 
    { 
        if (x == 36) 
            return '0';
        if (x >= 10) 
            return x - 10 + 'a';
        return x + '0'; 
    }
    ull gao(int base, string s) 
    {
        ull ret = 0; for (int j = 0; s[j]; j++)
        { 
            ret *= base, ret += c2i(s[j]);
            if (ret >= 1ll << 32)
            {
                flag = 0; break;
            } 
        }
        return ret;
    }
    void solve()
    { 
        int low = 1, a1 = 0;
        valid.clear(); 
        char equal;
        cin >> A >> op >> B >> equal >> C;
        for (int i = 0; A[i]; i++)
            low = max(low, c2i(A[i])), a1 += A[i] == '0';
        for (int i = 0; B[i]; i++)
            low = max(low, c2i(B[i])), a1 += B[i] == '0';
        for (int i = 0; C[i]; i++)
            low = max(low, c2i(C[i])), a1 += C[i] == '0'; 
        if (a1 || low != 1) 
            low++; 
        for (int i = low; i <= 36; i++) 
        { 
            flag = 1;
            a = gao(i, A), b = gao(i, B), c = gao(i, C);
            if (!flag)
                continue; 
            if (op == '+'&&a + b == c
                || op == '*'&&a*b == c
                || op == '-'&&a - b == c
                || op == '/'&&b != 0 
                && a%b == 0 && a / b == c) 
                valid.push_back(i); 
        }
        if (valid.empty())
            printf("invalid");
        else for (auto c : valid)
            printf("%c", i2c(c));
        printf("
    ");
    }
    int main() 
    { 
        int _;
        scanf("%d", &_);
        while (_--) 
            solve(); 
        return 0; 
    }
  • 相关阅读:
    字符编码与解码详解
    【Java反射机制】用反射改进简单工厂模式设计
    数据结构
    根据 中序遍历 和 后序遍历构造树(Presentation)(C++)
    【动态规划】记忆搜索(C++)
    Linux环境下安装中山大学东校区iNode客户端
    webpack前端开发环境搭建
    CSS中line-height继承问题
    MySQL中MyISAM与InnoDB的主要区别对比
    JavaScript中易混淆的DOM属性及方法对比
  • 原文地址:https://www.cnblogs.com/dealer/p/12736620.html
Copyright © 2011-2022 走看看