• 1010 Radix


    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:

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

    题目大意

    给我们N1,N2,tag,radix四个数,tag为1表示N1为radix进制,反之N2为radix进制

    让我们求另外一个数为几进制时,可以让 N1=N2成立


    思路

    首先确保N1与N2其中一个是固定已知的进制数(方便后续判断)

    即tag=1时候保持不变,tag为2时交换N1与N2

    num1 = N1 为 radix 进制时转换而来的10进制

    接着二分查找符合要求的进制

    OP(num,op),将op进制的N2转换为十进制的key,注意这里可能会溢出,所有只要溢出(key为负数),就算N2在op进制下比num1大

    注意如果不唯一输出最小情况,如果当前op进制下的N2已经等于key了,我们仍需继续往下判断


    C/C++ 

    1. #include
    2. using namespace std;
    3. typedef long long LL;
    4. LL OP(const string& num,LL op); // 将num转换为op进制
    5. int main()
    6. {
    7. string N,N1,N2;
    8. LL op1,op2;
    9. cin >> N1 >> N2 >> op1 >> op2;
    10. if(op1==2) N = N2, N2 = N1 , N1 = N;
    11. LL num1 = OP(N1,op2);
    12. char flag = *max_element(N2.begin(),N2.end()); // 找到N2中最大的那个字符
    13. LL head = isdigit(flag) ? flag - 47 : flag - 86;
    14. LL tail = max(num1,head),result=-1;
    15. while (head<=tail){
    16. LL avg = (head+tail)/2;
    17. LL key = OP(N2,avg);
    18. if(key==num1){
    19. result = avg;
    20. tail = avg-1;
    21. }else if(key<=0 || key>num1) tail = avg-1;
    22. else head = avg+1;
    23. }
    24. if(result==-1) cout << "Impossible" << endl;
    25. else cout << result << endl;
    26. return 0;
    27. }
    28. LL OP(const string& num,LL op)
    29. {
    30. LL result = 0;
    31. for(char ch : num){
    32. int key = ch - 48;
    33. if(ch>='a' && ch<='z') key = 10 + ch - 97;
    34. result = result * op + key;
    35. }
    36. return result;
    37. }


  • 相关阅读:
    Go十大常见错误第6篇:slice初始化常犯的错误
    Windows下通过CMake编译项目(2种方法)
    rpm安装postgresql12
    基于SkyEye运行Qt:著名应用程序开发框架
    【rabbitMQ】-延迟队列-模拟控制智能家居的操作指令
    企业数字化转型从哪些系统入门比较好
    Games104现代游戏引擎笔记高级ai
    Ubuntu 18.04/20.04 LTS 操作系统设置静态DNS
    解决Intellij IDEA @Test没有提示问题
    自动驾驶:未来的道路上的挑战与机遇
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127638895