2019 ICPC Asia Yinchuan Regional I. Base62(高精度/BigInteger)

As we already know, base64 is a common binary-to-text encoding scheme. Here we define a special series of positional systems that represent numbers using a base (a.k.a. radix) of 2 to 62. The symbols '0' -- '9' represent zero to nine, and 'A' -- 'Z' represent ten to thirty-five, and 'a' -- 'z' represent thirty-six to sixty-one. Now you need to convert some integer z in base x into base y.

Input

The input contains three integers x, y (2≤x,y≤62) and z(0≤z<x120), where the integer z is given in base x.

Output

Output the integer zz in base yy.

样例输入复制

16 2 FB

样例输出复制

11111011

简单的进制转换题目,不过需要用高精。C++的高精模版有可能会爆,所以可以用Java的大整数类来写(真香

注意判一下输入的z是0的情况。

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static int get(char c) {
        if(c <= '9') return (c - '0');
        else if(c <= 'Z') return 10 + (c - 'A');
        else return 36 + (c - 'a');
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger base1, base2, a;
        String s;
        base1 = sc.nextBigInteger();
        base2 = sc.nextBigInteger();
        s = sc.next();
        a = BigInteger.ZERO;
        for(int i = 0; i < s.length(); i++) {
            char now = s.charAt(i);
            a = a.multiply(base1);
            String xx = String.valueOf(get(now));
            BigInteger x = new BigInteger(xx);
            a = a.add(x);
        }
        if(a == BigInteger.ZERO) {
            System.out.println("0");
            return;
        }
        ArrayList<BigInteger> arr = new ArrayList<>();
        while(a != BigInteger.ZERO) {
            BigInteger aa = a;
            aa = aa.divide(base2);
            aa = aa.multiply(base2);
            BigInteger now = a.subtract(aa);
            arr.add(now);
            a = a.divide(base2);
        }

        for(int i = arr.size() - 1; i >= 0; i--) {
            BigInteger tmp = arr.get(i);
            int now = tmp.intValue();
            if(now <= 9) System.out.print(now);
            else if(now <= 35) System.out.print((char)(now - 10 + 'A'));
            else System.out.print((char)(now - 36 + 'a'));
        }
    }
}

posted @   脂环  阅读(116)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
主题色彩