Skip to content

Instantly share code, notes, and snippets.

@suicide
Created February 3, 2013 18:58
Show Gist options
  • Save suicide/4703161 to your computer and use it in GitHub Desktop.
Save suicide/4703161 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2013 Round 1 DeadPixels Java
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* @author psy
*
*/
public class DeadPixels {
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<String> output = new LinkedList<String>();
DeadPixels testCase = new DeadPixels();
int games = Integer.parseInt(in.nextLine());
for (int i = 1; i <= games; i++) {
int w = in.nextInt();
int h = in.nextInt();
int p = in.nextInt();
int q = in.nextInt();
int n = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
int result = testCase.findPositions(w, h, p, q, n, x, y, a, b, c, d);
output.add("Case #" + i + ": " + result);
}
for (String out : output) {
System.out.println(out);
}
}
public int findPositions(int w, int h, int p, int q, int n, int x, int y, int a, int b, int c, int d) {
int[] xArray = new int[n];
int[] yArray = new int[n];
xArray[0] = x;
yArray[0] = y;
for (int i = 1; i < n; i++) {
xArray[i] = (xArray[i - 1] * a + yArray[i - 1] * b + 1) % w;
yArray[i] = (xArray[i - 1] * c + yArray[i - 1] * d + 1) % h;
}
// find unique positions
int uniquePositions = 0;
for (int i = 0; i < w - p + 1; i++) {
for (int j = 0; j < h - q + 1; j++) {
int endP = i + p;
int endQ = j + q;
boolean fit = true;
for (int z = 0; z < n; z++) {
if (xArray[z] >= i && xArray[z] < endP) {
if (yArray[z] >= j && yArray[z] < endQ) {
fit = false;
break;
}
}
}
if (fit) {
uniquePositions++;
}
}
}
return uniquePositions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment