首页 > 代码库 > 蓝桥杯 矩阵翻硬币

蓝桥杯 矩阵翻硬币

思路:

参考了http://blog.csdn.net/snailset/article/details/26752435,思路很清晰。

用java实现了高效的牛顿迭代法。

我tm都要爱上java了。

实现:

 1 import java.math.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5 
 6     public static BigInteger sqrt(BigInteger x){
 7         if (x .equals(BigInteger.ZERO) || x.equals(BigInteger.ONE)) {
 8             return x;
 9         }
10         BigInteger two = BigInteger.valueOf(2L);
11         BigInteger y;
12         for (y = x.divide(two);
13              y.compareTo(x.divide(y)) > 0;
14              y = ((x.divide(y)).add(y)).divide(two));
15         return y;
16     }
17 
18     /**
19      * @param args
20      */
21     public static void main(String[] args) {
22         // TODO Auto-generated method stub
23         BigInteger n;
24         Scanner s = new Scanner(System.in);
25         BigInteger a = s.nextBigInteger();
26         BigInteger b = s.nextBigInteger();
27         BigInteger ia = sqrt(a);
28         BigInteger ib = sqrt(b);
29         System.out.println(ia.multiply(ib));
30     }
31 }

 

蓝桥杯 矩阵翻硬币