bnuoj-53073 Adorable height difference [Mathematics] [Non-original]

"There is a lot of rain during the Qingming Festival, and pedestrians on the road want to break their souls."

However, wfy is in a happy mood, because the BNU ACM team is out for a spring outing! And, um. . .

The following is the diary of wfy classmates:

 

Yesterday, Mr. He told us: Tomorrow we will go on a spring outing, everyone is ready to drink and eat!

Everyone was excited when they heard it, some cheered, some applauded, and they were extremely happy. The next day, we arrived at the school early, couldn't wait to get in the car, and came to the park. As soon as you walk in, it's so beautiful! There are so many trees in the park, tall and short, thick and thin, and dense, swaying gently in the spring breeze, as if welcoming us. There are so many flowers in the park, red and yellow, purple and white, with a faint fragrance that makes us all drunk. There is a clear river at the corner of the park. The river flows slowly. You can see the fish in the water swimming happily. How comfortable! The water plants are green and green, how fresh! Next to the river is a small forest, looking at a bright green from a distance. We ate, played games, played hide-and-seek, and went crazy in there. Behind the woods is a rolling hillside, winding like a swimming snake. Of course, I think the sky in the park is beautiful too. It is cloudless, clear and clear. The birds are spreading their wings and flying. They have different shapes. They rise for a while, slide down for a while, eat insects for a while, and rest in the woods for a while, very leisurely. Happy times are always so short, and soon, it gets dark. We reluctantly got in the car and went back to school, and I really hope to see this beautiful park again next spring.
After returning to school, Mr. He said: Please form a line, let's take pictures!

Mr. He especially likes cute things, such as **, such as ****, and so on.

Teacher He believes that when students stand in a row, the more the height difference between two adjacent students, the cuter the two students stand together.

Then the greater the height difference between all two adjacent classmates added up, the more adorable the photo taken, which is the cuteness index of this photo.

Mr. He hopes that the cuteness index of the photos taken is as large as possible.

However, Mr. He is not a math teacher, but a Chinese teacher. Teacher He feels very GG.

Teacher He just wanted to know what the mathematical expectation of the Mengli Index would be if the students were allowed to stand in a random line (all lines are equally likely to stand).

Smart I figured out the answer at once, and then Teacher He rewarded me with a very cute gift.

Really happy today.

 

The BNU ACM team has nfamous classmates with different heights 1, 2, \dots , n. Can you calculate the math expectations that Teacher He wants?

 

Input

The first is a positive integer T(T \leq 20)representing the number of groups of test data,

Each set of test data has only one row and contains an integer n (2 \ leq n \ leq 100).

 

Output

For each set of test data, output a line, including a real number, which represents the mathematical expectation of the Momentum Index, and the relative error is required not to exceed 10^{-9},

That is to say, let the output result be a, the standard answer is b, if it is satisfied \frac{ \left | a-b \right | }{max(1,b)} \leq 10^{-9}, the output result will be considered as the correct answer.

 

Sample Input

2
2
3

Sample Output

1.000000000000
2.666666666667

Hint

For the second set of examples, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2 ],[3,2,1], so the answer is\frac{2+3+3+3+3+2}{6}=\frac{8}{3}

 

ideas;

(1) Write a few more examples and find rules. When 2 is 3/3; when 3 is 8/3; when 4 is 15/3; when 5 is 24/3; when 6 is 35/3, it can be found that when n is ((n*n)-1) /3 is n square minus one

 1 #include <iostream>
 2 #include <iomanip>2
 3 using namespace std;
 4 int main()
 5 {
 6     int t;
 7     cin>>t;
 8     while(t--)
 9     {
10         int n;
11         cin>>n;
12         cout<<fixed<<setprecision(12)<<((n*n)-1.0)/3.0<<endl;
13     }
14     return 0;
15 }
View Code

(2) Another way of thinking, because it is a full arrangement, for n numbers, there are a total of n*(n-1)/2 forms of difference, and each difference has the same number of occurrences, and two for loops can be obtained. Find the sum of all forms of difference appearing once, then find the number of occurrences of each difference to find the sum of the differences. Since there are n-1 differences in each order, there are n! kind of sequence, so the number of times of each occurrence is: n!*(n-1)/(n*(n-1)/2)=2*(n-1)! So the sum is: (sum*2* (n-1)!)/n! = sum*2/n is the answer.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long ll;
 5 
 6 const int N=1000010;
 7 const int mod=1e9+7;
 8 const int Max=0x3f3f3f3f;
 9 
10 ll ans[N];
11 char ch[N];
12 
13 int main()
14 {
15     int T;
16     cin>>T;
17     while(T--)
18     {
19         int i,j;
20         ll sum=0;
21         int n;
22         cin>>n;
23         for(i=n;i>=1;i--)
24         {
25             for(j=1;j<=n;j++)
26             {
27                 if(i>j)
28                 sum+=(i-j);
29             }
30         }
31         double ans=(2.0*sum)/n;
32         cout<<fixed<<setprecision(12)<<ans<<endl;
33     }
34     return 0;
35 }
View Code

 

Reference blog: click here

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325211154&siteId=291194637