思路1
由于2022太大,用double来存储,即(5+2022 % 7) % 7即可
int num = 5;int t = (int)(Math.pow(20,22)%7);num +=t;num%=7;System.out.println(num+1);
思路2
你需要知道 (a * b ) % p = a % p * b % p
Scanner scan = new Scanner(System.in);int num = 1;for(int i = 1; i <= 22; i++)// (a * b ) % p = a % p * b % p// 这里再加上一个%p是担心 乘积会溢出num = (num %7 * 20 % 7) % 7;num = (num + 5) % 7 ;System.out.println(num+1);scan.close();
思路3
java中有一个类是专门用来进行大整数运算的:BigInteger
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {BigInteger res = new BigInteger("5");BigInteger product = new BigInteger("20");BigInteger twenty_two = new BigInteger("22");BigInteger seven = new BigInteger("7");//等同于 (20 ^22^ % 7 + 5)%7int i = product.modPow(twenty_two, seven).add(res).mod(seven).intValue();System.out.println(i+1);}
}