【蓝桥杯算法】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 对象排序使用的是归并排序
相关推荐
huohaiyu15 分钟前
Hashtable,HashMap,ConcurrentHashMap之间的区别
java·开发语言·多线程·哈希
信奥卷王1 小时前
[GESP202503 五级] 原根判断
java·数据结构·算法
兮山与1 小时前
算法4.0
算法
nju_spy1 小时前
力扣每日一题(二)任务安排问题 + 区间变换问题 + 排列组合数学推式子
算法·leetcode·二分查找·贪心·排列组合·容斥原理·最大堆
初听于你1 小时前
高频面试题解析:算法到数据库全攻略
数据库·算法
翟天保Steven1 小时前
ITK-基于Mattes互信息的二维多模态配准算法
算法
小咕聊编程1 小时前
【含文档+源码】基于SpringBoot的过滤协同算法之网上服装商城设计与实现
java·spring boot·后端
代码对我眨眼睛1 小时前
226. 翻转二叉树 LeetCode 热题 HOT 100
算法·leetcode·职场和发展
Zz_waiting.1 小时前
Spring 原理
java·spring·spring自动管理
黑色的山岗在沉睡2 小时前
LeetCode 494. 目标和
算法·leetcode·职场和发展