经典的分数取模,不能直接除,要使用模逆元改为相乘的形式,然后a乘b的模逆元后还要加上模数,因为a可能是负数,这是取余的一个性质,最后还要再取模
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int p=1000000007;
int t=sc.nextInt();
while(t-->0) {
BigInteger a=new BigInteger(""+sc.nextInt());
BigInteger b=new BigInteger(""+sc.nextInt());
a=(a.multiply(b.modInverse(new BigInteger(""+p)).add(new BigInteger(""+p))).mod(new BigInteger(""+p)));
System.out.println(a);
}
}
}



京公网安备 11010502036488号