You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.

If you choose the ith door, it can either take you back to the same position where you begun in ximinutes, or can take you out of the maze after xi minutes. If you come back to the same position, you can remember last K doors you have chosen. And when you are about to choose a door, you never choose a door that is already visited by you. Or we can say that you never choose a door that is visited as one of the last K doors. And the probability of choosing any remaining door is equal.

Now you want to find the expected time to get out of the maze.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and two integers n K (1 ≤ n ≤ 100, 0 ≤ K ≤ n). The next line contains n space separated integers. If the ith integer (xi) is positive, you can assume that the ithdoor will take you out of maze after xi minutes. If it's negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.

Output

For each case, print the case number and the expected time to get out of the maze. If it's impossible to get out of the maze, print '-1'. Otherwise print the result. Error less than 10-6 will be ignored.

Sample Input

4

 

2 0

10 10

 

2 0

10 -10

 

3 1

10 -10 -20

 

3 2

10 -10 -20

Sample Output

Case 1: 10

Case 2: 20.000

Case 3: 30.0000000000

Case 4: 25.0000000000

 

题意:

n个门,门有值:xi为负值的门带你abs(xi)后又回到原点;xi为正值的门则带你离开迷宫。

并且你会记住你前面选择的K道门,在下次选择的时候不会选择这些门。选择每扇门的概率相等。求走出迷宫的时间期望值。

分析:

LIight oj1027 的加强版,1027那道是无记忆的。

思维类似:每经过一个门出去期望都要考虑两种情况:一是直接能出去,二是返回再走一次才出去。


有自己的解释

定义dp[k]表示记住了K道门后,显然这K道门都是为负值的门,走出迷宫的时间期望值。

1.当num2<=k,那么可以随便选一个正门出去,期望为

 

                                                                  dp[k]=sum1/(n-num2)

2.num2>k 

                                                          

LightOJ-1395 A Dangerous Maze (II) (期望dp)_#include

3.对于0 <= i < K 则有

                                                            

LightOJ-1395 A Dangerous Maze (II) (期望dp)_ios_02

公式解释:

LightOJ-1395 A Dangerous Maze (II) (期望dp)_ios_03

 有概率选择直接出去的门,

LightOJ-1395 A Dangerous Maze (II) (期望dp)_ios_04

也可能返回在走一遍,但要在未被记忆的门num2-i的当中选,其期望为dp[i+1](第一公式k不变的原因,因为k就是最大记忆量了。关键是sum3.

sum3(i)的意思是在已经走过了i道负值门之后,再选择num-i道负值门的时间的和
但是前面的选择的i道负门, 具体是怎样的我们并不知道,该怎么求呢?

再来一个问题假设有n个数,先选K个数,在从剩下的数里面选择一个数的平均值是多少呢?

答案:n个数的平均值
n个数先选K个再选,可以等价于先从n个中选n-K个,再选一个
考虑每个数的贡献,选完某个数,然后就从剩下n-1个中选n-k-1个呗
平均值就等于

LightOJ-1395 A Dangerous Maze (II) (期望dp)_#include_05

所以,式子可以整理为:

                                                   

LightOJ-1395 A Dangerous Maze (II) (期望dp)_ios_06

移项:

                                                    

LightOJ-1395 A Dangerous Maze (II) (期望dp)_ios_07

从后往前递推即可

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define nmax 510
using namespace std;
int cas=0,T,m,k,n;
double p;
int main(){

scanf("%d",&T);
while(T--)
{
int n, k;
scanf("%d%d",&n,&k);
int num2 = 0,sum1 = 0,sum2 = 0, x;
for(int i = 0;i < n;i++){
scanf("%d",&x);
if(x > 0) sum1 += x;
else sum2 += -x,num2++;
}


if(num2 == n){
printf("Case %d: ",++cas);
printf("-1\n");
continue;
}
k = min(num2,k);
double ans = 0;
if(num2== k) ans = sum1 / 1.0 / (n - num2);
else ans = sum1 / 1.0 / (n - num2) + sum2 * 1.0 * (num2 - k) / num2 / (n - num2);

for(int i = k - 1;i >= 0;i--) {
ans = sum1 * 1.0 / (n - i) + (sum2 * 1.0 / num2 + ans) * (num2 - i) / (n - i);
}
printf("Case %d: %.12lf\n",++cas,ans);


}


}