【蓝桥杯算法】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 对象排序使用的是归并排序
相关推荐
孟陬3 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌3 小时前
一站式了解四种限流算法
java·后端·go
华仔啊4 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
也些宝5 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java
Gorway5 小时前
解析残差网络 (ResNet)
算法
拖拉斯旋风5 小时前
LeetCode 经典算法题解析:优先队列与广度优先搜索的巧妙应用
算法
Wect5 小时前
LeetCode 207. 课程表:两种解法(BFS+DFS)详细解析
前端·算法·typescript
Nyarlathotep01135 小时前
SpringBoot Starter的用法以及原理
java·spring boot
wuwen55 小时前
WebFlux + Lettuce Reactive 中 SkyWalking 链路上下文丢失的修复实践
java