hdu6433——Problem H. Pow

There are n numbers 3^0, 3^1, . . . , 3^n-1. Each time you can choose a subset of them (may be empty), and then add them up.
Count how many numbers can be represented in this way.
Input
The first line of the input contains an integer T , denoting the number of test cases. In each test case, there is a single integers n in one line, as described above.
1 ≤ T ≤ 20, 0 ≤ n ≤ 1000
Output
For each test case, output one line contains a single integer, denoting the answer.
Sample Input
4
9
7
8
233
Sample Output
512
128
256
13803492693581127574869511724554050904902217944340773110325048447598592

这个一开始觉得很难,仔细一想,这不就是一个数按带权的每一位的拆分吗,比如19=1*10^1 +9*10^0 这样n位有2^n种选择
然后其实我也不知道为什么答案就是2^n 反正其实直接看样例,然后java大数写个快速幂

代码:

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

public class Main {
   

    public static BigInteger pow(BigInteger n){
   
        BigInteger ans=BigInteger.ONE;
        BigInteger base=BigInteger.valueOf(2);
        while(n.compareTo(BigInteger.ZERO)>0){
   
            if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0){
   
                ans=ans.multiply(base);
            }
            base=base.multiply(base);
            n=n.divide(BigInteger.valueOf(2));
            //System.out.println(ans+" "+n);
        }
        return ans;
    }
    public static void main(String[] args) {
   
        Scanner scanner=new Scanner(System.in);
        int t=scanner.nextInt();
        while(t-->0){
   
            BigInteger n=scanner.nextBigInteger();
            System.out.println(pow(n));
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
04-30 18:05
空屿编号:你把墨镜摘下来是不是这样😭
点赞 评论 收藏
分享
04-01 11:08
中原工学院 Java
老六f:感觉这种培训期过了就找理由给你开了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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