codeforces 1348A - Phoenix and Balance

A. Phoenix and Balance
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Phoenix has n coins with weights 21,22,…,2n. He knows that n is even.

He wants to split the coins into two piles such that each pile has exactly n2 coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weights in the second pile. Help Phoenix minimize |a−b|, the absolute value of a−b.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains an integer n (2≤n≤30; n 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
inputCopy
2
2
4
outputCopy
2
6
Note
In the first test case, Phoenix has two coins with weights 2 and 4. No matter how he divides the coins, the difference will be 4−2=2.

In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16. It is optimal for Phoenix to place coins with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)−(4+8)=6.

链接:https://codeforces.ml/problemset/problem/1348/A
题意:21-2n分成两堆,求出两堆相减的min
思路:根据等比数列 an>sn-1(q>1)(自推,如有需要评论留言我再给推导过程) 可以知道最后一项一定比前面的所有数加起来还大 所以求两堆相减的min 直接可以前n/2-1项+最后一项-剩下的数
ac代码如下:

#include<iostream>
using namespace std;
int main()
{
	int a[31]={2,4};
	for(int i=2;i<31;i++)
	{
		a[i] = 2*a[i-1];
	}
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int sum1=0,sum2=0; 
		
		for(int i=0;i<n/2-1;i++)
		{
			sum1 += a[i];
		}
		sum1 += a[n-1];
		
		for(int i=n/2-1;i<n-1;i++)
		{
			sum2 += a[i];
		}
		cout<<sum1-sum2<<endl;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值