zoukankan      html  css  js  c++  java
  • Big Exponential Addition

    Big Exponential Addition
    给定一非负整数n计算2^n的值,一般而言把 2 乘上 n 次,就能得到答案。然而,当n特别大时,2^n要一次次地乘2可能稍嫌太慢,面对此一巨大问题利用分治(divide-and-conquer)演算法适当地拆解2 ^ n是个不错的策略,特别是在进行2^m + 2^n这类运算时,其效果更为明显。

    INPUT
    每一行有两非负整数,m与n 之间相隔一空白键。

    OUTPUT
    2^m + 2^n的精确值(每一笔输出在十进制2,000位以内),每个caes输出完毕後请输出一个换行字元做为区隔。

    SAMPLE INPUT
    3
    12 13
    20 14
    140 115


    SAMPLE OUTPUT
    12288
    1064960
    1393796616446538814624603420284493227884544


    答案

    public class BigExponentialAddition {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int num = scan.nextInt();
            while(num>0){
                int i = scan.nextInt();
              int j = scan.nextInt();
              System.out.println(bigExpoonential(i,j));
              num--;
            }
            
            
        }
        public static BigInteger bigExpoonential(int i, int j) {
    
            int poor = Math.abs(i - j);
            int minNum = 0;
            BigInteger num_tow = BigInteger.valueOf(2);
            minNum = i>j ? j : i;
            return num_tow.pow(minNum).multiply(num_tow.pow(poor).add(BigInteger.valueOf(1)));
        }
    }
  • 相关阅读:
    27、springboot整合RabbitMQ(1)
    26、springboot与消息
    25、springboot与缓存整合Redis
    24、springboot与缓存(2)
    linux之参数实用讲解
    linux之创建临时文件的方法
    【转】linux之shfit
    linux之stat
    Shell 环境中的输入输出重定向
    Linux下samba的安装与配置
  • 原文地址:https://www.cnblogs.com/bmbi/p/5281534.html
Copyright © 2011-2022 走看看