Java大数——a^b + b^a

a^b-b^a

Description

计算a^b-b^a的值

Input

第一行一个整数T,表示接下来有T行

每行两个整数a和b,(0<a,b<1000)

Output

输出值

Sample Input

3 1 1 2 1 10 9

Sample Output

0 1 -2486784401
 
解题思路:由于数值太大,用c++的pow(x,y)完全不能解决,所有用Java大数的方法。
代码如下:
import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger a,b;
        int t,c,d;
        t = cin.nextInt();
        for(int i = 0;i < t;i++) {
            c = cin.nextInt();
            d = cin.nextInt();
            a = BigInteger.valueOf(1);
            b = BigInteger.valueOf(1);
            for(int j = 1;j <= d; j++) {
                a = a.multiply(BigInteger.valueOf(c));
            }
            for(int j = 1;j <= c; j++) {
                b = b.multiply(BigInteger.valueOf(d));
            }
            a = a.subtract(b);
            System.out.println(a);
        }    
    }
}
posted @ 2018-07-20 10:15  半忧夏  阅读(244)  评论(0编辑  收藏  举报