Created
January 9, 2019 07:51
-
-
Save ClearNB/c6b3d30588255f0d2c8cc54e9d51e48d to your computer and use it in GitHub Desktop.
Lattice paths (Java 8 ver) Sample by ClearNB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public static void main(String[] args) { | |
long mod = 1000000007; | |
try (Scanner sc = new Scanner(System.in)) { | |
int T = sc.nextInt(); | |
for(int i = 0; i < T; i++) { | |
int N = sc.nextInt(); | |
int M = sc.nextInt(); | |
N++; | |
M++; | |
long[][] dp = new long[N][M]; | |
for(int j = 0; j < N; j++) { | |
dp[j][0] = 1; | |
} | |
for(int j = 0; j < M; j++) { | |
dp[0][j] = 1; | |
} | |
for(int j = 1; j < N; j++) { | |
for(int k = 1; k < M; k++) { | |
dp[j][k] = (dp[j - 1][k] + dp[j][k - 1]) % mod; | |
} | |
} | |
System.out.println(dp[N - 1][M - 1]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment