Problem:
Problem D: Deadly Dice Game

T.I. Financial Group, a world-famous group of finance companies, has decided to hold an evil gambling
game in which insolvent debtors compete for special treatment of exemption from their debts.

In this game, each debtor starts from one cell on the stage called the Deadly Ring. The Deadly Ring
consists of N cells and each cell is colored black or red. Each cell is connected to exactly two other
adjacent cells and all the cells form a ring. At the start of the game, each debtor chooses which cell to
start. Then he rolls a die and moves on the ring in clockwise order by cells as many as the number of
spots shown on the upside of the die. This step is called a round, and each debtor repeats a round T times.
A debtor will be exempted from his debt if he is standing on a red cell after he finishes all the rounds.
On the other hand, if he finishes the game at a black cell, he will be sent somewhere else and forced to
devote his entire life to hard labor.

You have happened to take part in this game. As you are allowed to choose the starting cell, you want to
start from a cell that maximizes the probability of finishing the game at a red cell. Fortunately you can
bring a laptop PC to the game stage, so you have decided to write a program that calculates the maximum
winning probability.

Input

The input consists of multiple datasets.

Each dataset consists of two lines. The first line contains two integers N (1 ≤ N ≤ 2000) and T (1 ≤ T ≤ 2000) in this order, delimited with a single space. The second line contains a string of N characters that
consists of characters ‘R’ and ‘B’, which indicate red and black respectively. This string represents the
colors of the cells in clockwise order.

The input is terminated by a line with two zeros. This is not part of any datasets and should not be
processed.

Output

For each dataset, print the maximum probability of finishing the game at a red cell in one line. Your
program may output an arbitrary number of digits after the decimal point, provided that the absolute
error does not exceed 10-8.

Sample Input

6 1
RBRBRB
10 1
RBBBRBBBBB
10 2
RBBBRBBBBB
10 10
RBBBBBBBBB
0 0

Output for the Sample Input

0.50000000
0.33333333
0.22222222
0.10025221

Solution in C (judge verdict: Accepted)

/*
  AOJ 2134
  Title:
  @kankichi573
*/
#include <stdio.h>
#include <string.h>
#include <float.h>
#define max(x,y) (((x)>(y))?(x):(y))
#define ONE_SIXTH (1.0/6.0)

double prob_table[21][12001];

void dice1(double p_after[],double p_before[],int turn)
{
  int i,j;
  for(i=0;i<=12000;i++)
    p_after[i]=0.0;
  for(i=turn;i<=6*turn;i++)
    for(j=1;j<=6;j++)
      p_after[i+j] += ONE_SIXTH * p_before[i];
}



void init()
{
  int i,j,k;
  double prob1[12001];
  double prob2[12001];

  prob1[0]=1.0;
  for(i=1;i<=12000;i++)
    prob1[i]=0.0;
  memcpy(&prob_table[0][0],prob1,sizeof(double)*12001);

  for(i=0;i<20;i++)
    {
      //printf("i=%d\n",i);
      for(j=0;j<100;j++)
	{
	  dice1(prob2,prob1,i*100+j);
	  memcpy(prob1,prob2,sizeof(double)*12001);
	}
	
      memcpy(&prob_table[i+1][0],prob1,sizeof(double)*12001);

      //for(k=0;k<=12000;k++)
      //if(prob_table[i+1][k]!=0.0)
      //printf("%d:%lg\n",k,prob_table[i+1][k]);
    }
}

void prepare(double prob[],int n)
{
  int i;
  double probw1[12001];
  memcpy(probw1,&prob_table[n/100][0],sizeof(double)*12001);
  memcpy(prob  ,&prob_table[n/100][0],sizeof(double)*12001);
  for(i=0;i<(n%100);i++)
    {
      dice1(prob,probw1,(n/100)*100+i);
      memcpy(probw1,prob,sizeof(double)*12001);
    }
}
main()
{
  int n,t,i,j;
  char color[2001];
  double prob[12001];
  double max_,p;

  init();
  
  while(scanf("%d %d",&n,&t) && (n||t))
    {
      scanf("%s",color);
      prepare(prob,t);
      /*
      for(j=10;j<20;j++)
	printf("[%d] %g\n",j,prob[j]);
      */
      max_=0.0;
      for(i=0;i<n;i++)
	{
	  for(p=0.0,j=t;j<=t*6;j++)
	    if(color[(i+j)%n]=='R')
	      {
		//printf("%d %lf[%d]\n",j,prob[(i+j)%n],(i+j)%n);
		p += prob[j];
	      }
	  max_=max(max_,p);
	}
      printf("%.9lf\n",max_);
    }
  return(0);
}