UVA 10883 Supermean

题目链接:UVA 10883

题意:给定n个数,每相邻两个数取平均数,重复这个过程直到只剩一个数。求最后的数的值。

思路:可以发现答案应该是(frac{sum_{i=0}^{n-1} C_{n-1}^i * d[i]}{2^{n-1}})。难点在于n最大值为50000。

所以求解过程中要用log2计算。

代码如下:

 1 #include"cstdio"
 2 #include"iostream"
 3 #include"cstring"
 4 #include"algorithm"
 5 #include"cstdlib"
 6 #include"vector"
 7 #include"set"
 8 #include"map"
 9 #include"cmath"
10 using namespace std;
11 typedef long long LL;
12 const LL MAXN=50010;
13 const LL MOD=1000000000+7;
14 const LL INF=0x3f3f3f3f;
15 
16 // C[n][m]=C[n][m-1]*(n-m+1)/m;
17 double d[MAXN];
18 int main()
19 {
20 #ifdef LOCAL
21     freopen("in.txt","r",stdin);
22     // freopen("out.txt","w",stdout);
23 #endif
24     int t;
25     scanf("%d",&t);
26     for(int tt=1;tt<=t;tt++)
27     {
28         int n;
29         scanf("%d",&n);
30         double C=0;
31         double ans=0;
32         for(int i=0;i<n;i++)
33         {
34             scanf("%lf",&d[i]);
35             if(i) C+=log2(1.0*(n-i)/i);
36             if(d[i]) ans+=((d[i]>0)?1:-1)*pow(2, ( C + log2(fabs(d[i])) - (n-1) ));
37         }
38 
39         printf("Case #%d: %.3lf
",tt,ans);
40     }
41     return 0;
42 }
原文地址:https://www.cnblogs.com/zarth/p/6761292.html