• 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. }


  • 相关阅读:
    SSL免费证书会报安全提示吗?
    【allegro 17.4软件操作保姆级教程五】布线前准备之过孔、差分对、布线集合添加
    vue--4.计算属性、属性侦听器、自定义指令、生命周期函数和组件
    外贸谈判过程中,我这样和客户斗智斗勇……
    计算机毕业设计Javaweb网上办公自动化系统(源码+系统+mysql数据库+lw文档)
    26. 删除有序数组中的重复项
    PS4 + ESP32 制作无线遥控器
    Jmeter集成到jenkins
    【寸铁的刷题笔记】图论、bfs、dfs
    C++宏的用法
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127638895