【蓝桥杯算法】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 对象排序使用的是归并排序
相关推荐
生锈的键盘几秒前
推荐算法实践:交叉特征的理解
算法
一个网络学徒2 分钟前
python5
java·服务器·前端
workflower6 分钟前
业务需求-假设场景
java·数据库·测试用例·集成测试·需求分析·模块测试·软件需求
乌萨奇也要立志学C++13 分钟前
【洛谷】BFS 求解最短路:从马的遍历到迷宫问题的实战解析
算法·宽度优先
老鼠只爱大米22 分钟前
LeetCode经典算法面试题 #46:全排列(回溯、交换、剪枝等五种实现方案详细解析)
算法·leetcode·剪枝·回溯·全排列·stj算法
专注VB编程开发20年23 分钟前
vb.net datatable新增数据时改用数组缓存
java·linux·windows
(>_<)26 分钟前
java minio 分片上传工具类与测试demo
java·minio·分片上传
不想打工的码农27 分钟前
MyBatis-Plus多数据源实战:被DBA追着改配置后,我肝出这份避坑指南(附动态切换源码)
java·后端
Dovis(誓平步青云)35 分钟前
《滑动窗口算法:从 “暴力遍历” 到 “线性高效” 的思维跃迁》
运维·服务器·数据库·算法
Coder_Boy_42 分钟前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring