ICPC Asia Nanning 2017 F. The Chosen One (高精度运算)

题目链接:The Chosen One

题意

\(t\) 组样例,每组给出一个整数 \(n(2\le n\le 10^{50})\),求不大于 \(n\) 的最大的 \(2\) 的整数次幂。

题解

高精度运算

Java BigInteger 中的 bitLength() 方法可以直接计算某个大数二进制表示下的位数。

更多关于 Java BigInteger 的操作参见我的另一篇文章 大数运算之 Java BigInteger 的基本用法

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

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while (t-->0){
            BigInteger n = in.nextBigInteger();
            BigInteger ans = new BigInteger("2");
            System.out.println(ans = ans.pow(n.bitLength() - 1));
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/wulitaotao/p/11360526.html