Tojo hates probabilities
All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian as well.
As a holiday gift, Tojo received a probability problem. The problem read as follows
Consider an N by M grid. Rows are numbered 1 to N, from top to bottom. Columns are numbered 1 to M, from left to right. You are initially at cell (1, 1) and want to go to cell (N, M). From any cell you can move to the cell below it or to the cell right to it. You should never go out of the grid. At any point you should consider all the possibilities of movement with equal probability
Let P[i][j] be the probability of visiting cell (i, j). You need to calculate the sum of P[i][j] for 1 ≤ i ≤ N, 1 ≤ i ≤ M.
As we all know, Tojo really hates probability related problems. He wants you to solve this task
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.Only line of each test case has two integer N and M.
For each test case, output a single line containing the required answer. Answers within an absolute or relative error of 10-6 will be accepted.
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 1000
- 1 ≤ M ≤ 1000
Input: 2 2 2 1 6 Output: 3.000000 6.000000
Example case 1
Probability matrix P for N=2, M=2 is
You are at (1, 1) initially. So the probablity of visiting (1, 1) is 1. At (1, 1) you have 2 options, move below to (2, 1) or to right cell (1, 2). Probablity of going to (1, 2) is 0.5. Probability of going to (2, 1) is 0.5. You always end up at (2, 2), so P is 1. Required sum = 1.0 + 0.5 + 0.5 + 1.0 = 3.0
Example case 2
Probability matrix P for N=1, M=6 is
1.0 1.0 1.0 1.0 1.0 1.0
Because at any position there is only one possible next position.
|Tags||anudeep2011, cook54, easy, maths|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, PYP3, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.