UVa First Bangladeshi Contest of 2012-2013 Season (E) Elliptic Athletics Track

問題

x^2 / a^2 + y^2 / b^2 = 1で表される楕円の周長を求めよ。
出力に10^-5を超える誤差があってはならない。

制約条件

テストケースは50個以下
a, bは1以上20以下の整数

方針

wikipediaの楕円のページに、楕円の周長を算出する式が載っていたので、
それで計算した。


近似式のほうを使ったらWAで、
無限級数を適当な項まで採用して足したらACだった。

ソースコード

typedef long double ld;
double calc(int a, int b){
	ld e2 = ld(a * a - b * b) / (a * a);

	ld ans = 1, f = 1, t = 1;
	rep(i, 10000){
		f *= ld(2 * i + 1) * (2 * i + 1) / (2 * i + 2) / (2 * i + 2);
		t *= e2;
		ans -= f * t / (2 * i + 1);
	}
	ans *= 2 * acos(-1.0l) * a;
	return (double)ans;
}
int main()
{
	int CS;
	cin >> CS;
	rep(cs, CS){
		int a, b;
		cin >> b >> a;
		
		printf("Case %d: %.9f\n", cs + 1, calc(a, b));
	}
	
	return 0;
}