超级平均数 (Supermean,UVa 10883)

 1 #include <iostream>
 2 #include <string.h>
 3 #include <string>
 4 #include <fstream>
 5 #include <algorithm>
 6 #include <stdio.h>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <cmath>
11 using namespace std;
12 const double eps = 1e-8;
13 const int INF=0x7fffffff;
14 unsigned long long uINF = ~0LL;
15 #define MAXN 10000007
16 typedef long long LL;
17 double a[MAXN];
18 int main()
19 {
20     int T,t=1,n;
21     scanf("%d",&T);
22     while(T--)
23     {
24         scanf("%d",&n);
25         for(int i=0;i<n;i++)
26         scanf("%lf",&a[i]);
27         double ans=0,temp=0;
28         for(int i=0;i<n;i++)
29         {
30         if(a[i]>0)ans+=exp(log(a[i])+temp-(n-1)*log(2));
31         if(a[i]<0)ans-=exp(log(-a[i])+temp-(n-1)*log(2));
32 
33         temp=temp+log(n-i-1)-log(i+1);
34         }
35         //cout<<ans<<endl;
36         printf("Case #%d: %.3lf\n",t++,ans);
37     }
38 
39     return 0;
40 }

数字范围 太大 化成指数运算  log c(n,k+1)-log c(n,k)=log (n-k) - log (k+1)

posted @ 2013-07-24 09:44  TO_Asia  阅读(375)  评论(0编辑  收藏  举报