UCV2013E - Greedy Walking

no tags 

Reginald is an N-dimensional traveler who wants to return to Filipistonia's Kingdom. He has an Obsessive-compulsive disorder in the way he travels so he can only do it following particular rules:

  1. Every step is exactly one unit long.
  2. He only moves in one dimension at a time.
  3. He only travels along the positive direction for each dimension.

For example, when traveling on a two-dimensional place. He can travel along either the X or the Y axis at any given time, but never on both at the same time. Moreover, since he only travels along the positive direction and every step is one unit long, his only possible moves are (+1, 0) and (0, +1).

Greedy Walking example

As you can see, he is a Greedy Walker: once he makes a decision he assumes it is the correct and he never goes back.

Given a starting position in an N-dimensional space (x1i, x2i, ... ,xni) your task is to count the number of diff erent travels he can make to position (x1f, x2f, ... , xnf) modulo 1000000007.


The input contains several test cases, each one corresponding to a single travel. Each test case consists of a single line with one integer (1 <= N <=  50) followed by two lines each one with N integers, fi rst line will be initial position and second line will be target position.

You can assume that 0 <= xki <= x1f <= 500 for all k, 1 <= k <= N and Sum(xki-xkf) <= 500.

The end of input is indicated by a test case with N = 0.


For each travel output a single line with one integer, the number of diff erent travels that exist from the initial position to the final position modulo 1000000007.


2 1
5 5
0 0 0 0
1 2 3 4
1 2 3 4 5
8 5 6 4 8
0 0 0 0 0
100 100 100 100 100
0 Output: 35

hide comments
Utkarsh Rastogi: 2015-01-19 17:07:55

my 100th on spoj.. :)

Hector Navarro: 2013-07-29 13:05:39

@BroadSword there is only one way to get from a point to itself...

BroadSword: 2013-07-29 11:36:40

If initial and target positions are same..the result should be 0 or 1?

Hector Navarro: 2013-07-25 15:44:00

@SITH: Your program generates no output... I catched an exception:

Last edit: 2013-07-25 15:44:18
SWOOSH!!!: 2013-07-24 06:32:24

I dont why I am getting WA again and again...@Hector could you explain to me why this is happening? Submission id 9708968

Added by:Hector Navarro
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Local UCV 2013. Fernando Crema