题目链接:The Chosen One

比赛链接:ICPC Asia Nanning 2017

题意

\(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));
        }
    }
}

相关文章:

  • 2021-11-19
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2021-12-25
相关资源
相似解决方案