zoukankan      html  css  js  c++  java
  • CF A. Phoenix and Balance Codeforces Round #638 (Div. 2) 5月1号

    A. Phoenix and Balance
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 |ab||a−b|, the absolute value of aba−b.

    Input

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

    The first line of each test case contains an integer nn (2n302≤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
    input
    Copy
    2
    2
    4
    
    output
    Copy
    2
    6
    
    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 42=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.

    这题的意思就是,他有n个硬币(n是偶数)然后放在两个盘子里然后求相减最小;所以很自然的我们可以想到先放前n/2-1个在一个盘子再把最后一个放进去,因为成指数增加,所以这个盘子肯定重;

     1 #include <bits/stdc++.h>
     2 const int maxn=1e5+50;
     3 const int INF=0x3f3f3f3f;
     4 using namespace std;
     5 int main(){
     6     int t;
     7     cin>>t;
     8     while(t--){
     9         int n;
    10         cin>>n;
    11        double ans=pow(2.0, n*1.0/2);//求前n项和化简得到的公式为ans=2*(2^(n/2)-1)
    12        ans=(ans-1)*2;
    13        cout<<ans<<'
    ';    
    14     } 
    15 
    16 
    17 return 0;
    18 }

    May I love u;

    五月快乐

  • 相关阅读:
    mysql授权grant
    实际用到的linux小方法
    rpm包与 yum 安装与卸载
    7个优秀的国内外移动端web框架(转)
    (转) 关于成为linux运维工程师需要掌握的技能
    Content-Language:en-US
    hibernate的formula如何使用
    No Hibernate Session bound to thread, and configuration does not allow creat
    spring mvc 配置
    十六进制字符串转化为byte数组
  • 原文地址:https://www.cnblogs.com/ahijing/p/12816793.html
Copyright © 2011-2022 走看看