Poj-crazy Tea Party, a very good math problem ~ ~ ~

Source: Internet
Author: User

Crazy Tea Party
Time Limit: 1000MS Memory Limit: 10000K

Description

N participants of << crazy Tea Party >> sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required-participants to sit-reverse order (so, left neighbors would bec ome right, and Right-left).

Input

The first line is the amount of tests. Each next line contains one integer n (1 <= n <= 32767)-The amount of crazy tea participants.

Output

For each number n of participants to crazy Tea Party print on the standard output, on a separate line, the minimum time re Quired for all participants to sit in reverse order.

Sample Input

3456

Sample Output

246

Source

Southeastern Europe 2003

test instructions is simple, n individuals (1~n) sit on a table, each minute adjacent two people can exchange positions, for a minimum of a few minutes to make the order into reverse;

Seemingly ordinary ideas no breakthrough, simple to find the law can not find, but we may find that the title only requires the final order for reverse, but not the position can not be changed; if there are 5 people: 1 2 3 4 5; After the transformation is 3 2 1 5 4; You see, the result is in reverse order. The position of the person starting with the number 1 can be arbitrary, but once the position on both sides of the 1 position is determined, if the last 1 is in the position numbered K, then the last K is at the beginning of the position numbered 1, the order becomes: K~1, n~k+1;

We know a bubble sort needs to change N (n-1)/2; Here it can be divided into two parts, 1~k transformation, need to transform K* (k-1)/2, the remaining number of n-k need to transform (n-k) * (n-k-1)/2; Total k* (k-1 )/2+(n-k) * (n-k-1)/2, decomposition into: k^2-nk+ (n*n-n)/2;n for the number of known, the minimum value of the two-tuple equation (4ac-b^2)/4a; The answer is n/2* (n/2-1); But for the odd and even unknown of N, The idea has been provided to this, the odd and even problem should not be difficult to solve; look at the code:

#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include < Cstring>using namespace Std;int Main () {    int t,n;    scanf ("%d", &t);     while (t--)     {         scanf ("%d", &n);         if (n%2==0)         printf ("%d\n", n/2* (n/2-1));         else            printf ("%d\n", n/2* ((n+1)/2-1));     }            return 0;}


Poj-crazy Tea Party, a very good math problem ~ ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.