uva - 10833 Supermean(二项式系数,对指数)

时间:2022-05-10 15:37:52

模拟发现,每个元素求和时,元素的系数是二项式系数,于是ans=sum(C(n-1,i)*a[i]/2^(n-1)),但是n太大,直接求会溢出,其实double的范围还是挺大的,所以可以将组合数转化成对数:

e^(lnC(n-1, k)*A[k]/(2^n-1) )  ==>  e^( ln C(n-1,k) + ln A[k] - (n-1)*ln2 );

又直接利用公式求二项式系数:C(n, k+1)/C(n, k) = (n-k)/(k+1);

而且对数还有递推求法:

logC(n,k+1)=logC(n,k)+log(n-k)-log(k+1)

代码:

 #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip> using namespace std; #define INF 0xffffff7
#define maxn 50010
const double tmp = log(2.0);
double data[maxn];
int main()
{
int T;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
int n;
scanf("%d", &n);
double ans = 0.0, c = 0.0;
for(int i = ; i < n; i++)
{
scanf("%lf", &data[i]);
if(data[i] > ) ans += exp(log(data[i]) - (n-)*log(2.0) + c);
else if(data[i] < ) ans -= exp(log(-data[i]) - (n-)*log(2.0) + c);
//cout << ans << endl;
c += log((double)n-i-)-log((double)i+);
}
printf("Case #%d: %.3lf\n", kase, ans);
}
return ;
}