count number of valid, unique sequences for moving on a 1xn board

2

1

There's a board with n squares in a horizontal row.

You start in the leftmost square, and roll a 3-faced dice.

3 possible outcomes for a single roll of the die:

  1. Left: you move 1 step to the left, if possible.
  2. None: you stay where you are
  3. Right: you move 1 step to the right, if possible.

You want to roll the dice exactly t times, and end up at the rightmost square on the last roll.

edit: If you arrive at the rightmost square before t rolls, your only valid move is None.

You need to determine the number of valid (meaning, for instance, that you don't count sequences that try to move left or right from the rightmost square), unique sequences of dice rolls, for given (t,n) pairs. As the answer may be large, give it modulo 1000000007 (ie. 10^9+7).

  • t is a positive integer s.t. t <= 1000
  • n is a positive integer s.t. 2 <= n <= 1000

Sample testcases:

  • t=1, n=2, answer=1
  • t=3, n=2, answer=3
  • t=4, n=3, answer=7

The Shortest code in Bytes wins

StChong8

Posted 2015-02-23T03:22:14.713

Reputation: 23

Question was closed 2015-02-23T12:49:42.233

2

Seems like you were redirected here from Stack Overflow. It's a good question, but there's two things I'd like to ask: 1) PPCG questions need an objective winning criterion - how are you going to decide the winner here? (My suggestion would be code-golf), and 2) Is this a question from an ongoing contest?

– Sp3000 – 2015-02-23T04:16:05.753

This question will get put on hold or closed as off topic if you don't give it an objective winning criterion. I would suggest code-golf (shortest code, in bytes) as it will be easy to determine the winner. Fastest code is likely to attract less answers (though they might be more interesting) and it requires you to test the answers to determine the winner. – Level River St – 2015-02-23T05:53:42.993

I'm getting an answer of 4, not 3, for t=3, n=2, with the sequences RSS, SRS, SSR, RLR. Am I missing a rule? – xnor – 2015-02-23T06:52:33.667

@xnor: yep, you're absolutely right; added it in as an edit. – StChong8 – 2015-02-23T07:40:03.073

@Sp3000: done, as suggested; no, the contest is not ongoing. it's different from the typical programming contest that is open for a specified duration, this one is self-initiated as and when you please, and then a countdown timer starts from that time of your choosing. – StChong8 – 2015-02-23T07:43:51.947

@StChong8 I'm also getting 8 for t=4, n=3. – xnor – 2015-02-23T07:43:54.727

@xnor t=4,n=3: the 7 that i get - RRLR, RRNN, RNRN, NNRR, NRRN, RNNR, RLRR – StChong8 – 2015-02-23T07:54:45.983

@StChong8 OK, I'm now getting the same values with the edit that you must stand still after arriving at the rightmost square, including 7 for t=4, n=3. – xnor – 2015-02-23T07:56:35.163

3If this is from another contest, permanent or not, I still don't think it's a good idea to post it here, in particular, because it might violate their copyright. – Martin Ender – 2015-02-23T07:56:53.553

@xnor: yea, sorry about that. – StChong8 – 2015-02-23T07:56:57.473

@StChong8 "@xnor t=4,n=3: the 7 that i get - RRLR" How is RRLR supposed to be valid ? – Mukul Kumar – 2015-02-23T11:34:43.987

@StChong8 can we move left on leftmost pos'n?Plz be quick as I cannot post my code unless you answer. – Mukul Kumar – 2015-02-23T12:21:59.777

1

In view of our policy on posting challenges from competitions, I'm putting this on hold for now.

– Doorknob – 2015-02-23T12:49:42.233

@Doorknob is that enough to reopen this question ? – Mukul Kumar – 2015-02-23T15:22:17.333

@MukulKumar The winning criterion has nothing to do with Doorknob's close vote. That was the close reason es1024 and I chose before a winning criterion was added. While this is no longer the appropriate close reason, I thought the challenge should still be closed, as stated in a comment above, and linked to by Doorknob - since I can't retract my close vote and recast it with another reason I just left it as it is. – Martin Ender – 2015-02-23T15:40:00.610

@MukulKumar: no, moving left from the leftmost pos (ie. starting square) is not allowed. – StChong8 – 2015-02-23T15:53:24.577

@StChong8 "@xnor t=4,n=3: the 7 that i get - RRLR" How is RRLR supposed to be valid ? – Mukul Kumar – 2015-02-23T16:02:53.287

Is this from a "virtual contest" for practicing purpose in preparation of real contests? In that case I think it is fine. – jimmy23013 – 2015-02-24T04:35:23.987

Answers

6

Python, 74

f=lambda t,n,i=1:i==n or t*i and sum(f(t-1,n,i+d)for d in[-1,0,1])%(1e9+7)

A recusive solution. The current position i is stored 1-indexed as an optional argument.

  • If the current position i==n, we've arrived at the final square and are forced to stay still, giving 1 path.
  • Otherwise,
    • If the remaining time t==0, we've run out of time, so there's 0 paths.
    • If the current position i==0, we've run off the left end already, so there's 0 paths.
  • If neither of those was true, there we recurse. We add up the path counts for each of the directions left, none, and right, corresponding to displacements [-1,0,1], and take the result modulo 1e9+7 as required.

An alternative solution that uses eval with string substitution to do the sum. Same number of chars. Note the use of unary + to start the sum.

f=lambda t,n,i=1:i==n or t*i and eval(3*'+f(t-1,n,i+%d)'%(-1,0,1))%(1e9+7)

Here's another a solution that is surely short for any language with built-in matrices, so anyone who can golf it is welcome to take it.

Let M be the n*n transition matrix for the walk, which is a tridiagonal banded matrix with 1's on the main diagonal and the diagonals above and below it, except for a 0 for movement left from the rightmost cell.

1 1 0 0 0
1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
0 0 0 0 1

Then, the output is (1,n) entry of the matrix power M^t.

xnor

Posted 2015-02-23T03:22:14.713

Reputation: 115 687

Nice and short! The faster way to do it would be to set up an array with one cell for each square of the board and iterate t times. Then on each iteration, add the values of the cells to left and right to it, similar to Pascal's triangle. I have a 126 byte ruby solution based on that approach, which I can't post now :-S Matrix idea is cool too. – Level River St – 2015-02-23T14:26:10.420

Thks; pls share more details how you came up with/derived the n*n transition matrix? – StChong8 – 2015-02-23T15:53:58.940