Phoenix and Balance

Phoenix and Balance

Phoenix has nn coins with weights 21,22,…,2n21,22,…,2n. He knows that nn is even.
He wants to split the coins into two piles such that each pile has exactly n2n2 coins and the difference of weights between the two piles is minimized. Formally, let aa denote the sum of weights in the first pile, and bb denote the sum of weights in the second pile. Help Phoenix minimize |a−b||a−b|, the absolute value of a−ba−b.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases.
The first line of each test case contains an integer nn (2≤n≤302≤n≤30; nn is even) — the number of coins that Phoenix has.

Output

For each test case, output one integer — the minimum possible difference of weights between the two piles.

Example

Note

在这里插入图片描述

Note

In the first test case, Phoenix has two coins with weights 22 and 44. No matter how he divides the coins, the difference will be 4−2=24−2=2.
In the second test case, Phoenix has four coins of weight 22, 44, 88, and 1616. It is optimal for Phoenix to place coins with weights 22 and 1616 in one pile, and coins with weights 44 and 88 in another pile. The difference is (2+16)−(4+8)=6(2+16)−(4+8)=6.

题解

1. 题目大意:

有n个硬币(n一定是偶数),第i个硬币重量为2的i次方,将硬币数量平均分配为两份,并且重量相差最小,t个样例,每个样例n个硬币,输出重量相差最小的绝对值。

2. 思路:

最重的一定为第n个,并且2^1 +……2 ^n-1=(2 ^n) -1。所以只需将前n/2-1项与第n项相加即可。

代码


```cpp
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int t,n,i;
    cin>>t;
    int a[31];
    while(t--){
      long long sum1=0,sum2=0,d;        //n<=30不开long long也超不了
      cin>>n;
      for(i=1;i<=n/2-1;i++){
        a[i]=pow(2,i);                 //先赋值再加和。
        sum1+=a[i];
      }
      for(i=n/2;i<n;i++){
        a[i]=pow(2,i);
        sum2+=a[i];
      }
      a[n]=pow(2,n);
      sum1+=a[n];
      d=sum1-sum2;              //因为可以确定2^n大于其他数之和直接求差无需判断正负。
      cout<<d<<endl;            //<cmath>包含绝对值函数abs()。
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值