各位有什么好的思路吗?逆向思维好考虑

逆向思维好考虑,就是用总的情况数,减去不可能出现坏账的情况。但是用的BigInteger超时了

import java.math.BigInteger;
import java.util.Scanner;

public class laoxu {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        BigInteger w = in.nextBigInteger();
        BigInteger temp = pow2(w,n);
        BigInteger x = BigInteger.valueOf(1);
        BigInteger n_temp = BigInteger.valueOf(n);
        BigInteger w_cha = w.subtract(x);
        BigInteger y = pow2(w_cha,n-1).multiply(n_temp);
        BigInteger res = temp.subtract(y);
        BigInteger tt = BigInteger.valueOf(100003);
        System.out.println(res.remainder(tt));
    }

    public static BigInteger pow2(BigInteger x, int n) {
        if (n == 0)
            return BigInteger.valueOf(1);
        else {
            if (n % 2 == 0)
                return pow2(x.multiply(x), n / 2);
            else
                return pow2(x.multiply(x), (n - 1) / 2).multiply(x);
        }
    }
}
全部评论
import java.util.*; public class Main{     public static void main(String[] args){         Scanner in = new Scanner(System.in);         long n = in.nextLong();         long w = in.nextLong();         System.out.println(power(n,w) - (n * power(n-1, w-1)) % 100003l);     }          public static long power(long a, long b){    //快速幂         long tmp = 1;         while (b > 0){             if(b % 2 == 1){                 tmp = tmp % 100003l;                 a = a % 100003l;                 tmp = tmp * a;             }             a = a % 100003l;             a = a * a;             b = b >> 1;         }         return tmp % 100003l;     } } 过了80%,不知道为什么
点赞 回复 分享
发布于 2017-09-28 21:37
为什么不在pow2函数里每次乘法运算都取模呢?返回值也取模   不然你最后的结果超级超级大  运算复杂度当然高。。。
点赞 回复 分享
发布于 2017-09-28 21:37

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务