Problem: Example

Input

1 10 2
1 1 1


Output

2.0

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

int N,K;
double T[33];
double dp[1011][33];
double minc[1011];

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  {
    int r1,r2;
    cin>>r1>>r2;
    N=r2-r1;
  }
  cin>>K;
  rep(i,K+1)cin>>T[i];

  rep(i,N+1)repl(j,1,K+1)dp[i][j]=INF;
  rep(i,N+1)minc[i]=INF;
  minc[0]=0; minc[1]=0;
  repl(i,2,N+1){
    repl(j,1,K+1){
      repl(k,1,i){
        int l=i-k;
        double p1=(double)l/(double)i;
        double p2=(double)(i-l)/(double)i;
        if(j>1){
          minch(dp[i][j],p2*dp[k][j-1]+p1*(T[j]+minc[l]));
        }else{
          minch(dp[i][j],p2*(minc[i-l]+T[0])+p1*(T[j]+minc[l]));
        }
      }
      minch(minc[i],dp[i][j]);
    }
  }
  printf("%.10f\n", minc[N]);
  return 0;
}