The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occurs during the monsoon, when it doubles in height. The second growth cycle of the tree occurs during the summer, when its height increases by 1 meter.
Now, a new Utopian tree sapling is planted at the onset of the monsoon.
Its height is 1 meter. Can you find the height of the tree after N growth cycles?

Input Format
The first line contains an integer, T, the number of test cases.
T lines follow. Each line contains an integer, N, that denotes the number of cycles for that test case.

Constraints
1 <= T <= 10
0 <= N <= 60

Output Format
For each test case, print the height of the Utopian tree after N cycles.


题解:

 import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*; public class Solution { static int Utopian_Tree(int N) {
if(N == 0)
return 1;
if(N == 1)
return 2;
if(N%2 == 0)
return (int)Math.pow(2, N/2+1)-1;
return (int)Math.pow(2, N/2+2)-2;
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t;
t = in.nextInt();
for(int i = 0;i < t;i++){
int n;
n = in.nextInt();
System.out.println(Utopian_Tree(n));
}
}
}

【HackerRank】Utopian tree的更多相关文章

  1. 【BZOJ】2631: tree LCT

    [题意]给定n个点的树,每个点初始权值为1,m次操作:1.x到y的点加值,2.断一条边并连一条边,保证仍是树,3.x到y的点乘值,4.x到y的点权值和取模.n,m<=10^5. [算法]Link ...

  2. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  3. 【题解】Digit Tree

    [题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. ...

  4. 【题解】[P4178 Tree]

    [题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...

  5. 【总结】Link-Cut Tree

    这是一篇关于LCT的总结 加删边的好朋友--Link Cut Tree Link-Cut Tree,LCT的全称 可以说是从树剖引出的问题 树剖可以解决静态的修改或查询树的链上信息:那如果图会不断改变 ...

  6. 【BZOJ】1468: Tree(POJ1741) 点分治

    [题意]给定带边权树,求两点距离<=k的点对数.n<=40000. [算法]点分治 [题解]对于一个区域,选择其重心x作为根,则划分出来的每棵子树都是子区域,可以证明至多划分log n次( ...

  7. 【HackerRank】Cut the tree

    题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...

  8. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  9. 【Leetcode】【Easy】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

随机推荐

  1. Unity3D 移动开发代码优化

    1. 尽量避免每帧处理 比方: function Update() { DoSomeThing(); } 可改为每5帧处理一次: function Update() { if(Time.frameCo ...

  2. (转载)Javascript removeChild()不能删除全部子节点的解决办法

    在Javascript中,只提供了一种删除节点的方法:removeChild().removeChild() 方法用来删除父节点的一个子节点. 语法: parent.removeChild(thisN ...

  3. ExtJs学习-搭建开发环境

    Extjs是一个非常棒的ajax框架,可以用来开发十分绚丽外观的客户端框架,能使B/S框架更加具有活力.它是一个用javascript编写的框架,与后台技术无关的ajax框架.因此,可以把ExtJs使 ...

  4. ios . -- UICollectionView --cell 自适应

    #pragma mark — 视图控制器中使用:(关键) layout.estimatedItemSize = CGSizeMake(WIDTH, ); // layout约束这边必须要用estima ...

  5. MySQL文件和Innodb引擎文件

    构成MySQL数据库和InnoDB存储引擎表的各种类型文件,组要有以下文件 参数文件:告诉MySQL实例启动时在哪里可以找到,数据库文件,并且指定某些初始化参数,这些参数定义了某种内存结构的大小等设置 ...

  6. Android无线测试之—UiAutomator UiDevice API介绍三

    获取坐标与坐标点击 一.坐标相关的知识: 1)手机屏幕坐标:左上角开始到右下角结束 2)DP:设备独立像素,例如320像素显示到640像素上要拉伸一倍 3)Point:代表一个点(x,y),左上角的坐 ...

  7. iOS-获取当前网页的 url 和 title 和 html

    本文转载至 http://www.th7.cn/Program/IOS/201310/156916.shtml   @property (strong,nonatomic)UIWebView *web ...

  8. 关于错误处理程序中【return】的用法

    先让俺这位新人帮各位有幸游览到我博客文章的叔叔阿姨哥哥姐姐们解释一下什么是错误处理?即:当程序发生错误时,保证程序不会异常中断的机制. 那么为什么程序中会有错误处理呢?像我们通常无论是玩手机或者玩游戏 ...

  9. 《从零开始学Swift》学习笔记(Day 18)——有几个分支语句?

    原创文章,欢迎转载.转载请注明:关东升的博客       分支语句又称条件语句,Swift编程语言提供了if.switch和guard三种分支语句. if语句 由if语句引导的选择结构有if结构.if ...

  10. 【RSS】我的RSS使用介绍

    早就想写一个有关RSS的文章,一直没时间,今天刚好被现DL说了一波,那就先整理出一篇教程吧.后续说不定还有分享: 分享相关PPT: 一.我使用的服务: Feedly:https://feedly.co ...