小牛获胜一次的概率为,那么小牛此次游戏获胜的概率就为,所以小牛输的概率就为1-。(千万看准 这里求的是输的概率 不是赢得概率emmmm~~~)
通过费马小定理求逆元即可喽
java选手注意 用Scanner是过不了的(反正我wa了很多次) 要用StreamTokenizer才可以
import java.math.*; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; public class Main { public static long mod = 1000000007; public static void main(String args[]) throws IOException { StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out =new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken(); int t = (int)in.nval; while(t-->0) { in.nextToken(); long n = (long)in.nval; in.nextToken(); long m = (long)in.nval; long ans = pow(n,m); out.println((ans-1)*pow(ans,mod-2)%mod); out.flush(); } } public static long pow(long n,long m) { long ans=1,sum=n; while(m>0) { if((m&1)==1) { ans=ans*sum%mod; } sum=sum*sum%mod; m>>=1; } return ans%mod; } }