• 1010 Radix 甲级 xp_xht123


    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

    这应该是甲级最恶心的一道题了,以傲人的11%的正确率,屈居榜首

    解题思路:

    坑点一:需要使用long long 才能存的下最终的结果

    有可能有这样一个数在36进制下的数,“ z ...... z ”,中间有很多的z,如果转换成某一进制下的数会非常的大,一定会超出int整型

    坑点二:注意有字母,需要映射到10 ~ 35之间

    坑点三: 由于坑点一的存在,所以答案有可能非常的大,不能使用普通的暴力的搜索,需要使用二分查找

     注意:答案不一定在36以内,只是给定需要转换的数的进制是在2 ~ 36之间的,但是答案一定小于转换出来的目标数。

    因为一个数中的每一位数一定小于进制radix,二分的时候左端点不是0,而是每一位数中最大的那一位 + 1。

    1. #include
    2. #include
    3. using namespace std;
    4. typedef long long ll;
    5. int get(char c)
    6. {
    7. //将数字和字母映射到题目给定的范围
    8. if(c <= '9') return c - '0';
    9. return c - 'a' + 10;
    10. }
    11. ll calc(string n , ll r)
    12. {
    13. ll res = 0;
    14. for(auto c : n)
    15. {
    16. //当给定的数是拥有十位数字的时候最多最多在36进制下是3e15左右的数,所以计算的结果超过1e16就不用再算了
    17. if((double)res * r + get(c) > 1e16) return 1e18;//过大的数用double
    18. res = res * r + get(c);//秦九韶算法
    19. }
    20. return res;
    21. }
    22. int main()
    23. {
    24. string n1 , n2;
    25. cin >> n1 >> n2;
    26. int tag , radix;
    27. cin >> tag >> radix;
    28. if(tag == 2) swap(n1 , n2);//同一化为n1
    29. ll target = calc(n1 , radix);//将n1转换为radix进制下的目标数
    30. //使用二分查找
    31. ll l = 0 , r = target;
    32. //字符串中的每一位数一定小于进制radix
    33. //所以一定要加1
    34. for (auto c : n2) l = max(l, (ll)get(c) + 1);
    35. while(l < r)
    36. {
    37. ll mid = l + r >> 1;
    38. if(calc(n2 , mid) >= target) r = mid; // 说明进制过大
    39. else l = mid + 1;//进制过小
    40. }
    41. if(calc(n2 , l) != target) puts("Impossible");
    42. else cout << l << endl;
    43. }

  • 相关阅读:
    汽车行驶性能的主观评价方法(2)-驾驶员的任务
    html笔记
    界面控件DevExpress WPF流程图组件,完美复制Visio UI!(一)
    【Linux】Ubuntu美化bash【教程】
    阿里云函数计算 1024云上见 AIGC小说创作大赛:如何搭建自定义的阿里通义千问
    百度Echarts实现饼图,较官网示例更多项显示
    51物联:流量卡不能重复购买吗,网上流量卡为什么不能重复购买?
    第十一章 数据库技术 11.1- 术语 11.2-关系数据库的相关术语
    数据结构之【动态数组】
    【总结】坐标变换和过渡矩阵(易忘记)
  • 原文地址:https://blog.csdn.net/xp_xht123/article/details/126351300