乘法逆元:费马小定理(利用快速乘法幂)(JAVA)
这个最后推导出来为
逆元=n^mod-2,因为1000000007是一个质数
//package com.js.datastructure.recursion.蓝桥.国特训练营;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int t = scanner.nextInt();for (int i = 0; i < t; i++) {int n = scanner.nextInt();System.out.println(fp(n,1000000005));}}//快速乘法幂static long fp(long x, long y){long ans = 1;while (y > 0){if((y & 1) == 1){ans = (ans * x) % 1000000007;}y>>=1;x = (x * x) % 1000000007;}return ans;}
}