Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created October 3, 2016 21:51
Show Gist options
  • Save chermehdi/2a79c77cbaed378b82c5a6342f0be9aa to your computer and use it in GitHub Desktop.
Save chermehdi/2a79c77cbaed378b82c5a6342f0be9aa to your computer and use it in GitHub Desktop.
Solution to hackerEarth Dexter
package com.algorithms;
/**
* @Author Mehdi Maick
* Created on 03/10/2016.
*/
import java.util.*;
import java.io.*;
public class DexterAndMandrak {
static int t, n, m;
static int MOD = (int) 1e9 + 7;
static int[][] memo = new int[1010][1010];
public static int solve(int curr, int opp) {
if (memo[curr][opp] != -1) {
return memo[curr][opp];
}
if (curr + opp == n) {
if (curr > opp) {
return 1;
} else {
return 0;
}
}
int ans = solve(curr + 1, opp) % MOD;
if (curr >= (m * (opp + 1))) {
ans += solve(curr, opp + 1) % MOD;
}
memo[curr][opp] = ans;
return memo[curr][opp];
}
public static void solve(FastReader fs, PrintWriter pw) {
t = fs.nextInt();
while (t-- > 0) {
n = fs.nextInt();
m = fs.nextInt();
for (int i = 0; i <= n; i++)
Arrays.fill(memo[i], -1);
pw.println(solve(0, 0));
}
}
public static void main(String[] args) throws Exception {
FastReader fs = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
solve(fs, pw);
fs.close();
pw.close();
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nextLong();
}
return arr;
}
int[] nextIntArrayOneBased(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArrayOneBased(int n) {
long[] arr = new long[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextLong();
}
return arr;
}
void close() {
try {
reader.close();
} catch (IOException e) {
System.err.println("There's been an error trying closing the reader ");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment