PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000091. Vé xe có giá ngẫu nhiên (Randomly-priced Tickets)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Vé xe có giá ngẫu nhiên (Randomly-priced Tickets)

Randomly-priced Tickets

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn towns with directed coach connections. The fare for each ride is independently uniform over integers 1,…,R1,\dots,R. For each source, destination, and budget BB, calculate the probability that the sum of fares of a route with the fewest rides does not exceed BB. An unreachable destination has probability zero; travelling from a town to itself needs no ticket.

Input

First n,Rn,R, then nn directed-adjacency Y/N strings of length nn, then qq queries s t B with 1-based towns and a nonnegative budget.

Output

Print one probability between 0 and 1 with exactly six digits after the decimal point per query.

Subtasks

  • Subtask 1 (40 points): $2\le n\le7,\ 1\le R\le8,\ 1\le q\le10,\ 0\le B\le250$. 0≤B≤2500\le B\le250; nn chuỗi dài nn chỉ có Y/N; các lượt xe có giá độc lập, đều trong {1,…,R}\{1,\ldots,R\}.

  • Subtask 2 (60 points): $2\le n\le26,\ 1\le R\le25,\ 1\le q\le10,\ 0\le B\le250$. 0≤B≤2500\le B\le250; nn chuỗi dài nn chỉ có Y/N; các lượt xe có giá độc lập, đều trong {1,…,R}\{1,\ldots,R\}.

Examples

Input

3 4
NYY
YNY
YYN
2
1 3 1
1 3 4

Output

0.250000
1.000000

Explanation

Town 1 directly reaches town 3 in one ride. The ticket price is uniform over 1,2,3,4, so budget 1 succeeds with probability 0.25 and budget 4 with probability 1.