首页 > 代码库 > UVA 10883 Supermean 上下取对数防溢出

UVA 10883 Supermean 上下取对数防溢出

题目链接:点击打开链接

溢出了半天,觉累不爱


#include<math.h>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll int

int main(){
	ll Cas=  1, T; cin>>T;
	while(T--){
		ll n;
		cin>>n;
		double sum = 0, a;
		double hehe = 0;
		for(ll i = 0; i < n;i++){
			cin>>a;
			if(a<0) sum-=exp(hehe+log(-a)-(n-1)*log(2));
			else sum += exp(hehe+log(a)-(n-1)*log(2));
			hehe+=log(n-1-i)-log(i+1);
		}
		printf("Case #%d: %.3lf\n",Cas++,sum);
	}
	return 0;
}