# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
331286 | AnythingWithJ | A Huge Tower (CEOI10_tower) | Java | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
static TreeSet<Integer> visited;
static int MOD = 1000000009;
static List<List<Integer>> graph;
static int N, D;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer s = new StringTokenizer(br.readLine());
N = Integer.parseInt(s.nextToken());
D = Integer.parseInt(s.nextToken());
int [] towers = new int[N+1];
s = new StringTokenizer(br.readLine());
for (int i = 1; i <= N; i++) {
towers[i] = Integer.parseInt(s.nextToken());
}
graph = new ArrayList<>();
for (int i = 0; i <= N; i++) {
graph.add(new ArrayList<>());
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (j==i) continue;
if (towers[i] <= towers[j]+D) {
graph.get(i).add(j);
}
}
}
visited = new TreeSet<>();
long res = 0;
for (int i = 1; i <= N; i++) {
visited.clear();
res += dfs (i);
res %= MOD;
}
System.out.println(res);
}
static long dfs (int currNode) {
if (visited.contains(currNode)) return 0;
visited.add(currNode);
//System.out.println(currNode+" start");
//System.out.println(visited);
if (visited.size() == N) {
visited.remove(currNode);
return 1;
}
long currRes = 0;
for (int adj : graph.get(currNode)) {
if (!visited.contains(adj)) currRes += dfs(adj);
currRes %= MOD;
}
visited.remove(currNode);
//System.out.println(currNode+" end");
//System.out.println(visited);
return currRes;
}
}