参考
http://www.cnblogs.com/staginner/archive/2011/12/13/2286691.html
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define PI 3.1415926535897932626 using namespace std; int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);} #define MAXN 35 double v[MAXN],ans,T; int N; int main() { v[0]=1; for (int i=1;i<=30;i++) v[i]=v[i-1]*2; while (scanf("%d%lf",&N,&T)!=EOF) { if (N==0 && T==0) break; if (fabs(1-T)<1e-9) {printf("%.3lf ",v[N]);continue;} double ans=v[N]; for (int i=N-1;i>=0;i--) { double f=v[i]/ans; if (f<=T) ans=(1+T)/2*ans; else ans=(f-T)/(1-T)*v[i]+(1-f)/(1-T)*(1+f)/2*ans; } printf("%.3lf ",ans); } return 0; }