1010 Radix

BigInteger:

java 复制代码
import java.util.Scanner;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String N1 = in.next();
        String N2 = in.next();
        int tag = in.nextInt();
        long radix = in.nextLong();  // 初始进制

        BigInteger tgt = BigInteger.ZERO;
        if (tag == 1) {
            tgt = convert10(N1, radix);
        } else {
            tgt = convert10(N2, radix);
        }

        String process = (tag == 1) ? N2 : N1;
        BigInteger res = BigInteger.valueOf(-1);
        long maxDigit = getRange(process) + 1;  // 可能的最小基数
        BigInteger l = BigInteger.valueOf(maxDigit);
        BigInteger r = tgt.max(BigInteger.valueOf(maxDigit));

        while (l.compareTo(r) <= 0) {  // 左闭右闭区间
            BigInteger mid = l.add(r).divide(BigInteger.TWO);
            BigInteger cur = convert10(process, mid.longValue());
    
            if (cur.equals(tgt)) {
                res = mid;
                break;
            } else if (cur.compareTo(tgt) > 0) {
                r = mid.subtract(BigInteger.ONE);  // cur 超过 tgt 或发生溢出,缩小右边界
            } else {
                l = mid.add(BigInteger.ONE);  // cur 小于 tgt,增加左边界
            }
        }

        if (res.equals(BigInteger.valueOf(-1))) {
            System.out.println("Impossible");
        } else {
            System.out.println(res);
        }
    }

    // 将字符串 s 按照 radix 进制转换为 10 进制数
    public static BigInteger convert10(String s, long radix) {
        BigInteger res = BigInteger.ZERO;
        BigInteger base = BigInteger.ONE;
        BigInteger bigRadix = BigInteger.valueOf(radix);
        
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);
            BigInteger value;
            if (Character.isDigit(c)) {
                value = BigInteger.valueOf(c - '0');
            } else {
                value = BigInteger.valueOf(c - 'a' + 10);
            }

            res = res.add(value.multiply(base));
            base = base.multiply(bigRadix);
        }

        return res;
    }

    // 获取字符串中最大的字符所代表的数值,用于确定最小的可能进制
    public static long getRange(String s) {
        long maxDigit = -1;
        for (char c : s.toCharArray()) {
            if (Character.isDigit(c)) {
                maxDigit = Math.max(maxDigit, c - '0');  // 数字字符
            } else if (Character.isLetter(c)) {
                maxDigit = Math.max(maxDigit, c - 'a' + 10);  // 字母字符
            }
        }
        return maxDigit;
    }
}

long:

java 复制代码
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String N1 = in.next();
        String N2 = in.next();
        int tag = in.nextInt();
        long radix = (long) in.nextInt();
        long tgt = -1;
        if(tag == 1){
            tgt = convert10(N1, radix);
        }else{
            tgt = convert10(N2, radix);
        }
        String process = tag == 1 ? N2 : N1;
        long res = -1;
        long l = getRange(process) + 1;
        long r = Math.max(tgt, l);
        while(l <= r){//左闭右闭
            long mid = (l+r) >> 1;
            long cur = convert10(process, mid);
            if(cur == tgt){
                res = mid;
                break;
            }else if(cur == -1 || cur > tgt){
                r = mid - 1;
            }else{
                l = mid + 1;
            }
        }
        if(res == -1){
            System.out.println("Impossible");
        }else{
            System.out.println(res);
        }
    }
    public static long convert10(String s, long radix){
        char[] ch = s.toCharArray();
        long res = 0;
        long base = 1;
        for(int i = ch.length-1; i >= 0; i--){
            if(0 <= ch[i]-'0' && ch[i]-'0' <= 9){
                res += base * (ch[i]-'0');
            }else if(0 <= ch[i]-'a' && ch[i]-'a' <= 25){
                res += base * (ch[i]-'a'+10);
            }
            base *= radix;
            if(res < 0 || base < 0){
                return -1;
            }
        }
        return res;
    }
    public static long getRange(String s) {
        char[] ch = s.toCharArray();
        long maxDigit = -1;
        for (char c : ch) {
            if (Character.isDigit(c)) {
                maxDigit = Math.max(maxDigit, c - '0');  // 处理数字
            } else if (Character.isLetter(c)) {
                maxDigit = Math.max(maxDigit, c - 'a' + 10);  // 处理字母
            }
        }
        return maxDigit;
    }
}
相关推荐
格林威4 小时前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
程序员莫小特6 小时前
老题新解|大整数加法
数据结构·c++·算法
过往入尘土7 小时前
服务端与客户端的简单链接
人工智能·python·算法·pycharm·大模型
zycoder.7 小时前
力扣面试经典150题day1第一题(lc88),第二题(lc27)
算法·leetcode·面试
蒙奇D索大7 小时前
【数据结构】考研数据结构核心考点:二叉排序树(BST)全方位详解与代码实现
数据结构·笔记·学习·考研·算法·改行学it
智驱力人工智能8 小时前
工厂抽烟检测系统 智能化安全管控新方案 加油站吸烟检测技术 吸烟行为智能监测
人工智能·算法·安全·边缘计算·抽烟检测算法·工厂抽烟检测系统·吸烟监测
程序员爱钓鱼9 小时前
Go语言实战案例——进阶与部署篇:编写Makefile自动构建Go项目
后端·算法·go
_Power_Y9 小时前
Java面试常用算法api速刷
java·算法·面试
艾醒(AiXing-w)9 小时前
大模型面试题剖析:模型微调中冷启动与热启动的概念、阶段与实例解析
人工智能·深度学习·算法·语言模型·自然语言处理
天选之女wow9 小时前
【代码随想录算法训练营——Day32】动态规划——509.斐波那契数、70.爬楼梯、746.使用最小花费爬楼梯
算法·leetcode·动态规划