UVA - 10883 Supermean

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   strong   for   

Description

Problem F
Supermean
Time Limit: 2 second
"I have not failed. I‘ve just found 10,000 ways that won‘t work."
Thomas Edison

Do you know how to compute the mean (or average) of n numbers? Well, that‘s not good enough for me. I want the supermean! "What‘s a supermean," you ask? I‘ll tell you. List then given numbers in non-decreasing order. Now compute the average of each pair of adjacent numbers. This will give youn - 1 numbers listed in non-decreasing order. Repeat this process on the new list of numbers until you are left with just one number - the supermean. I tried writing a program to do this, but it‘s too slow. :-( Can you help me?

Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containingn (0<n<=50000). The next line will contain the n input numbers, each one between -1000 and 1000, in non-decreasing order.

Output
For each test case, output one line containing "Case #x:" followed by the supermean, rounded to 3 fractional digits.

Sample Input Sample Output
4110.421.0 2.231 2 351 2 3 4 5
Case #1: 10.400Case #2: 1.600Case #3: 2.000Case #4: 3.000

Problemsetter: Igor Naverniouk

題意:給出n個數,每相鄰的兩個數求平均數,將得到n-1個,然後再兩兩求平均數,依次類推直到最後一個,求這個數是多少

思路:係數的話很容易想到是楊輝三角的係數,但是因為n大太,所以為了防止溢出我們用log來儲存,每一項的通式是:∑i=0n?1C[n?1][i]?num[i]2n?1

然後就是在推組合數的同時對數處理

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 50005;double C[maxn], num[maxn];int main() {int t, n, cas = 1;scanf("%d", &t);while (t--) {scanf("%d", &n);for (int i = 0; i < n; i++)scanf("%lf", &num[i]);double ans = 0.0, tmp = log10(1);for (int i = 0; i < n; i++) { if (i) tmp = tmp + log10(n-i) - log10(i);if (num[i] < 0)ans -= pow(10, tmp + log10(-num[i]) - (n-1)*log10(2));else ans += pow(10, tmp + log10(num[i]) - (n-1)*log10(2));}printf("Case #%d: %.3lf\n", cas++, ans);}return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.