【蓝桥杯算法】Java的基础API

1. BigInteger 的使用

1.1. 判素数

package 模板;

import java.math.BigInteger;
import java.util.Scanner;

public class 判素数 {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        int q = in.nextInt();
        while (q-- > 0) {
            BigInteger a = new BigInteger(in.next());
            if (a.isProbablePrime(20)) {
                System.out.println(a);
            } else {
                System.out.println(a.nextProbablePrime());
            }
        }
    }
}

1.2. 快速幂

package 模板;

import java.math.BigInteger;
import java.util.Scanner;

public class 快速幂 {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        BigInteger b = new BigInteger(in.next());
        BigInteger p = new BigInteger(in.next());
        BigInteger k = new BigInteger(in.next());

        System.out.println(b.modPow(p, k));
    }
}

1.3. 求逆元

package 模板;

import java.math.BigInteger;
import java.util.Scanner;

public class 逆元 {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        int q = in.nextInt();
        while (q-- > 0) {
            BigInteger a = new BigInteger(in.next());
            BigInteger b = new BigInteger(in.next());
            System.out.println(a.modInverse(b));
        }
    }
}

2. Arrays

  1. 直接对于 int 对象排序使用的是快排
  2. 对于 Integer 对象排序使用的是归并排序
相关推荐
Smark.8 分钟前
Gurobi基础语法之 addConstr, addConstrs, addQConstr, addMQConstr
算法
S-X-S26 分钟前
算法总结-数组/字符串
java·数据结构·算法
linwq830 分钟前
设计模式学习(二)
java·学习·设计模式
桦说编程1 小时前
CompletableFuture 超时功能有大坑!使用不当直接生产事故!
java·性能优化·函数式编程·并发编程
@_@哆啦A梦1 小时前
Redis 基础命令
java·数据库·redis
Joyner20181 小时前
python-leetcode-从中序与后序遍历序列构造二叉树
算法·leetcode·职场和发展
因兹菜1 小时前
[LeetCode]day9 203.移除链表元素
算法·leetcode·链表
LNsupermali1 小时前
力扣257. 二叉树的所有路径(遍历思想解决)
算法·leetcode·职场和发展
雾月551 小时前
LeetCode LCR180文件组合
算法·leetcode·职场和发展
萌の鱼1 小时前
leetcode 2080. 区间内查询数字的频率
数据结构·c++·算法·leetcode