f1
stringlengths 6
6
| f2
stringlengths 6
6
| content_f1
stringlengths 149
20.2k
| content_f2
stringlengths 149
20.2k
| flag
int64 0
1
| __index_level_0__
int64 0
3.83k
|
---|---|---|---|---|---|
C20026 | C20078 | import java.io.*;
import java.util.*;
import java.math.*;
class D
{
private static final boolean DEBUG_ON = true;
private static final boolean ECHO_ON = true;
private static BufferedReader input;
private static BufferedWriter output;
private static final int INF = Integer.MAX_VALUE / 2;
private static final int MOD = 10007;
private static int H, W, D, row, col;
public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);}
public static int sqrt(int X)
{
int answer = 1, interval = 1;
for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;}
while (interval >= 1)
{
while ((answer * answer) <= X) {answer += interval;}
answer -= interval;
interval /= 10;
}
return answer;
}
public static void main(String[] args)
{
try
{
input = new BufferedReader(new FileReader(args[0] + ".in"));
output = new BufferedWriter(new FileWriter(args[0] + ".out"));
String line = input.readLine();
int testcases = getInt(line, 0);
for (int testcase = 1; testcase <= testcases; testcase++)
{
char[][] real = getCharMatrix(input);
HashSet<Integer> valid = new HashSet<Integer>();
for (int i = row - D; i <= row + D; i++)
{
int range = sqrt((D * D) - ((i - row) * (i - row)));
for (int j = col - range; j <= col + range; j++)
{
int diffX = i - row;
int diffY = j - col;
if (0 == diffX && 0 == diffY) {continue;}
int gcd = gcd(Math.abs(diffX), Math.abs(diffY));
int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D);
if (valid.contains(direction)) {continue;}
int x = 100 * row + 50;
int y = 100 * col + 50;
for (int k = 0; k < 100; k++)
{
int nextX = x + diffX;
int nextY = y + diffY;
int xCell = x / 100;
int yCell = y / 100;
int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;}
int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;}
if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;}
else if (xCell != nextXCell && yCell == nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if (xCell == nextXCell && yCell != nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else
{
int cornerX = -1, cornerY = -1;
if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);}
else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);}
else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;}
else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;}
if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner
else
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner
else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;}
}
if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;}
}
}
}
String result = "Case #" + testcase + ": " + valid.size();
output(result);
}
input.close();
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));}
public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));}
public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));}
public static String getString(String line, int index)
{
line = line.trim();
while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;}
if ((-1) == line.indexOf(' ')) {return line;}
else {return line.substring(0, line.indexOf(' '));}
}
public static int[] getIntArray(String line)
{
String[] strings = getStringArray(line);
int[] numbers = new int[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);}
return numbers;
}
public static long[] getLongArray(String line)
{
String[] strings = getStringArray(line);
long[] numbers = new long[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);}
return numbers;
}
public static double[] getDoubleArray(String line)
{
String[] strings = getStringArray(line);
double[] numbers = new double[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);}
return numbers;
}
public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);}
public static int[] getIntArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
int[] numbers = new int[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);}
return numbers;
}
public static long[] getLongArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
long[] numbers = new long[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);}
return numbers;
}
public static double[] getDoubleArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
double[] numbers = new double[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);}
return numbers;
}
public static String[] getStringArray(String line, int begin, int end)
{
String[] lines = line.trim().split("(\\s)+", 0);
String[] results = new String[end - begin];
for (int i = begin; i < end; i++) {results[i - begin] = lines[i];}
return results;
}
public static char[][] getCharMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
H = getInt(line, 0);
W = getInt(line, 1);
D = getInt(line, 2);
char[][] matrix = new char[H][W];
for (int i = 0; i < H; i++)
{
line = input.readLine();
for (int j = 0; j < W; j++)
{
char c = matrix[i][j] = line.charAt(j);
if ('X' == c) {row = i; col = j;}
}
}
return matrix;
}
public static int[][] getIntMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
int R = getInt(line, 0);
int C = getInt(line, 1);
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++)
{
line = input.readLine();
for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);}
}
return matrix;
}
public static boolean[][] newBooleanMatrix(int R, int C, boolean value)
{
boolean[][] matrix = new boolean[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static char[][] newCharMatrix(int R, int C, char value)
{
char[][] matrix = new char[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static int[][] newIntMatrix(int R, int C, int value)
{
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static long[][] newLongMatrix(int R, int C, long value)
{
long[][] matrix = new long[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static double[][] newDoubleMatrix(int R, int C, double value)
{
double[][] matrix = new double[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static void output(String s) throws Exception
{
if (ECHO_ON) {System.out.println(s);}
output.write(s);
output.newLine();
}
public static String toKey(boolean[] array)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
for (int i = 0; i < array.length / 16; i++)
{
char c = 0;
for (int j = 0; j < 16; j++)
{
c <<= 1; if (array[i * 16 + j]) {c += 1;}
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % 16); j++)
{
c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;}
}
buffer.append(c + "");
return buffer.toString();
}
public static String toKey(int[] array, int bit)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
if (bit > 16)
{
for (int i = 0; i < array.length; i++)
{
char c1 = (char)(array[i] >> 16);
char c2 = (char)(array[i] & 0xFFFF);
buffer.append("" + c1 + c2);
}
}
else
{
int n = 16 / bit;
for (int i = 0; i < array.length / n; i++)
{
char c = 0;
for (int j = 0; j < n; j++)
{
c <<= bit; c += array[i * n + j];
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % n); j++)
{
c <<= bit; c += array[(array.length / n) * n + j];
}
buffer.append(c + "");
}
return buffer.toString();
}
public static void debug(String s)
{if (DEBUG_ON) {System.out.println(s);}}
public static void debug(String s0, double l0)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0);}}
public static void debug(String s0, double l0, String s1, double l1)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2)
{if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}}
public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);}
buffer.append((array[array.length - 1] == true) ? "1" : "0");
System.out.println(buffer.toString());
}
}
public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
}
| import java.awt.Point;
public class FracVec {
public final Frac row, col;
public FracVec(Frac row, Frac col) {
this.row = row;
this.col = col;
}
public FracVec(Point pos) {
row = new Frac(pos.y);
col = new Frac(pos.x);
}
public FracVec flipHoriz() {
return new FracVec(row, col.neg());
}
public FracVec flipVert() {
return new FracVec(row.neg(), col);
}
public boolean isCorner() {
return row.denom == 1 && col.denom == 1;
}
public FracVec add(FracVec other) {
return new FracVec(row.add(other.row), col.add(other.col));
}
public FracVec sub(FracVec other) {
return new FracVec(row.sub(other.row), col.sub(other.col));
}
public FracVec neg() {
return new FracVec(row.neg(), col.neg());
}
public boolean equals(FracVec other) {
return row.equals(other.row) && col.equals(other.col);
}
public boolean equals(Point p) {
return row.equals(p.y) && col.equals(p.x);
}
@Override
public boolean equals(Object other) {
return other instanceof FracVec && equals((FracVec) other);
}
@Override
public int hashCode() {
return (31 + row.hashCode()) * 31 + col.hashCode();
}
public String toString() {
return "( " + row + ", " + col + " )";
}
public FracVec multiply(Frac scalar) {
return new FracVec(row.mult(scalar), col.mult(scalar));
}
public int compareLengthTo(int d) {
return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d));
}
public Point getNextCell(FracVec dm) {
int resRow, resCol;
if (dm.row.sgn() < 0) {
resRow = row.roundDownAddSmallDown();
} else {
resRow = row.roundDownAddSmallUp();
}
if (dm.col.sgn() < 0)
resCol = col.roundDownAddSmallDown();
else
resCol = col.roundDownAddSmallUp();
return new Point(resCol, resRow);
}
}
| 0 | 3,800 |
C20026 | C20057 | import java.io.*;
import java.util.*;
import java.math.*;
class D
{
private static final boolean DEBUG_ON = true;
private static final boolean ECHO_ON = true;
private static BufferedReader input;
private static BufferedWriter output;
private static final int INF = Integer.MAX_VALUE / 2;
private static final int MOD = 10007;
private static int H, W, D, row, col;
public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);}
public static int sqrt(int X)
{
int answer = 1, interval = 1;
for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;}
while (interval >= 1)
{
while ((answer * answer) <= X) {answer += interval;}
answer -= interval;
interval /= 10;
}
return answer;
}
public static void main(String[] args)
{
try
{
input = new BufferedReader(new FileReader(args[0] + ".in"));
output = new BufferedWriter(new FileWriter(args[0] + ".out"));
String line = input.readLine();
int testcases = getInt(line, 0);
for (int testcase = 1; testcase <= testcases; testcase++)
{
char[][] real = getCharMatrix(input);
HashSet<Integer> valid = new HashSet<Integer>();
for (int i = row - D; i <= row + D; i++)
{
int range = sqrt((D * D) - ((i - row) * (i - row)));
for (int j = col - range; j <= col + range; j++)
{
int diffX = i - row;
int diffY = j - col;
if (0 == diffX && 0 == diffY) {continue;}
int gcd = gcd(Math.abs(diffX), Math.abs(diffY));
int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D);
if (valid.contains(direction)) {continue;}
int x = 100 * row + 50;
int y = 100 * col + 50;
for (int k = 0; k < 100; k++)
{
int nextX = x + diffX;
int nextY = y + diffY;
int xCell = x / 100;
int yCell = y / 100;
int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;}
int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;}
if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;}
else if (xCell != nextXCell && yCell == nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if (xCell == nextXCell && yCell != nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else
{
int cornerX = -1, cornerY = -1;
if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);}
else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);}
else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;}
else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;}
if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner
else
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner
else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;}
}
if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;}
}
}
}
String result = "Case #" + testcase + ": " + valid.size();
output(result);
}
input.close();
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));}
public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));}
public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));}
public static String getString(String line, int index)
{
line = line.trim();
while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;}
if ((-1) == line.indexOf(' ')) {return line;}
else {return line.substring(0, line.indexOf(' '));}
}
public static int[] getIntArray(String line)
{
String[] strings = getStringArray(line);
int[] numbers = new int[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);}
return numbers;
}
public static long[] getLongArray(String line)
{
String[] strings = getStringArray(line);
long[] numbers = new long[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);}
return numbers;
}
public static double[] getDoubleArray(String line)
{
String[] strings = getStringArray(line);
double[] numbers = new double[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);}
return numbers;
}
public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);}
public static int[] getIntArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
int[] numbers = new int[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);}
return numbers;
}
public static long[] getLongArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
long[] numbers = new long[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);}
return numbers;
}
public static double[] getDoubleArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
double[] numbers = new double[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);}
return numbers;
}
public static String[] getStringArray(String line, int begin, int end)
{
String[] lines = line.trim().split("(\\s)+", 0);
String[] results = new String[end - begin];
for (int i = begin; i < end; i++) {results[i - begin] = lines[i];}
return results;
}
public static char[][] getCharMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
H = getInt(line, 0);
W = getInt(line, 1);
D = getInt(line, 2);
char[][] matrix = new char[H][W];
for (int i = 0; i < H; i++)
{
line = input.readLine();
for (int j = 0; j < W; j++)
{
char c = matrix[i][j] = line.charAt(j);
if ('X' == c) {row = i; col = j;}
}
}
return matrix;
}
public static int[][] getIntMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
int R = getInt(line, 0);
int C = getInt(line, 1);
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++)
{
line = input.readLine();
for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);}
}
return matrix;
}
public static boolean[][] newBooleanMatrix(int R, int C, boolean value)
{
boolean[][] matrix = new boolean[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static char[][] newCharMatrix(int R, int C, char value)
{
char[][] matrix = new char[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static int[][] newIntMatrix(int R, int C, int value)
{
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static long[][] newLongMatrix(int R, int C, long value)
{
long[][] matrix = new long[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static double[][] newDoubleMatrix(int R, int C, double value)
{
double[][] matrix = new double[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static void output(String s) throws Exception
{
if (ECHO_ON) {System.out.println(s);}
output.write(s);
output.newLine();
}
public static String toKey(boolean[] array)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
for (int i = 0; i < array.length / 16; i++)
{
char c = 0;
for (int j = 0; j < 16; j++)
{
c <<= 1; if (array[i * 16 + j]) {c += 1;}
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % 16); j++)
{
c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;}
}
buffer.append(c + "");
return buffer.toString();
}
public static String toKey(int[] array, int bit)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
if (bit > 16)
{
for (int i = 0; i < array.length; i++)
{
char c1 = (char)(array[i] >> 16);
char c2 = (char)(array[i] & 0xFFFF);
buffer.append("" + c1 + c2);
}
}
else
{
int n = 16 / bit;
for (int i = 0; i < array.length / n; i++)
{
char c = 0;
for (int j = 0; j < n; j++)
{
c <<= bit; c += array[i * n + j];
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % n); j++)
{
c <<= bit; c += array[(array.length / n) * n + j];
}
buffer.append(c + "");
}
return buffer.toString();
}
public static void debug(String s)
{if (DEBUG_ON) {System.out.println(s);}}
public static void debug(String s0, double l0)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0);}}
public static void debug(String s0, double l0, String s1, double l1)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2)
{if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}}
public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);}
buffer.append((array[array.length - 1] == true) ? "1" : "0");
System.out.println(buffer.toString());
}
}
public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
}
| package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
public class Prime {
public static class PrimeData {
public int[] list;
public boolean[] map;
private PrimeData( final int[] values , final boolean[] map ) {
list = values;
this.map = map;
}
}
public static long[] factorize( long n , final int[] primes ) {
final List< Long > factor = Lists.newArrayList();
for ( final int p : primes ) {
if ( n < p * p ) {
break;
}
while ( n % p == 0 ) {
factor.add( ( long ) p );
n /= p;
}
}
if ( n > 1 ) {
factor.add( n );
}
return Longs.toArray( factor );
}
public static PrimeData prepare( final int n ) {
final List< Integer > primes = Lists.newArrayList();
final boolean[] map = new boolean[ n ];
Arrays.fill( map , true );
map[ 0 ] = map[ 1 ] = false;
primes.add( 2 );
for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) {
map[ composite ] = false;
}
for ( int value = 3 ; value < n ; value += 2 ) {
if ( map[ value ] ) {
primes.add( value );
for ( int composite = value * 2 ; composite < n ; composite += value ) {
map[ composite ] = false;
}
}
}
return new PrimeData( Ints.toArray( primes ) , map );
}
}
| 0 | 3,801 |
C20026 | C20030 | import java.io.*;
import java.util.*;
import java.math.*;
class D
{
private static final boolean DEBUG_ON = true;
private static final boolean ECHO_ON = true;
private static BufferedReader input;
private static BufferedWriter output;
private static final int INF = Integer.MAX_VALUE / 2;
private static final int MOD = 10007;
private static int H, W, D, row, col;
public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);}
public static int sqrt(int X)
{
int answer = 1, interval = 1;
for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;}
while (interval >= 1)
{
while ((answer * answer) <= X) {answer += interval;}
answer -= interval;
interval /= 10;
}
return answer;
}
public static void main(String[] args)
{
try
{
input = new BufferedReader(new FileReader(args[0] + ".in"));
output = new BufferedWriter(new FileWriter(args[0] + ".out"));
String line = input.readLine();
int testcases = getInt(line, 0);
for (int testcase = 1; testcase <= testcases; testcase++)
{
char[][] real = getCharMatrix(input);
HashSet<Integer> valid = new HashSet<Integer>();
for (int i = row - D; i <= row + D; i++)
{
int range = sqrt((D * D) - ((i - row) * (i - row)));
for (int j = col - range; j <= col + range; j++)
{
int diffX = i - row;
int diffY = j - col;
if (0 == diffX && 0 == diffY) {continue;}
int gcd = gcd(Math.abs(diffX), Math.abs(diffY));
int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D);
if (valid.contains(direction)) {continue;}
int x = 100 * row + 50;
int y = 100 * col + 50;
for (int k = 0; k < 100; k++)
{
int nextX = x + diffX;
int nextY = y + diffY;
int xCell = x / 100;
int yCell = y / 100;
int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;}
int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;}
if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;}
else if (xCell != nextXCell && yCell == nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if (xCell == nextXCell && yCell != nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else
{
int cornerX = -1, cornerY = -1;
if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);}
else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);}
else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;}
else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;}
if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner
else
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner
else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;}
}
if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;}
}
}
}
String result = "Case #" + testcase + ": " + valid.size();
output(result);
}
input.close();
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));}
public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));}
public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));}
public static String getString(String line, int index)
{
line = line.trim();
while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;}
if ((-1) == line.indexOf(' ')) {return line;}
else {return line.substring(0, line.indexOf(' '));}
}
public static int[] getIntArray(String line)
{
String[] strings = getStringArray(line);
int[] numbers = new int[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);}
return numbers;
}
public static long[] getLongArray(String line)
{
String[] strings = getStringArray(line);
long[] numbers = new long[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);}
return numbers;
}
public static double[] getDoubleArray(String line)
{
String[] strings = getStringArray(line);
double[] numbers = new double[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);}
return numbers;
}
public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);}
public static int[] getIntArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
int[] numbers = new int[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);}
return numbers;
}
public static long[] getLongArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
long[] numbers = new long[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);}
return numbers;
}
public static double[] getDoubleArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
double[] numbers = new double[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);}
return numbers;
}
public static String[] getStringArray(String line, int begin, int end)
{
String[] lines = line.trim().split("(\\s)+", 0);
String[] results = new String[end - begin];
for (int i = begin; i < end; i++) {results[i - begin] = lines[i];}
return results;
}
public static char[][] getCharMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
H = getInt(line, 0);
W = getInt(line, 1);
D = getInt(line, 2);
char[][] matrix = new char[H][W];
for (int i = 0; i < H; i++)
{
line = input.readLine();
for (int j = 0; j < W; j++)
{
char c = matrix[i][j] = line.charAt(j);
if ('X' == c) {row = i; col = j;}
}
}
return matrix;
}
public static int[][] getIntMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
int R = getInt(line, 0);
int C = getInt(line, 1);
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++)
{
line = input.readLine();
for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);}
}
return matrix;
}
public static boolean[][] newBooleanMatrix(int R, int C, boolean value)
{
boolean[][] matrix = new boolean[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static char[][] newCharMatrix(int R, int C, char value)
{
char[][] matrix = new char[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static int[][] newIntMatrix(int R, int C, int value)
{
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static long[][] newLongMatrix(int R, int C, long value)
{
long[][] matrix = new long[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static double[][] newDoubleMatrix(int R, int C, double value)
{
double[][] matrix = new double[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static void output(String s) throws Exception
{
if (ECHO_ON) {System.out.println(s);}
output.write(s);
output.newLine();
}
public static String toKey(boolean[] array)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
for (int i = 0; i < array.length / 16; i++)
{
char c = 0;
for (int j = 0; j < 16; j++)
{
c <<= 1; if (array[i * 16 + j]) {c += 1;}
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % 16); j++)
{
c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;}
}
buffer.append(c + "");
return buffer.toString();
}
public static String toKey(int[] array, int bit)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
if (bit > 16)
{
for (int i = 0; i < array.length; i++)
{
char c1 = (char)(array[i] >> 16);
char c2 = (char)(array[i] & 0xFFFF);
buffer.append("" + c1 + c2);
}
}
else
{
int n = 16 / bit;
for (int i = 0; i < array.length / n; i++)
{
char c = 0;
for (int j = 0; j < n; j++)
{
c <<= bit; c += array[i * n + j];
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % n); j++)
{
c <<= bit; c += array[(array.length / n) * n + j];
}
buffer.append(c + "");
}
return buffer.toString();
}
public static void debug(String s)
{if (DEBUG_ON) {System.out.println(s);}}
public static void debug(String s0, double l0)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0);}}
public static void debug(String s0, double l0, String s1, double l1)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2)
{if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}}
public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);}
buffer.append((array[array.length - 1] == true) ? "1" : "0");
System.out.println(buffer.toString());
}
}
public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class RayVector {
public final int x;
public final int y;
public RayVector(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof RayVector) {
RayVector v = (RayVector)obj;
if (v.x * y == v.y * x)
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.x;
hash = 41 * hash + this.y;
return hash;
}
public Fraction getScalarGardient() {
return new Fraction(Math.abs(y), Math.abs(x));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| 0 | 3,802 |
C20026 | C20002 | import java.io.*;
import java.util.*;
import java.math.*;
class D
{
private static final boolean DEBUG_ON = true;
private static final boolean ECHO_ON = true;
private static BufferedReader input;
private static BufferedWriter output;
private static final int INF = Integer.MAX_VALUE / 2;
private static final int MOD = 10007;
private static int H, W, D, row, col;
public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);}
public static int sqrt(int X)
{
int answer = 1, interval = 1;
for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;}
while (interval >= 1)
{
while ((answer * answer) <= X) {answer += interval;}
answer -= interval;
interval /= 10;
}
return answer;
}
public static void main(String[] args)
{
try
{
input = new BufferedReader(new FileReader(args[0] + ".in"));
output = new BufferedWriter(new FileWriter(args[0] + ".out"));
String line = input.readLine();
int testcases = getInt(line, 0);
for (int testcase = 1; testcase <= testcases; testcase++)
{
char[][] real = getCharMatrix(input);
HashSet<Integer> valid = new HashSet<Integer>();
for (int i = row - D; i <= row + D; i++)
{
int range = sqrt((D * D) - ((i - row) * (i - row)));
for (int j = col - range; j <= col + range; j++)
{
int diffX = i - row;
int diffY = j - col;
if (0 == diffX && 0 == diffY) {continue;}
int gcd = gcd(Math.abs(diffX), Math.abs(diffY));
int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D);
if (valid.contains(direction)) {continue;}
int x = 100 * row + 50;
int y = 100 * col + 50;
for (int k = 0; k < 100; k++)
{
int nextX = x + diffX;
int nextY = y + diffY;
int xCell = x / 100;
int yCell = y / 100;
int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;}
int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;}
if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;}
else if (xCell != nextXCell && yCell == nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if (xCell == nextXCell && yCell != nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else
{
int cornerX = -1, cornerY = -1;
if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);}
else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);}
else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;}
else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;}
if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner
else
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner
else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;}
}
if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;}
}
}
}
String result = "Case #" + testcase + ": " + valid.size();
output(result);
}
input.close();
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));}
public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));}
public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));}
public static String getString(String line, int index)
{
line = line.trim();
while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;}
if ((-1) == line.indexOf(' ')) {return line;}
else {return line.substring(0, line.indexOf(' '));}
}
public static int[] getIntArray(String line)
{
String[] strings = getStringArray(line);
int[] numbers = new int[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);}
return numbers;
}
public static long[] getLongArray(String line)
{
String[] strings = getStringArray(line);
long[] numbers = new long[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);}
return numbers;
}
public static double[] getDoubleArray(String line)
{
String[] strings = getStringArray(line);
double[] numbers = new double[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);}
return numbers;
}
public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);}
public static int[] getIntArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
int[] numbers = new int[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);}
return numbers;
}
public static long[] getLongArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
long[] numbers = new long[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);}
return numbers;
}
public static double[] getDoubleArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
double[] numbers = new double[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);}
return numbers;
}
public static String[] getStringArray(String line, int begin, int end)
{
String[] lines = line.trim().split("(\\s)+", 0);
String[] results = new String[end - begin];
for (int i = begin; i < end; i++) {results[i - begin] = lines[i];}
return results;
}
public static char[][] getCharMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
H = getInt(line, 0);
W = getInt(line, 1);
D = getInt(line, 2);
char[][] matrix = new char[H][W];
for (int i = 0; i < H; i++)
{
line = input.readLine();
for (int j = 0; j < W; j++)
{
char c = matrix[i][j] = line.charAt(j);
if ('X' == c) {row = i; col = j;}
}
}
return matrix;
}
public static int[][] getIntMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
int R = getInt(line, 0);
int C = getInt(line, 1);
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++)
{
line = input.readLine();
for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);}
}
return matrix;
}
public static boolean[][] newBooleanMatrix(int R, int C, boolean value)
{
boolean[][] matrix = new boolean[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static char[][] newCharMatrix(int R, int C, char value)
{
char[][] matrix = new char[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static int[][] newIntMatrix(int R, int C, int value)
{
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static long[][] newLongMatrix(int R, int C, long value)
{
long[][] matrix = new long[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static double[][] newDoubleMatrix(int R, int C, double value)
{
double[][] matrix = new double[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static void output(String s) throws Exception
{
if (ECHO_ON) {System.out.println(s);}
output.write(s);
output.newLine();
}
public static String toKey(boolean[] array)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
for (int i = 0; i < array.length / 16; i++)
{
char c = 0;
for (int j = 0; j < 16; j++)
{
c <<= 1; if (array[i * 16 + j]) {c += 1;}
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % 16); j++)
{
c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;}
}
buffer.append(c + "");
return buffer.toString();
}
public static String toKey(int[] array, int bit)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
if (bit > 16)
{
for (int i = 0; i < array.length; i++)
{
char c1 = (char)(array[i] >> 16);
char c2 = (char)(array[i] & 0xFFFF);
buffer.append("" + c1 + c2);
}
}
else
{
int n = 16 / bit;
for (int i = 0; i < array.length / n; i++)
{
char c = 0;
for (int j = 0; j < n; j++)
{
c <<= bit; c += array[i * n + j];
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % n); j++)
{
c <<= bit; c += array[(array.length / n) * n + j];
}
buffer.append(c + "");
}
return buffer.toString();
}
public static void debug(String s)
{if (DEBUG_ON) {System.out.println(s);}}
public static void debug(String s0, double l0)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0);}}
public static void debug(String s0, double l0, String s1, double l1)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2)
{if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}}
public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);}
buffer.append((array[array.length - 1] == true) ? "1" : "0");
System.out.println(buffer.toString());
}
}
public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
}
| import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Double.parseDouble;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collection.*;
public class D
{
static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); }
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int T = parseInt(br.readLine());
for(int t = 0; t++ < T; )
{
String[] line = br.readLine().split(" ");
int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]);
char[][] G = new char[H][];
for(int h = 0; h < H; h++)
G[h] = br.readLine().toCharArray();
int X = 0, Y = 0;
outer:for(Y = 0; Y < H; Y++)
for(X = 0; X < W; X++)
if(G[Y][X] == 'X')
break outer;
int count = 0;
for(int i = -D; i <= D; i++)
{
for(int j = -D; j <= D; j++)
{
int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y;
int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy));
if(gcd(Math.abs(dx), Math.abs(dy)) != 1)
continue;
x0 = x = X * scale + scale / 2;
y0 = y = Y * scale + scale / 2;
do
{
steps -= 1;
if(x % scale == 0 && y % scale == 0)
{
// at a corner
int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1;
int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2;
if(G[yi+dyi][xi+dxi] == '#')
{
if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#')
steps = -1; // kill the light
if(G[yi+dyi][xi] == '#')
dy *= -1;
if(G[yi][xi+dxi] == '#')
dx *= -1;
} else ;
// otherwise step as normal
} else if(x % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi][xi-1] == '#')
dx *= -1;
} else if(y % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi-1][xi] == '#')
dy *= -1;
} else ;
// smooth sailing
x += dx;
y += dy;
} while(steps >= 0 && !(x == x0 && y == y0));
if(steps >= 0)
++count;
}
}
out.println("Case #" + t +": " + count) ;
}
}
}
| 0 | 3,803 |
C20026 | C20056 | import java.io.*;
import java.util.*;
import java.math.*;
class D
{
private static final boolean DEBUG_ON = true;
private static final boolean ECHO_ON = true;
private static BufferedReader input;
private static BufferedWriter output;
private static final int INF = Integer.MAX_VALUE / 2;
private static final int MOD = 10007;
private static int H, W, D, row, col;
public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);}
public static int sqrt(int X)
{
int answer = 1, interval = 1;
for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;}
while (interval >= 1)
{
while ((answer * answer) <= X) {answer += interval;}
answer -= interval;
interval /= 10;
}
return answer;
}
public static void main(String[] args)
{
try
{
input = new BufferedReader(new FileReader(args[0] + ".in"));
output = new BufferedWriter(new FileWriter(args[0] + ".out"));
String line = input.readLine();
int testcases = getInt(line, 0);
for (int testcase = 1; testcase <= testcases; testcase++)
{
char[][] real = getCharMatrix(input);
HashSet<Integer> valid = new HashSet<Integer>();
for (int i = row - D; i <= row + D; i++)
{
int range = sqrt((D * D) - ((i - row) * (i - row)));
for (int j = col - range; j <= col + range; j++)
{
int diffX = i - row;
int diffY = j - col;
if (0 == diffX && 0 == diffY) {continue;}
int gcd = gcd(Math.abs(diffX), Math.abs(diffY));
int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D);
if (valid.contains(direction)) {continue;}
int x = 100 * row + 50;
int y = 100 * col + 50;
for (int k = 0; k < 100; k++)
{
int nextX = x + diffX;
int nextY = y + diffY;
int xCell = x / 100;
int yCell = y / 100;
int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;}
int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;}
if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;}
else if (xCell != nextXCell && yCell == nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if (xCell == nextXCell && yCell != nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else
{
int cornerX = -1, cornerY = -1;
if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);}
else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);}
else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;}
else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;}
if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner
else
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner
else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;}
}
if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;}
}
}
}
String result = "Case #" + testcase + ": " + valid.size();
output(result);
}
input.close();
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));}
public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));}
public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));}
public static String getString(String line, int index)
{
line = line.trim();
while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;}
if ((-1) == line.indexOf(' ')) {return line;}
else {return line.substring(0, line.indexOf(' '));}
}
public static int[] getIntArray(String line)
{
String[] strings = getStringArray(line);
int[] numbers = new int[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);}
return numbers;
}
public static long[] getLongArray(String line)
{
String[] strings = getStringArray(line);
long[] numbers = new long[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);}
return numbers;
}
public static double[] getDoubleArray(String line)
{
String[] strings = getStringArray(line);
double[] numbers = new double[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);}
return numbers;
}
public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);}
public static int[] getIntArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
int[] numbers = new int[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);}
return numbers;
}
public static long[] getLongArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
long[] numbers = new long[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);}
return numbers;
}
public static double[] getDoubleArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
double[] numbers = new double[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);}
return numbers;
}
public static String[] getStringArray(String line, int begin, int end)
{
String[] lines = line.trim().split("(\\s)+", 0);
String[] results = new String[end - begin];
for (int i = begin; i < end; i++) {results[i - begin] = lines[i];}
return results;
}
public static char[][] getCharMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
H = getInt(line, 0);
W = getInt(line, 1);
D = getInt(line, 2);
char[][] matrix = new char[H][W];
for (int i = 0; i < H; i++)
{
line = input.readLine();
for (int j = 0; j < W; j++)
{
char c = matrix[i][j] = line.charAt(j);
if ('X' == c) {row = i; col = j;}
}
}
return matrix;
}
public static int[][] getIntMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
int R = getInt(line, 0);
int C = getInt(line, 1);
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++)
{
line = input.readLine();
for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);}
}
return matrix;
}
public static boolean[][] newBooleanMatrix(int R, int C, boolean value)
{
boolean[][] matrix = new boolean[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static char[][] newCharMatrix(int R, int C, char value)
{
char[][] matrix = new char[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static int[][] newIntMatrix(int R, int C, int value)
{
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static long[][] newLongMatrix(int R, int C, long value)
{
long[][] matrix = new long[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static double[][] newDoubleMatrix(int R, int C, double value)
{
double[][] matrix = new double[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static void output(String s) throws Exception
{
if (ECHO_ON) {System.out.println(s);}
output.write(s);
output.newLine();
}
public static String toKey(boolean[] array)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
for (int i = 0; i < array.length / 16; i++)
{
char c = 0;
for (int j = 0; j < 16; j++)
{
c <<= 1; if (array[i * 16 + j]) {c += 1;}
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % 16); j++)
{
c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;}
}
buffer.append(c + "");
return buffer.toString();
}
public static String toKey(int[] array, int bit)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
if (bit > 16)
{
for (int i = 0; i < array.length; i++)
{
char c1 = (char)(array[i] >> 16);
char c2 = (char)(array[i] & 0xFFFF);
buffer.append("" + c1 + c2);
}
}
else
{
int n = 16 / bit;
for (int i = 0; i < array.length / n; i++)
{
char c = 0;
for (int j = 0; j < n; j++)
{
c <<= bit; c += array[i * n + j];
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % n); j++)
{
c <<= bit; c += array[(array.length / n) * n + j];
}
buffer.append(c + "");
}
return buffer.toString();
}
public static void debug(String s)
{if (DEBUG_ON) {System.out.println(s);}}
public static void debug(String s0, double l0)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0);}}
public static void debug(String s0, double l0, String s1, double l1)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2)
{if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}}
public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);}
buffer.append((array[array.length - 1] == true) ? "1" : "0");
System.out.println(buffer.toString());
}
}
public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
}
| package jp.funnything.competition.util;
import java.math.BigDecimal;
/**
* Utility for BigDeciaml
*/
public class BD {
public static BigDecimal ZERO = BigDecimal.ZERO;
public static BigDecimal ONE = BigDecimal.ONE;
public static BigDecimal add( final BigDecimal x , final BigDecimal y ) {
return x.add( y );
}
public static BigDecimal add( final BigDecimal x , final double y ) {
return add( x , v( y ) );
}
public static BigDecimal add( final double x , final BigDecimal y ) {
return add( v( x ) , y );
}
public static int cmp( final BigDecimal x , final BigDecimal y ) {
return x.compareTo( y );
}
public static int cmp( final BigDecimal x , final double y ) {
return cmp( x , v( y ) );
}
public static int cmp( final double x , final BigDecimal y ) {
return cmp( v( x ) , y );
}
public static BigDecimal div( final BigDecimal x , final BigDecimal y ) {
return x.divide( y );
}
public static BigDecimal div( final BigDecimal x , final double y ) {
return div( x , v( y ) );
}
public static BigDecimal div( final double x , final BigDecimal y ) {
return div( v( x ) , y );
}
public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) {
return x.multiply( y );
}
public static BigDecimal mul( final BigDecimal x , final double y ) {
return mul( x , v( y ) );
}
public static BigDecimal mul( final double x , final BigDecimal y ) {
return mul( v( x ) , y );
}
public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) {
return x.subtract( y );
}
public static BigDecimal sub( final BigDecimal x , final double y ) {
return sub( x , v( y ) );
}
public static BigDecimal sub( final double x , final BigDecimal y ) {
return sub( v( x ) , y );
}
public static BigDecimal v( final double value ) {
return BigDecimal.valueOf( value );
}
}
| 0 | 3,804 |
C20026 | C20018 | import java.io.*;
import java.util.*;
import java.math.*;
class D
{
private static final boolean DEBUG_ON = true;
private static final boolean ECHO_ON = true;
private static BufferedReader input;
private static BufferedWriter output;
private static final int INF = Integer.MAX_VALUE / 2;
private static final int MOD = 10007;
private static int H, W, D, row, col;
public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);}
public static int sqrt(int X)
{
int answer = 1, interval = 1;
for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;}
while (interval >= 1)
{
while ((answer * answer) <= X) {answer += interval;}
answer -= interval;
interval /= 10;
}
return answer;
}
public static void main(String[] args)
{
try
{
input = new BufferedReader(new FileReader(args[0] + ".in"));
output = new BufferedWriter(new FileWriter(args[0] + ".out"));
String line = input.readLine();
int testcases = getInt(line, 0);
for (int testcase = 1; testcase <= testcases; testcase++)
{
char[][] real = getCharMatrix(input);
HashSet<Integer> valid = new HashSet<Integer>();
for (int i = row - D; i <= row + D; i++)
{
int range = sqrt((D * D) - ((i - row) * (i - row)));
for (int j = col - range; j <= col + range; j++)
{
int diffX = i - row;
int diffY = j - col;
if (0 == diffX && 0 == diffY) {continue;}
int gcd = gcd(Math.abs(diffX), Math.abs(diffY));
int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D);
if (valid.contains(direction)) {continue;}
int x = 100 * row + 50;
int y = 100 * col + 50;
for (int k = 0; k < 100; k++)
{
int nextX = x + diffX;
int nextY = y + diffY;
int xCell = x / 100;
int yCell = y / 100;
int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;}
int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;}
if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;}
else if (xCell != nextXCell && yCell == nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if (xCell == nextXCell && yCell != nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else
{
int cornerX = -1, cornerY = -1;
if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);}
else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);}
else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;}
else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;}
if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner
else
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner
else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;}
}
if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;}
}
}
}
String result = "Case #" + testcase + ": " + valid.size();
output(result);
}
input.close();
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));}
public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));}
public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));}
public static String getString(String line, int index)
{
line = line.trim();
while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;}
if ((-1) == line.indexOf(' ')) {return line;}
else {return line.substring(0, line.indexOf(' '));}
}
public static int[] getIntArray(String line)
{
String[] strings = getStringArray(line);
int[] numbers = new int[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);}
return numbers;
}
public static long[] getLongArray(String line)
{
String[] strings = getStringArray(line);
long[] numbers = new long[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);}
return numbers;
}
public static double[] getDoubleArray(String line)
{
String[] strings = getStringArray(line);
double[] numbers = new double[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);}
return numbers;
}
public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);}
public static int[] getIntArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
int[] numbers = new int[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);}
return numbers;
}
public static long[] getLongArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
long[] numbers = new long[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);}
return numbers;
}
public static double[] getDoubleArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
double[] numbers = new double[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);}
return numbers;
}
public static String[] getStringArray(String line, int begin, int end)
{
String[] lines = line.trim().split("(\\s)+", 0);
String[] results = new String[end - begin];
for (int i = begin; i < end; i++) {results[i - begin] = lines[i];}
return results;
}
public static char[][] getCharMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
H = getInt(line, 0);
W = getInt(line, 1);
D = getInt(line, 2);
char[][] matrix = new char[H][W];
for (int i = 0; i < H; i++)
{
line = input.readLine();
for (int j = 0; j < W; j++)
{
char c = matrix[i][j] = line.charAt(j);
if ('X' == c) {row = i; col = j;}
}
}
return matrix;
}
public static int[][] getIntMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
int R = getInt(line, 0);
int C = getInt(line, 1);
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++)
{
line = input.readLine();
for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);}
}
return matrix;
}
public static boolean[][] newBooleanMatrix(int R, int C, boolean value)
{
boolean[][] matrix = new boolean[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static char[][] newCharMatrix(int R, int C, char value)
{
char[][] matrix = new char[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static int[][] newIntMatrix(int R, int C, int value)
{
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static long[][] newLongMatrix(int R, int C, long value)
{
long[][] matrix = new long[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static double[][] newDoubleMatrix(int R, int C, double value)
{
double[][] matrix = new double[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static void output(String s) throws Exception
{
if (ECHO_ON) {System.out.println(s);}
output.write(s);
output.newLine();
}
public static String toKey(boolean[] array)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
for (int i = 0; i < array.length / 16; i++)
{
char c = 0;
for (int j = 0; j < 16; j++)
{
c <<= 1; if (array[i * 16 + j]) {c += 1;}
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % 16); j++)
{
c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;}
}
buffer.append(c + "");
return buffer.toString();
}
public static String toKey(int[] array, int bit)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
if (bit > 16)
{
for (int i = 0; i < array.length; i++)
{
char c1 = (char)(array[i] >> 16);
char c2 = (char)(array[i] & 0xFFFF);
buffer.append("" + c1 + c2);
}
}
else
{
int n = 16 / bit;
for (int i = 0; i < array.length / n; i++)
{
char c = 0;
for (int j = 0; j < n; j++)
{
c <<= bit; c += array[i * n + j];
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % n); j++)
{
c <<= bit; c += array[(array.length / n) * n + j];
}
buffer.append(c + "");
}
return buffer.toString();
}
public static void debug(String s)
{if (DEBUG_ON) {System.out.println(s);}}
public static void debug(String s0, double l0)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0);}}
public static void debug(String s0, double l0, String s1, double l1)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2)
{if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}}
public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);}
buffer.append((array[array.length - 1] == true) ? "1" : "0");
System.out.println(buffer.toString());
}
}
public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
}
| package template;
import java.util.Objects;
public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{
private T1 o1;
private T2 o2;
public Pair(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Pair)) {return false;}
return (compareTo((Pair<T1, T2>)other) == 0);
}
@Override
public int compareTo(Pair<T1, T2> other) {
int c1 = ((Comparable<T1>) o1).compareTo(other.getO1());
if (c1 != 0) {return c1;}
int c2 = ((Comparable<T2>) o2).compareTo(other.getO2());
return c2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.o1);
hash = 83 * hash + Objects.hashCode(this.o2);
return hash;
}
@Override
public String toString()
{
return "[" + o1 + ", " + o2 + "]";
}
public T1 getO1() {
return o1;
}
public void setO1(T1 o1) {
this.o1 = o1;
}
public T2 getO2() {
return o2;
}
public void setO2(T2 o2) {
this.o2 = o2;
}
}
| 0 | 3,805 |
C20026 | C20051 | import java.io.*;
import java.util.*;
import java.math.*;
class D
{
private static final boolean DEBUG_ON = true;
private static final boolean ECHO_ON = true;
private static BufferedReader input;
private static BufferedWriter output;
private static final int INF = Integer.MAX_VALUE / 2;
private static final int MOD = 10007;
private static int H, W, D, row, col;
public static int gcd(int n, int m) {return (0 == m) ? (n) : gcd(m, n % m);}
public static int sqrt(int X)
{
int answer = 1, interval = 1;
for (int i = String.valueOf(X).length() / 2; i > 0; i--) {interval *= 10;}
while (interval >= 1)
{
while ((answer * answer) <= X) {answer += interval;}
answer -= interval;
interval /= 10;
}
return answer;
}
public static void main(String[] args)
{
try
{
input = new BufferedReader(new FileReader(args[0] + ".in"));
output = new BufferedWriter(new FileWriter(args[0] + ".out"));
String line = input.readLine();
int testcases = getInt(line, 0);
for (int testcase = 1; testcase <= testcases; testcase++)
{
char[][] real = getCharMatrix(input);
HashSet<Integer> valid = new HashSet<Integer>();
for (int i = row - D; i <= row + D; i++)
{
int range = sqrt((D * D) - ((i - row) * (i - row)));
for (int j = col - range; j <= col + range; j++)
{
int diffX = i - row;
int diffY = j - col;
if (0 == diffX && 0 == diffY) {continue;}
int gcd = gcd(Math.abs(diffX), Math.abs(diffY));
int direction = (((diffX/gcd) + D) << 16) + ((diffY/gcd) + D);
if (valid.contains(direction)) {continue;}
int x = 100 * row + 50;
int y = 100 * col + 50;
for (int k = 0; k < 100; k++)
{
int nextX = x + diffX;
int nextY = y + diffY;
int xCell = x / 100;
int yCell = y / 100;
int nextXCell = nextX / 100; if (0 == nextX % 100) {nextXCell = (nextX + diffX) / 100;}
int nextYCell = nextY / 100; if (0 == nextY % 100) {nextYCell = (nextY + diffY) / 100;}
if (xCell == nextXCell && yCell == nextYCell) {x = nextX; y = nextY;}
else if (xCell != nextXCell && yCell == nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if (xCell == nextXCell && yCell != nextYCell)
{
if ('#' != real[nextXCell][nextYCell]) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else
{
int cornerX = -1, cornerY = -1;
if (nextXCell < xCell && nextYCell < yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * (nextYCell + 1);}
else if (nextXCell > xCell && nextYCell < yCell) {cornerX = 100 * nextXCell; cornerY = 100 * (nextYCell + 1);}
else if (nextXCell < xCell && nextYCell > yCell) {cornerX = 100 * (nextXCell + 1); cornerY = 100 * nextYCell;}
else if (nextXCell > xCell && nextYCell > yCell) {cornerX = 100 * nextXCell; cornerY = 100 * nextYCell;}
if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {x = nextX; y = nextY;} // passing corner
else
{
diffX = -diffX; diffY = -diffY;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' == real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if ((cornerX - x) * (nextY - y) == (nextX - x) * (cornerY - y)) {break;} // hitting corner
else if (Math.abs((cornerX - x) * (nextY - y)) < Math.abs((nextX - x) * (cornerY - y))) // hitting Y
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' == real[xCell][nextYCell] && '#' != real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) <= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffY = -diffY;
if (nextYCell < yCell) {y = (100 * (nextYCell + 1)) - (y - (100 * (nextYCell + 1)));}
else {y = (100 * nextYCell) + ((100 * nextYCell) - y);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' == real[nextXCell][yCell])
{
if (Math.abs((cornerX - x) * (nextY - y)) >= Math.abs((nextX - x) * (cornerY - y))) {x = nextX; y = nextY;}
else
{
diffX = -diffX;
if (nextXCell < xCell) {x = (100 * (nextXCell + 1)) - (x - (100 * (nextXCell + 1)));}
else {x = (100 * nextXCell) + ((100 * nextXCell) - x);}
x = x + diffX; y = y + diffY;
}
}
else if ('#' != real[nextXCell][nextYCell] && '#' != real[xCell][nextYCell] && '#' != real[nextXCell][yCell]) {x = nextX; y = nextY;}
}
if ((100 * row + 50) == x && (100 * col + 50) == y) {valid.add(direction); break;}
}
}
}
String result = "Case #" + testcase + ": " + valid.size();
output(result);
}
input.close();
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static int getInt(String line, int index) {return Integer.parseInt(getString(line, index));}
public static long getLong(String line, int index) {return Long.parseLong(getString(line, index));}
public static double getDouble(String line, int index) {return Double.parseDouble(getString(line, index));}
public static String getString(String line, int index)
{
line = line.trim();
while (index > 0) {line = line.substring(line.indexOf(' ') + 1); index--;}
if ((-1) == line.indexOf(' ')) {return line;}
else {return line.substring(0, line.indexOf(' '));}
}
public static int[] getIntArray(String line)
{
String[] strings = getStringArray(line);
int[] numbers = new int[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Integer.parseInt(strings[i]);}
return numbers;
}
public static long[] getLongArray(String line)
{
String[] strings = getStringArray(line);
long[] numbers = new long[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Long.parseLong(strings[i]);}
return numbers;
}
public static double[] getDoubleArray(String line)
{
String[] strings = getStringArray(line);
double[] numbers = new double[strings.length];
for (int i = 0; i < strings.length; i++) {numbers[i] = Double.parseDouble(strings[i]);}
return numbers;
}
public static String[] getStringArray(String line) {return line.trim().split("(\\s)+", 0);}
public static int[] getIntArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
int[] numbers = new int[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Integer.parseInt(strings[i - begin]);}
return numbers;
}
public static long[] getLongArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
long[] numbers = new long[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Long.parseLong(strings[i - begin]);}
return numbers;
}
public static double[] getDoubleArray(String line, int begin, int end)
{
String[] strings = getStringArray(line, begin, end);
double[] numbers = new double[end - begin];
for (int i = begin; i < end; i++) {numbers[i - begin] = Double.parseDouble(strings[i - begin]);}
return numbers;
}
public static String[] getStringArray(String line, int begin, int end)
{
String[] lines = line.trim().split("(\\s)+", 0);
String[] results = new String[end - begin];
for (int i = begin; i < end; i++) {results[i - begin] = lines[i];}
return results;
}
public static char[][] getCharMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
H = getInt(line, 0);
W = getInt(line, 1);
D = getInt(line, 2);
char[][] matrix = new char[H][W];
for (int i = 0; i < H; i++)
{
line = input.readLine();
for (int j = 0; j < W; j++)
{
char c = matrix[i][j] = line.charAt(j);
if ('X' == c) {row = i; col = j;}
}
}
return matrix;
}
public static int[][] getIntMatrix(BufferedReader input) throws Exception
{
String line = input.readLine();
int R = getInt(line, 0);
int C = getInt(line, 1);
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++)
{
line = input.readLine();
for (int j = 0; j < C; j++) {matrix[i][j] = getInt(line, j);}
}
return matrix;
}
public static boolean[][] newBooleanMatrix(int R, int C, boolean value)
{
boolean[][] matrix = new boolean[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static char[][] newCharMatrix(int R, int C, char value)
{
char[][] matrix = new char[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static int[][] newIntMatrix(int R, int C, int value)
{
int[][] matrix = new int[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static long[][] newLongMatrix(int R, int C, long value)
{
long[][] matrix = new long[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static double[][] newDoubleMatrix(int R, int C, double value)
{
double[][] matrix = new double[R][C];
for (int i = 0; i < R; i++) for(int j = 0; j < C; j++) {matrix[i][j] = value;}
return matrix;
}
public static void output(String s) throws Exception
{
if (ECHO_ON) {System.out.println(s);}
output.write(s);
output.newLine();
}
public static String toKey(boolean[] array)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
for (int i = 0; i < array.length / 16; i++)
{
char c = 0;
for (int j = 0; j < 16; j++)
{
c <<= 1; if (array[i * 16 + j]) {c += 1;}
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % 16); j++)
{
c <<= 1; if (array[(array.length / 16) * 16 + j]) {c += 1;}
}
buffer.append(c + "");
return buffer.toString();
}
public static String toKey(int[] array, int bit)
{
StringBuffer buffer = new StringBuffer(array.length + ",");
if (bit > 16)
{
for (int i = 0; i < array.length; i++)
{
char c1 = (char)(array[i] >> 16);
char c2 = (char)(array[i] & 0xFFFF);
buffer.append("" + c1 + c2);
}
}
else
{
int n = 16 / bit;
for (int i = 0; i < array.length / n; i++)
{
char c = 0;
for (int j = 0; j < n; j++)
{
c <<= bit; c += array[i * n + j];
}
buffer.append(c + "");
}
char c = 0;
for (int j = 0; j < (array.length % n); j++)
{
c <<= bit; c += array[(array.length / n) * n + j];
}
buffer.append(c + "");
}
return buffer.toString();
}
public static void debug(String s)
{if (DEBUG_ON) {System.out.println(s);}}
public static void debug(String s0, double l0)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0);}}
public static void debug(String s0, double l0, String s1, double l1)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2)
{if (DEBUG_ON) { System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3);}}
public static void debug(String s0, double l0, String s1, double l1, String s2, double l2, String s3, double l3, String s4, double l4)
{if (DEBUG_ON) {System.out.println(s0+" = "+l0+"; "+s1+" = "+l1+"; "+s2+" = "+l2+"; "+s3+" = "+l3+"; "+s4+" = "+l4);}}
public static void debug(boolean[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append((array[i] == true ? "1" : "0") + separator);}
buffer.append((array[array.length - 1] == true) ? "1" : "0");
System.out.println(buffer.toString());
}
}
public static void debug(boolean[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(boolean[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(char[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(char[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(char[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
public static void debug(int[] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[] array, String separator)
{
if (DEBUG_ON)
{
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length - 1; i++) {buffer.append(array[i] + separator);}
buffer.append(array[array.length - 1]);
System.out.println(buffer.toString());
}
}
public static void debug(int[][] array) {if (DEBUG_ON) {debug(array, " ");}}
public static void debug(int[][] array, String separator)
{if (DEBUG_ON) {for (int i = 0; i < array.length; i++) {debug(array[i], separator);}}}
}
| package jp.funnything.competition.util;
import java.util.List;
import com.google.common.collect.Lists;
public class Lists2 {
public static < T > List< T > newArrayListAsArray( final int length ) {
final List< T > list = Lists.newArrayListWithCapacity( length );
for ( int index = 0 ; index < length ; index++ ) {
list.add( null );
}
return list;
}
}
| 0 | 3,806 |
C20078 | C20057 | import java.awt.Point;
public class FracVec {
public final Frac row, col;
public FracVec(Frac row, Frac col) {
this.row = row;
this.col = col;
}
public FracVec(Point pos) {
row = new Frac(pos.y);
col = new Frac(pos.x);
}
public FracVec flipHoriz() {
return new FracVec(row, col.neg());
}
public FracVec flipVert() {
return new FracVec(row.neg(), col);
}
public boolean isCorner() {
return row.denom == 1 && col.denom == 1;
}
public FracVec add(FracVec other) {
return new FracVec(row.add(other.row), col.add(other.col));
}
public FracVec sub(FracVec other) {
return new FracVec(row.sub(other.row), col.sub(other.col));
}
public FracVec neg() {
return new FracVec(row.neg(), col.neg());
}
public boolean equals(FracVec other) {
return row.equals(other.row) && col.equals(other.col);
}
public boolean equals(Point p) {
return row.equals(p.y) && col.equals(p.x);
}
@Override
public boolean equals(Object other) {
return other instanceof FracVec && equals((FracVec) other);
}
@Override
public int hashCode() {
return (31 + row.hashCode()) * 31 + col.hashCode();
}
public String toString() {
return "( " + row + ", " + col + " )";
}
public FracVec multiply(Frac scalar) {
return new FracVec(row.mult(scalar), col.mult(scalar));
}
public int compareLengthTo(int d) {
return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d));
}
public Point getNextCell(FracVec dm) {
int resRow, resCol;
if (dm.row.sgn() < 0) {
resRow = row.roundDownAddSmallDown();
} else {
resRow = row.roundDownAddSmallUp();
}
if (dm.col.sgn() < 0)
resCol = col.roundDownAddSmallDown();
else
resCol = col.roundDownAddSmallUp();
return new Point(resCol, resRow);
}
}
| package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
public class Prime {
public static class PrimeData {
public int[] list;
public boolean[] map;
private PrimeData( final int[] values , final boolean[] map ) {
list = values;
this.map = map;
}
}
public static long[] factorize( long n , final int[] primes ) {
final List< Long > factor = Lists.newArrayList();
for ( final int p : primes ) {
if ( n < p * p ) {
break;
}
while ( n % p == 0 ) {
factor.add( ( long ) p );
n /= p;
}
}
if ( n > 1 ) {
factor.add( n );
}
return Longs.toArray( factor );
}
public static PrimeData prepare( final int n ) {
final List< Integer > primes = Lists.newArrayList();
final boolean[] map = new boolean[ n ];
Arrays.fill( map , true );
map[ 0 ] = map[ 1 ] = false;
primes.add( 2 );
for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) {
map[ composite ] = false;
}
for ( int value = 3 ; value < n ; value += 2 ) {
if ( map[ value ] ) {
primes.add( value );
for ( int composite = value * 2 ; composite < n ; composite += value ) {
map[ composite ] = false;
}
}
}
return new PrimeData( Ints.toArray( primes ) , map );
}
}
| 0 | 3,807 |
C20078 | C20030 | import java.awt.Point;
public class FracVec {
public final Frac row, col;
public FracVec(Frac row, Frac col) {
this.row = row;
this.col = col;
}
public FracVec(Point pos) {
row = new Frac(pos.y);
col = new Frac(pos.x);
}
public FracVec flipHoriz() {
return new FracVec(row, col.neg());
}
public FracVec flipVert() {
return new FracVec(row.neg(), col);
}
public boolean isCorner() {
return row.denom == 1 && col.denom == 1;
}
public FracVec add(FracVec other) {
return new FracVec(row.add(other.row), col.add(other.col));
}
public FracVec sub(FracVec other) {
return new FracVec(row.sub(other.row), col.sub(other.col));
}
public FracVec neg() {
return new FracVec(row.neg(), col.neg());
}
public boolean equals(FracVec other) {
return row.equals(other.row) && col.equals(other.col);
}
public boolean equals(Point p) {
return row.equals(p.y) && col.equals(p.x);
}
@Override
public boolean equals(Object other) {
return other instanceof FracVec && equals((FracVec) other);
}
@Override
public int hashCode() {
return (31 + row.hashCode()) * 31 + col.hashCode();
}
public String toString() {
return "( " + row + ", " + col + " )";
}
public FracVec multiply(Frac scalar) {
return new FracVec(row.mult(scalar), col.mult(scalar));
}
public int compareLengthTo(int d) {
return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d));
}
public Point getNextCell(FracVec dm) {
int resRow, resCol;
if (dm.row.sgn() < 0) {
resRow = row.roundDownAddSmallDown();
} else {
resRow = row.roundDownAddSmallUp();
}
if (dm.col.sgn() < 0)
resCol = col.roundDownAddSmallDown();
else
resCol = col.roundDownAddSmallUp();
return new Point(resCol, resRow);
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class RayVector {
public final int x;
public final int y;
public RayVector(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof RayVector) {
RayVector v = (RayVector)obj;
if (v.x * y == v.y * x)
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.x;
hash = 41 * hash + this.y;
return hash;
}
public Fraction getScalarGardient() {
return new Fraction(Math.abs(y), Math.abs(x));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| 0 | 3,808 |
C20078 | C20002 | import java.awt.Point;
public class FracVec {
public final Frac row, col;
public FracVec(Frac row, Frac col) {
this.row = row;
this.col = col;
}
public FracVec(Point pos) {
row = new Frac(pos.y);
col = new Frac(pos.x);
}
public FracVec flipHoriz() {
return new FracVec(row, col.neg());
}
public FracVec flipVert() {
return new FracVec(row.neg(), col);
}
public boolean isCorner() {
return row.denom == 1 && col.denom == 1;
}
public FracVec add(FracVec other) {
return new FracVec(row.add(other.row), col.add(other.col));
}
public FracVec sub(FracVec other) {
return new FracVec(row.sub(other.row), col.sub(other.col));
}
public FracVec neg() {
return new FracVec(row.neg(), col.neg());
}
public boolean equals(FracVec other) {
return row.equals(other.row) && col.equals(other.col);
}
public boolean equals(Point p) {
return row.equals(p.y) && col.equals(p.x);
}
@Override
public boolean equals(Object other) {
return other instanceof FracVec && equals((FracVec) other);
}
@Override
public int hashCode() {
return (31 + row.hashCode()) * 31 + col.hashCode();
}
public String toString() {
return "( " + row + ", " + col + " )";
}
public FracVec multiply(Frac scalar) {
return new FracVec(row.mult(scalar), col.mult(scalar));
}
public int compareLengthTo(int d) {
return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d));
}
public Point getNextCell(FracVec dm) {
int resRow, resCol;
if (dm.row.sgn() < 0) {
resRow = row.roundDownAddSmallDown();
} else {
resRow = row.roundDownAddSmallUp();
}
if (dm.col.sgn() < 0)
resCol = col.roundDownAddSmallDown();
else
resCol = col.roundDownAddSmallUp();
return new Point(resCol, resRow);
}
}
| import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Double.parseDouble;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collection.*;
public class D
{
static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); }
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int T = parseInt(br.readLine());
for(int t = 0; t++ < T; )
{
String[] line = br.readLine().split(" ");
int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]);
char[][] G = new char[H][];
for(int h = 0; h < H; h++)
G[h] = br.readLine().toCharArray();
int X = 0, Y = 0;
outer:for(Y = 0; Y < H; Y++)
for(X = 0; X < W; X++)
if(G[Y][X] == 'X')
break outer;
int count = 0;
for(int i = -D; i <= D; i++)
{
for(int j = -D; j <= D; j++)
{
int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y;
int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy));
if(gcd(Math.abs(dx), Math.abs(dy)) != 1)
continue;
x0 = x = X * scale + scale / 2;
y0 = y = Y * scale + scale / 2;
do
{
steps -= 1;
if(x % scale == 0 && y % scale == 0)
{
// at a corner
int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1;
int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2;
if(G[yi+dyi][xi+dxi] == '#')
{
if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#')
steps = -1; // kill the light
if(G[yi+dyi][xi] == '#')
dy *= -1;
if(G[yi][xi+dxi] == '#')
dx *= -1;
} else ;
// otherwise step as normal
} else if(x % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi][xi-1] == '#')
dx *= -1;
} else if(y % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi-1][xi] == '#')
dy *= -1;
} else ;
// smooth sailing
x += dx;
y += dy;
} while(steps >= 0 && !(x == x0 && y == y0));
if(steps >= 0)
++count;
}
}
out.println("Case #" + t +": " + count) ;
}
}
}
| 0 | 3,809 |
C20078 | C20056 | import java.awt.Point;
public class FracVec {
public final Frac row, col;
public FracVec(Frac row, Frac col) {
this.row = row;
this.col = col;
}
public FracVec(Point pos) {
row = new Frac(pos.y);
col = new Frac(pos.x);
}
public FracVec flipHoriz() {
return new FracVec(row, col.neg());
}
public FracVec flipVert() {
return new FracVec(row.neg(), col);
}
public boolean isCorner() {
return row.denom == 1 && col.denom == 1;
}
public FracVec add(FracVec other) {
return new FracVec(row.add(other.row), col.add(other.col));
}
public FracVec sub(FracVec other) {
return new FracVec(row.sub(other.row), col.sub(other.col));
}
public FracVec neg() {
return new FracVec(row.neg(), col.neg());
}
public boolean equals(FracVec other) {
return row.equals(other.row) && col.equals(other.col);
}
public boolean equals(Point p) {
return row.equals(p.y) && col.equals(p.x);
}
@Override
public boolean equals(Object other) {
return other instanceof FracVec && equals((FracVec) other);
}
@Override
public int hashCode() {
return (31 + row.hashCode()) * 31 + col.hashCode();
}
public String toString() {
return "( " + row + ", " + col + " )";
}
public FracVec multiply(Frac scalar) {
return new FracVec(row.mult(scalar), col.mult(scalar));
}
public int compareLengthTo(int d) {
return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d));
}
public Point getNextCell(FracVec dm) {
int resRow, resCol;
if (dm.row.sgn() < 0) {
resRow = row.roundDownAddSmallDown();
} else {
resRow = row.roundDownAddSmallUp();
}
if (dm.col.sgn() < 0)
resCol = col.roundDownAddSmallDown();
else
resCol = col.roundDownAddSmallUp();
return new Point(resCol, resRow);
}
}
| package jp.funnything.competition.util;
import java.math.BigDecimal;
/**
* Utility for BigDeciaml
*/
public class BD {
public static BigDecimal ZERO = BigDecimal.ZERO;
public static BigDecimal ONE = BigDecimal.ONE;
public static BigDecimal add( final BigDecimal x , final BigDecimal y ) {
return x.add( y );
}
public static BigDecimal add( final BigDecimal x , final double y ) {
return add( x , v( y ) );
}
public static BigDecimal add( final double x , final BigDecimal y ) {
return add( v( x ) , y );
}
public static int cmp( final BigDecimal x , final BigDecimal y ) {
return x.compareTo( y );
}
public static int cmp( final BigDecimal x , final double y ) {
return cmp( x , v( y ) );
}
public static int cmp( final double x , final BigDecimal y ) {
return cmp( v( x ) , y );
}
public static BigDecimal div( final BigDecimal x , final BigDecimal y ) {
return x.divide( y );
}
public static BigDecimal div( final BigDecimal x , final double y ) {
return div( x , v( y ) );
}
public static BigDecimal div( final double x , final BigDecimal y ) {
return div( v( x ) , y );
}
public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) {
return x.multiply( y );
}
public static BigDecimal mul( final BigDecimal x , final double y ) {
return mul( x , v( y ) );
}
public static BigDecimal mul( final double x , final BigDecimal y ) {
return mul( v( x ) , y );
}
public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) {
return x.subtract( y );
}
public static BigDecimal sub( final BigDecimal x , final double y ) {
return sub( x , v( y ) );
}
public static BigDecimal sub( final double x , final BigDecimal y ) {
return sub( v( x ) , y );
}
public static BigDecimal v( final double value ) {
return BigDecimal.valueOf( value );
}
}
| 0 | 3,810 |
C20078 | C20018 | import java.awt.Point;
public class FracVec {
public final Frac row, col;
public FracVec(Frac row, Frac col) {
this.row = row;
this.col = col;
}
public FracVec(Point pos) {
row = new Frac(pos.y);
col = new Frac(pos.x);
}
public FracVec flipHoriz() {
return new FracVec(row, col.neg());
}
public FracVec flipVert() {
return new FracVec(row.neg(), col);
}
public boolean isCorner() {
return row.denom == 1 && col.denom == 1;
}
public FracVec add(FracVec other) {
return new FracVec(row.add(other.row), col.add(other.col));
}
public FracVec sub(FracVec other) {
return new FracVec(row.sub(other.row), col.sub(other.col));
}
public FracVec neg() {
return new FracVec(row.neg(), col.neg());
}
public boolean equals(FracVec other) {
return row.equals(other.row) && col.equals(other.col);
}
public boolean equals(Point p) {
return row.equals(p.y) && col.equals(p.x);
}
@Override
public boolean equals(Object other) {
return other instanceof FracVec && equals((FracVec) other);
}
@Override
public int hashCode() {
return (31 + row.hashCode()) * 31 + col.hashCode();
}
public String toString() {
return "( " + row + ", " + col + " )";
}
public FracVec multiply(Frac scalar) {
return new FracVec(row.mult(scalar), col.mult(scalar));
}
public int compareLengthTo(int d) {
return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d));
}
public Point getNextCell(FracVec dm) {
int resRow, resCol;
if (dm.row.sgn() < 0) {
resRow = row.roundDownAddSmallDown();
} else {
resRow = row.roundDownAddSmallUp();
}
if (dm.col.sgn() < 0)
resCol = col.roundDownAddSmallDown();
else
resCol = col.roundDownAddSmallUp();
return new Point(resCol, resRow);
}
}
| package template;
import java.util.Objects;
public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{
private T1 o1;
private T2 o2;
public Pair(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Pair)) {return false;}
return (compareTo((Pair<T1, T2>)other) == 0);
}
@Override
public int compareTo(Pair<T1, T2> other) {
int c1 = ((Comparable<T1>) o1).compareTo(other.getO1());
if (c1 != 0) {return c1;}
int c2 = ((Comparable<T2>) o2).compareTo(other.getO2());
return c2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.o1);
hash = 83 * hash + Objects.hashCode(this.o2);
return hash;
}
@Override
public String toString()
{
return "[" + o1 + ", " + o2 + "]";
}
public T1 getO1() {
return o1;
}
public void setO1(T1 o1) {
this.o1 = o1;
}
public T2 getO2() {
return o2;
}
public void setO2(T2 o2) {
this.o2 = o2;
}
}
| 0 | 3,811 |
C20078 | C20051 | import java.awt.Point;
public class FracVec {
public final Frac row, col;
public FracVec(Frac row, Frac col) {
this.row = row;
this.col = col;
}
public FracVec(Point pos) {
row = new Frac(pos.y);
col = new Frac(pos.x);
}
public FracVec flipHoriz() {
return new FracVec(row, col.neg());
}
public FracVec flipVert() {
return new FracVec(row.neg(), col);
}
public boolean isCorner() {
return row.denom == 1 && col.denom == 1;
}
public FracVec add(FracVec other) {
return new FracVec(row.add(other.row), col.add(other.col));
}
public FracVec sub(FracVec other) {
return new FracVec(row.sub(other.row), col.sub(other.col));
}
public FracVec neg() {
return new FracVec(row.neg(), col.neg());
}
public boolean equals(FracVec other) {
return row.equals(other.row) && col.equals(other.col);
}
public boolean equals(Point p) {
return row.equals(p.y) && col.equals(p.x);
}
@Override
public boolean equals(Object other) {
return other instanceof FracVec && equals((FracVec) other);
}
@Override
public int hashCode() {
return (31 + row.hashCode()) * 31 + col.hashCode();
}
public String toString() {
return "( " + row + ", " + col + " )";
}
public FracVec multiply(Frac scalar) {
return new FracVec(row.mult(scalar), col.mult(scalar));
}
public int compareLengthTo(int d) {
return row.mult(row).add(col.mult(col)).compareTo(new Frac(d * d));
}
public Point getNextCell(FracVec dm) {
int resRow, resCol;
if (dm.row.sgn() < 0) {
resRow = row.roundDownAddSmallDown();
} else {
resRow = row.roundDownAddSmallUp();
}
if (dm.col.sgn() < 0)
resCol = col.roundDownAddSmallDown();
else
resCol = col.roundDownAddSmallUp();
return new Point(resCol, resRow);
}
}
| package jp.funnything.competition.util;
import java.util.List;
import com.google.common.collect.Lists;
public class Lists2 {
public static < T > List< T > newArrayListAsArray( final int length ) {
final List< T > list = Lists.newArrayListWithCapacity( length );
for ( int index = 0 ; index < length ; index++ ) {
list.add( null );
}
return list;
}
}
| 0 | 3,812 |
C20057 | C20030 | package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
public class Prime {
public static class PrimeData {
public int[] list;
public boolean[] map;
private PrimeData( final int[] values , final boolean[] map ) {
list = values;
this.map = map;
}
}
public static long[] factorize( long n , final int[] primes ) {
final List< Long > factor = Lists.newArrayList();
for ( final int p : primes ) {
if ( n < p * p ) {
break;
}
while ( n % p == 0 ) {
factor.add( ( long ) p );
n /= p;
}
}
if ( n > 1 ) {
factor.add( n );
}
return Longs.toArray( factor );
}
public static PrimeData prepare( final int n ) {
final List< Integer > primes = Lists.newArrayList();
final boolean[] map = new boolean[ n ];
Arrays.fill( map , true );
map[ 0 ] = map[ 1 ] = false;
primes.add( 2 );
for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) {
map[ composite ] = false;
}
for ( int value = 3 ; value < n ; value += 2 ) {
if ( map[ value ] ) {
primes.add( value );
for ( int composite = value * 2 ; composite < n ; composite += value ) {
map[ composite ] = false;
}
}
}
return new PrimeData( Ints.toArray( primes ) , map );
}
}
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class RayVector {
public final int x;
public final int y;
public RayVector(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof RayVector) {
RayVector v = (RayVector)obj;
if (v.x * y == v.y * x)
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.x;
hash = 41 * hash + this.y;
return hash;
}
public Fraction getScalarGardient() {
return new Fraction(Math.abs(y), Math.abs(x));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| 0 | 3,813 |
C20057 | C20002 | package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
public class Prime {
public static class PrimeData {
public int[] list;
public boolean[] map;
private PrimeData( final int[] values , final boolean[] map ) {
list = values;
this.map = map;
}
}
public static long[] factorize( long n , final int[] primes ) {
final List< Long > factor = Lists.newArrayList();
for ( final int p : primes ) {
if ( n < p * p ) {
break;
}
while ( n % p == 0 ) {
factor.add( ( long ) p );
n /= p;
}
}
if ( n > 1 ) {
factor.add( n );
}
return Longs.toArray( factor );
}
public static PrimeData prepare( final int n ) {
final List< Integer > primes = Lists.newArrayList();
final boolean[] map = new boolean[ n ];
Arrays.fill( map , true );
map[ 0 ] = map[ 1 ] = false;
primes.add( 2 );
for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) {
map[ composite ] = false;
}
for ( int value = 3 ; value < n ; value += 2 ) {
if ( map[ value ] ) {
primes.add( value );
for ( int composite = value * 2 ; composite < n ; composite += value ) {
map[ composite ] = false;
}
}
}
return new PrimeData( Ints.toArray( primes ) , map );
}
}
| import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Double.parseDouble;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collection.*;
public class D
{
static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); }
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int T = parseInt(br.readLine());
for(int t = 0; t++ < T; )
{
String[] line = br.readLine().split(" ");
int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]);
char[][] G = new char[H][];
for(int h = 0; h < H; h++)
G[h] = br.readLine().toCharArray();
int X = 0, Y = 0;
outer:for(Y = 0; Y < H; Y++)
for(X = 0; X < W; X++)
if(G[Y][X] == 'X')
break outer;
int count = 0;
for(int i = -D; i <= D; i++)
{
for(int j = -D; j <= D; j++)
{
int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y;
int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy));
if(gcd(Math.abs(dx), Math.abs(dy)) != 1)
continue;
x0 = x = X * scale + scale / 2;
y0 = y = Y * scale + scale / 2;
do
{
steps -= 1;
if(x % scale == 0 && y % scale == 0)
{
// at a corner
int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1;
int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2;
if(G[yi+dyi][xi+dxi] == '#')
{
if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#')
steps = -1; // kill the light
if(G[yi+dyi][xi] == '#')
dy *= -1;
if(G[yi][xi+dxi] == '#')
dx *= -1;
} else ;
// otherwise step as normal
} else if(x % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi][xi-1] == '#')
dx *= -1;
} else if(y % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi-1][xi] == '#')
dy *= -1;
} else ;
// smooth sailing
x += dx;
y += dy;
} while(steps >= 0 && !(x == x0 && y == y0));
if(steps >= 0)
++count;
}
}
out.println("Case #" + t +": " + count) ;
}
}
}
| 0 | 3,814 |
C20057 | C20056 | package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
public class Prime {
public static class PrimeData {
public int[] list;
public boolean[] map;
private PrimeData( final int[] values , final boolean[] map ) {
list = values;
this.map = map;
}
}
public static long[] factorize( long n , final int[] primes ) {
final List< Long > factor = Lists.newArrayList();
for ( final int p : primes ) {
if ( n < p * p ) {
break;
}
while ( n % p == 0 ) {
factor.add( ( long ) p );
n /= p;
}
}
if ( n > 1 ) {
factor.add( n );
}
return Longs.toArray( factor );
}
public static PrimeData prepare( final int n ) {
final List< Integer > primes = Lists.newArrayList();
final boolean[] map = new boolean[ n ];
Arrays.fill( map , true );
map[ 0 ] = map[ 1 ] = false;
primes.add( 2 );
for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) {
map[ composite ] = false;
}
for ( int value = 3 ; value < n ; value += 2 ) {
if ( map[ value ] ) {
primes.add( value );
for ( int composite = value * 2 ; composite < n ; composite += value ) {
map[ composite ] = false;
}
}
}
return new PrimeData( Ints.toArray( primes ) , map );
}
}
| package jp.funnything.competition.util;
import java.math.BigDecimal;
/**
* Utility for BigDeciaml
*/
public class BD {
public static BigDecimal ZERO = BigDecimal.ZERO;
public static BigDecimal ONE = BigDecimal.ONE;
public static BigDecimal add( final BigDecimal x , final BigDecimal y ) {
return x.add( y );
}
public static BigDecimal add( final BigDecimal x , final double y ) {
return add( x , v( y ) );
}
public static BigDecimal add( final double x , final BigDecimal y ) {
return add( v( x ) , y );
}
public static int cmp( final BigDecimal x , final BigDecimal y ) {
return x.compareTo( y );
}
public static int cmp( final BigDecimal x , final double y ) {
return cmp( x , v( y ) );
}
public static int cmp( final double x , final BigDecimal y ) {
return cmp( v( x ) , y );
}
public static BigDecimal div( final BigDecimal x , final BigDecimal y ) {
return x.divide( y );
}
public static BigDecimal div( final BigDecimal x , final double y ) {
return div( x , v( y ) );
}
public static BigDecimal div( final double x , final BigDecimal y ) {
return div( v( x ) , y );
}
public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) {
return x.multiply( y );
}
public static BigDecimal mul( final BigDecimal x , final double y ) {
return mul( x , v( y ) );
}
public static BigDecimal mul( final double x , final BigDecimal y ) {
return mul( v( x ) , y );
}
public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) {
return x.subtract( y );
}
public static BigDecimal sub( final BigDecimal x , final double y ) {
return sub( x , v( y ) );
}
public static BigDecimal sub( final double x , final BigDecimal y ) {
return sub( v( x ) , y );
}
public static BigDecimal v( final double value ) {
return BigDecimal.valueOf( value );
}
}
| 0 | 3,815 |
C20057 | C20018 | package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
public class Prime {
public static class PrimeData {
public int[] list;
public boolean[] map;
private PrimeData( final int[] values , final boolean[] map ) {
list = values;
this.map = map;
}
}
public static long[] factorize( long n , final int[] primes ) {
final List< Long > factor = Lists.newArrayList();
for ( final int p : primes ) {
if ( n < p * p ) {
break;
}
while ( n % p == 0 ) {
factor.add( ( long ) p );
n /= p;
}
}
if ( n > 1 ) {
factor.add( n );
}
return Longs.toArray( factor );
}
public static PrimeData prepare( final int n ) {
final List< Integer > primes = Lists.newArrayList();
final boolean[] map = new boolean[ n ];
Arrays.fill( map , true );
map[ 0 ] = map[ 1 ] = false;
primes.add( 2 );
for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) {
map[ composite ] = false;
}
for ( int value = 3 ; value < n ; value += 2 ) {
if ( map[ value ] ) {
primes.add( value );
for ( int composite = value * 2 ; composite < n ; composite += value ) {
map[ composite ] = false;
}
}
}
return new PrimeData( Ints.toArray( primes ) , map );
}
}
| package template;
import java.util.Objects;
public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{
private T1 o1;
private T2 o2;
public Pair(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Pair)) {return false;}
return (compareTo((Pair<T1, T2>)other) == 0);
}
@Override
public int compareTo(Pair<T1, T2> other) {
int c1 = ((Comparable<T1>) o1).compareTo(other.getO1());
if (c1 != 0) {return c1;}
int c2 = ((Comparable<T2>) o2).compareTo(other.getO2());
return c2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.o1);
hash = 83 * hash + Objects.hashCode(this.o2);
return hash;
}
@Override
public String toString()
{
return "[" + o1 + ", " + o2 + "]";
}
public T1 getO1() {
return o1;
}
public void setO1(T1 o1) {
this.o1 = o1;
}
public T2 getO2() {
return o2;
}
public void setO2(T2 o2) {
this.o2 = o2;
}
}
| 0 | 3,816 |
C20057 | C20051 | package jp.funnything.competition.util;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
public class Prime {
public static class PrimeData {
public int[] list;
public boolean[] map;
private PrimeData( final int[] values , final boolean[] map ) {
list = values;
this.map = map;
}
}
public static long[] factorize( long n , final int[] primes ) {
final List< Long > factor = Lists.newArrayList();
for ( final int p : primes ) {
if ( n < p * p ) {
break;
}
while ( n % p == 0 ) {
factor.add( ( long ) p );
n /= p;
}
}
if ( n > 1 ) {
factor.add( n );
}
return Longs.toArray( factor );
}
public static PrimeData prepare( final int n ) {
final List< Integer > primes = Lists.newArrayList();
final boolean[] map = new boolean[ n ];
Arrays.fill( map , true );
map[ 0 ] = map[ 1 ] = false;
primes.add( 2 );
for ( int composite = 2 * 2 ; composite < n ; composite += 2 ) {
map[ composite ] = false;
}
for ( int value = 3 ; value < n ; value += 2 ) {
if ( map[ value ] ) {
primes.add( value );
for ( int composite = value * 2 ; composite < n ; composite += value ) {
map[ composite ] = false;
}
}
}
return new PrimeData( Ints.toArray( primes ) , map );
}
}
| package jp.funnything.competition.util;
import java.util.List;
import com.google.common.collect.Lists;
public class Lists2 {
public static < T > List< T > newArrayListAsArray( final int length ) {
final List< T > list = Lists.newArrayListWithCapacity( length );
for ( int index = 0 ; index < length ; index++ ) {
list.add( null );
}
return list;
}
}
| 0 | 3,817 |
C20030 | C20002 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class RayVector {
public final int x;
public final int y;
public RayVector(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof RayVector) {
RayVector v = (RayVector)obj;
if (v.x * y == v.y * x)
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.x;
hash = 41 * hash + this.y;
return hash;
}
public Fraction getScalarGardient() {
return new Fraction(Math.abs(y), Math.abs(x));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Double.parseDouble;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collection.*;
public class D
{
static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); }
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int T = parseInt(br.readLine());
for(int t = 0; t++ < T; )
{
String[] line = br.readLine().split(" ");
int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]);
char[][] G = new char[H][];
for(int h = 0; h < H; h++)
G[h] = br.readLine().toCharArray();
int X = 0, Y = 0;
outer:for(Y = 0; Y < H; Y++)
for(X = 0; X < W; X++)
if(G[Y][X] == 'X')
break outer;
int count = 0;
for(int i = -D; i <= D; i++)
{
for(int j = -D; j <= D; j++)
{
int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y;
int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy));
if(gcd(Math.abs(dx), Math.abs(dy)) != 1)
continue;
x0 = x = X * scale + scale / 2;
y0 = y = Y * scale + scale / 2;
do
{
steps -= 1;
if(x % scale == 0 && y % scale == 0)
{
// at a corner
int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1;
int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2;
if(G[yi+dyi][xi+dxi] == '#')
{
if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#')
steps = -1; // kill the light
if(G[yi+dyi][xi] == '#')
dy *= -1;
if(G[yi][xi+dxi] == '#')
dx *= -1;
} else ;
// otherwise step as normal
} else if(x % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi][xi-1] == '#')
dx *= -1;
} else if(y % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi-1][xi] == '#')
dy *= -1;
} else ;
// smooth sailing
x += dx;
y += dy;
} while(steps >= 0 && !(x == x0 && y == y0));
if(steps >= 0)
++count;
}
}
out.println("Case #" + t +": " + count) ;
}
}
}
| 0 | 3,818 |
C20030 | C20056 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class RayVector {
public final int x;
public final int y;
public RayVector(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof RayVector) {
RayVector v = (RayVector)obj;
if (v.x * y == v.y * x)
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.x;
hash = 41 * hash + this.y;
return hash;
}
public Fraction getScalarGardient() {
return new Fraction(Math.abs(y), Math.abs(x));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| package jp.funnything.competition.util;
import java.math.BigDecimal;
/**
* Utility for BigDeciaml
*/
public class BD {
public static BigDecimal ZERO = BigDecimal.ZERO;
public static BigDecimal ONE = BigDecimal.ONE;
public static BigDecimal add( final BigDecimal x , final BigDecimal y ) {
return x.add( y );
}
public static BigDecimal add( final BigDecimal x , final double y ) {
return add( x , v( y ) );
}
public static BigDecimal add( final double x , final BigDecimal y ) {
return add( v( x ) , y );
}
public static int cmp( final BigDecimal x , final BigDecimal y ) {
return x.compareTo( y );
}
public static int cmp( final BigDecimal x , final double y ) {
return cmp( x , v( y ) );
}
public static int cmp( final double x , final BigDecimal y ) {
return cmp( v( x ) , y );
}
public static BigDecimal div( final BigDecimal x , final BigDecimal y ) {
return x.divide( y );
}
public static BigDecimal div( final BigDecimal x , final double y ) {
return div( x , v( y ) );
}
public static BigDecimal div( final double x , final BigDecimal y ) {
return div( v( x ) , y );
}
public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) {
return x.multiply( y );
}
public static BigDecimal mul( final BigDecimal x , final double y ) {
return mul( x , v( y ) );
}
public static BigDecimal mul( final double x , final BigDecimal y ) {
return mul( v( x ) , y );
}
public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) {
return x.subtract( y );
}
public static BigDecimal sub( final BigDecimal x , final double y ) {
return sub( x , v( y ) );
}
public static BigDecimal sub( final double x , final BigDecimal y ) {
return sub( v( x ) , y );
}
public static BigDecimal v( final double value ) {
return BigDecimal.valueOf( value );
}
}
| 0 | 3,819 |
C20030 | C20018 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class RayVector {
public final int x;
public final int y;
public RayVector(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof RayVector) {
RayVector v = (RayVector)obj;
if (v.x * y == v.y * x)
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.x;
hash = 41 * hash + this.y;
return hash;
}
public Fraction getScalarGardient() {
return new Fraction(Math.abs(y), Math.abs(x));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| package template;
import java.util.Objects;
public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{
private T1 o1;
private T2 o2;
public Pair(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Pair)) {return false;}
return (compareTo((Pair<T1, T2>)other) == 0);
}
@Override
public int compareTo(Pair<T1, T2> other) {
int c1 = ((Comparable<T1>) o1).compareTo(other.getO1());
if (c1 != 0) {return c1;}
int c2 = ((Comparable<T2>) o2).compareTo(other.getO2());
return c2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.o1);
hash = 83 * hash + Objects.hashCode(this.o2);
return hash;
}
@Override
public String toString()
{
return "[" + o1 + ", " + o2 + "]";
}
public T1 getO1() {
return o1;
}
public void setO1(T1 o1) {
this.o1 = o1;
}
public T2 getO2() {
return o2;
}
public void setO2(T2 o2) {
this.o2 = o2;
}
}
| 0 | 3,820 |
C20030 | C20051 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uk.co.epii.codejam.hallofmirrors;
/**
*
* @author jim
*/
public class RayVector {
public final int x;
public final int y;
public RayVector(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof RayVector) {
RayVector v = (RayVector)obj;
if (v.x * y == v.y * x)
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + this.x;
hash = 41 * hash + this.y;
return hash;
}
public Fraction getScalarGardient() {
return new Fraction(Math.abs(y), Math.abs(x));
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(x);
sb.append(",");
sb.append(y);
sb.append(")");
return sb.toString();
}
}
| package jp.funnything.competition.util;
import java.util.List;
import com.google.common.collect.Lists;
public class Lists2 {
public static < T > List< T > newArrayListAsArray( final int length ) {
final List< T > list = Lists.newArrayListWithCapacity( length );
for ( int index = 0 ; index < length ; index++ ) {
list.add( null );
}
return list;
}
}
| 0 | 3,821 |
C20002 | C20056 | import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Double.parseDouble;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collection.*;
public class D
{
static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); }
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int T = parseInt(br.readLine());
for(int t = 0; t++ < T; )
{
String[] line = br.readLine().split(" ");
int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]);
char[][] G = new char[H][];
for(int h = 0; h < H; h++)
G[h] = br.readLine().toCharArray();
int X = 0, Y = 0;
outer:for(Y = 0; Y < H; Y++)
for(X = 0; X < W; X++)
if(G[Y][X] == 'X')
break outer;
int count = 0;
for(int i = -D; i <= D; i++)
{
for(int j = -D; j <= D; j++)
{
int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y;
int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy));
if(gcd(Math.abs(dx), Math.abs(dy)) != 1)
continue;
x0 = x = X * scale + scale / 2;
y0 = y = Y * scale + scale / 2;
do
{
steps -= 1;
if(x % scale == 0 && y % scale == 0)
{
// at a corner
int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1;
int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2;
if(G[yi+dyi][xi+dxi] == '#')
{
if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#')
steps = -1; // kill the light
if(G[yi+dyi][xi] == '#')
dy *= -1;
if(G[yi][xi+dxi] == '#')
dx *= -1;
} else ;
// otherwise step as normal
} else if(x % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi][xi-1] == '#')
dx *= -1;
} else if(y % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi-1][xi] == '#')
dy *= -1;
} else ;
// smooth sailing
x += dx;
y += dy;
} while(steps >= 0 && !(x == x0 && y == y0));
if(steps >= 0)
++count;
}
}
out.println("Case #" + t +": " + count) ;
}
}
}
| package jp.funnything.competition.util;
import java.math.BigDecimal;
/**
* Utility for BigDeciaml
*/
public class BD {
public static BigDecimal ZERO = BigDecimal.ZERO;
public static BigDecimal ONE = BigDecimal.ONE;
public static BigDecimal add( final BigDecimal x , final BigDecimal y ) {
return x.add( y );
}
public static BigDecimal add( final BigDecimal x , final double y ) {
return add( x , v( y ) );
}
public static BigDecimal add( final double x , final BigDecimal y ) {
return add( v( x ) , y );
}
public static int cmp( final BigDecimal x , final BigDecimal y ) {
return x.compareTo( y );
}
public static int cmp( final BigDecimal x , final double y ) {
return cmp( x , v( y ) );
}
public static int cmp( final double x , final BigDecimal y ) {
return cmp( v( x ) , y );
}
public static BigDecimal div( final BigDecimal x , final BigDecimal y ) {
return x.divide( y );
}
public static BigDecimal div( final BigDecimal x , final double y ) {
return div( x , v( y ) );
}
public static BigDecimal div( final double x , final BigDecimal y ) {
return div( v( x ) , y );
}
public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) {
return x.multiply( y );
}
public static BigDecimal mul( final BigDecimal x , final double y ) {
return mul( x , v( y ) );
}
public static BigDecimal mul( final double x , final BigDecimal y ) {
return mul( v( x ) , y );
}
public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) {
return x.subtract( y );
}
public static BigDecimal sub( final BigDecimal x , final double y ) {
return sub( x , v( y ) );
}
public static BigDecimal sub( final double x , final BigDecimal y ) {
return sub( v( x ) , y );
}
public static BigDecimal v( final double value ) {
return BigDecimal.valueOf( value );
}
}
| 0 | 3,822 |
C20002 | C20018 | import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Double.parseDouble;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collection.*;
public class D
{
static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); }
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int T = parseInt(br.readLine());
for(int t = 0; t++ < T; )
{
String[] line = br.readLine().split(" ");
int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]);
char[][] G = new char[H][];
for(int h = 0; h < H; h++)
G[h] = br.readLine().toCharArray();
int X = 0, Y = 0;
outer:for(Y = 0; Y < H; Y++)
for(X = 0; X < W; X++)
if(G[Y][X] == 'X')
break outer;
int count = 0;
for(int i = -D; i <= D; i++)
{
for(int j = -D; j <= D; j++)
{
int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y;
int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy));
if(gcd(Math.abs(dx), Math.abs(dy)) != 1)
continue;
x0 = x = X * scale + scale / 2;
y0 = y = Y * scale + scale / 2;
do
{
steps -= 1;
if(x % scale == 0 && y % scale == 0)
{
// at a corner
int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1;
int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2;
if(G[yi+dyi][xi+dxi] == '#')
{
if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#')
steps = -1; // kill the light
if(G[yi+dyi][xi] == '#')
dy *= -1;
if(G[yi][xi+dxi] == '#')
dx *= -1;
} else ;
// otherwise step as normal
} else if(x % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi][xi-1] == '#')
dx *= -1;
} else if(y % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi-1][xi] == '#')
dy *= -1;
} else ;
// smooth sailing
x += dx;
y += dy;
} while(steps >= 0 && !(x == x0 && y == y0));
if(steps >= 0)
++count;
}
}
out.println("Case #" + t +": " + count) ;
}
}
}
| package template;
import java.util.Objects;
public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{
private T1 o1;
private T2 o2;
public Pair(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Pair)) {return false;}
return (compareTo((Pair<T1, T2>)other) == 0);
}
@Override
public int compareTo(Pair<T1, T2> other) {
int c1 = ((Comparable<T1>) o1).compareTo(other.getO1());
if (c1 != 0) {return c1;}
int c2 = ((Comparable<T2>) o2).compareTo(other.getO2());
return c2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.o1);
hash = 83 * hash + Objects.hashCode(this.o2);
return hash;
}
@Override
public String toString()
{
return "[" + o1 + ", " + o2 + "]";
}
public T1 getO1() {
return o1;
}
public void setO1(T1 o1) {
this.o1 = o1;
}
public T2 getO2() {
return o2;
}
public void setO2(T2 o2) {
this.o2 = o2;
}
}
| 0 | 3,823 |
C20002 | C20051 | import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Double.parseDouble;
import static java.lang.Long.parseLong;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collection.*;
public class D
{
static int gcd(int a, int b) { return b == 0 ? a : a == 0 ? b : gcd(b, a%b); }
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int T = parseInt(br.readLine());
for(int t = 0; t++ < T; )
{
String[] line = br.readLine().split(" ");
int H = parseInt(line[0]), W = parseInt(line[1]), D = parseInt(line[2]);
char[][] G = new char[H][];
for(int h = 0; h < H; h++)
G[h] = br.readLine().toCharArray();
int X = 0, Y = 0;
outer:for(Y = 0; Y < H; Y++)
for(X = 0; X < W; X++)
if(G[Y][X] == 'X')
break outer;
int count = 0;
for(int i = -D; i <= D; i++)
{
for(int j = -D; j <= D; j++)
{
int dx = i, dy = j, scale = 2 * Math.abs((dx == 0 ? 1 : dx) * (dy == 0 ? 1 : dy)), x0, y0, x, y;
int steps = (int)Math.floor(scale * D / Math.sqrt(dx * dx + dy * dy));
if(gcd(Math.abs(dx), Math.abs(dy)) != 1)
continue;
x0 = x = X * scale + scale / 2;
y0 = y = Y * scale + scale / 2;
do
{
steps -= 1;
if(x % scale == 0 && y % scale == 0)
{
// at a corner
int dxi = dx > 0 ? 1 : -1, dyi = dy > 0 ? 1 : -1;
int xi = (x / scale) - (dxi + 1) / 2, yi = (y / scale) - (dyi + 1) / 2;
if(G[yi+dyi][xi+dxi] == '#')
{
if(G[yi+dyi][xi] != '#' && G[yi][xi+dxi] != '#')
steps = -1; // kill the light
if(G[yi+dyi][xi] == '#')
dy *= -1;
if(G[yi][xi+dxi] == '#')
dx *= -1;
} else ;
// otherwise step as normal
} else if(x % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi][xi-1] == '#')
dx *= -1;
} else if(y % scale == 0) {
int xi = x / scale, yi = y / scale;
if(G[yi][xi] == '#' || G[yi-1][xi] == '#')
dy *= -1;
} else ;
// smooth sailing
x += dx;
y += dy;
} while(steps >= 0 && !(x == x0 && y == y0));
if(steps >= 0)
++count;
}
}
out.println("Case #" + t +": " + count) ;
}
}
}
| package jp.funnything.competition.util;
import java.util.List;
import com.google.common.collect.Lists;
public class Lists2 {
public static < T > List< T > newArrayListAsArray( final int length ) {
final List< T > list = Lists.newArrayListWithCapacity( length );
for ( int index = 0 ; index < length ; index++ ) {
list.add( null );
}
return list;
}
}
| 0 | 3,824 |
C20056 | C20018 | package jp.funnything.competition.util;
import java.math.BigDecimal;
/**
* Utility for BigDeciaml
*/
public class BD {
public static BigDecimal ZERO = BigDecimal.ZERO;
public static BigDecimal ONE = BigDecimal.ONE;
public static BigDecimal add( final BigDecimal x , final BigDecimal y ) {
return x.add( y );
}
public static BigDecimal add( final BigDecimal x , final double y ) {
return add( x , v( y ) );
}
public static BigDecimal add( final double x , final BigDecimal y ) {
return add( v( x ) , y );
}
public static int cmp( final BigDecimal x , final BigDecimal y ) {
return x.compareTo( y );
}
public static int cmp( final BigDecimal x , final double y ) {
return cmp( x , v( y ) );
}
public static int cmp( final double x , final BigDecimal y ) {
return cmp( v( x ) , y );
}
public static BigDecimal div( final BigDecimal x , final BigDecimal y ) {
return x.divide( y );
}
public static BigDecimal div( final BigDecimal x , final double y ) {
return div( x , v( y ) );
}
public static BigDecimal div( final double x , final BigDecimal y ) {
return div( v( x ) , y );
}
public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) {
return x.multiply( y );
}
public static BigDecimal mul( final BigDecimal x , final double y ) {
return mul( x , v( y ) );
}
public static BigDecimal mul( final double x , final BigDecimal y ) {
return mul( v( x ) , y );
}
public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) {
return x.subtract( y );
}
public static BigDecimal sub( final BigDecimal x , final double y ) {
return sub( x , v( y ) );
}
public static BigDecimal sub( final double x , final BigDecimal y ) {
return sub( v( x ) , y );
}
public static BigDecimal v( final double value ) {
return BigDecimal.valueOf( value );
}
}
| package template;
import java.util.Objects;
public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{
private T1 o1;
private T2 o2;
public Pair(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Pair)) {return false;}
return (compareTo((Pair<T1, T2>)other) == 0);
}
@Override
public int compareTo(Pair<T1, T2> other) {
int c1 = ((Comparable<T1>) o1).compareTo(other.getO1());
if (c1 != 0) {return c1;}
int c2 = ((Comparable<T2>) o2).compareTo(other.getO2());
return c2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.o1);
hash = 83 * hash + Objects.hashCode(this.o2);
return hash;
}
@Override
public String toString()
{
return "[" + o1 + ", " + o2 + "]";
}
public T1 getO1() {
return o1;
}
public void setO1(T1 o1) {
this.o1 = o1;
}
public T2 getO2() {
return o2;
}
public void setO2(T2 o2) {
this.o2 = o2;
}
}
| 0 | 3,825 |
C20056 | C20051 | package jp.funnything.competition.util;
import java.math.BigDecimal;
/**
* Utility for BigDeciaml
*/
public class BD {
public static BigDecimal ZERO = BigDecimal.ZERO;
public static BigDecimal ONE = BigDecimal.ONE;
public static BigDecimal add( final BigDecimal x , final BigDecimal y ) {
return x.add( y );
}
public static BigDecimal add( final BigDecimal x , final double y ) {
return add( x , v( y ) );
}
public static BigDecimal add( final double x , final BigDecimal y ) {
return add( v( x ) , y );
}
public static int cmp( final BigDecimal x , final BigDecimal y ) {
return x.compareTo( y );
}
public static int cmp( final BigDecimal x , final double y ) {
return cmp( x , v( y ) );
}
public static int cmp( final double x , final BigDecimal y ) {
return cmp( v( x ) , y );
}
public static BigDecimal div( final BigDecimal x , final BigDecimal y ) {
return x.divide( y );
}
public static BigDecimal div( final BigDecimal x , final double y ) {
return div( x , v( y ) );
}
public static BigDecimal div( final double x , final BigDecimal y ) {
return div( v( x ) , y );
}
public static BigDecimal mul( final BigDecimal x , final BigDecimal y ) {
return x.multiply( y );
}
public static BigDecimal mul( final BigDecimal x , final double y ) {
return mul( x , v( y ) );
}
public static BigDecimal mul( final double x , final BigDecimal y ) {
return mul( v( x ) , y );
}
public static BigDecimal sub( final BigDecimal x , final BigDecimal y ) {
return x.subtract( y );
}
public static BigDecimal sub( final BigDecimal x , final double y ) {
return sub( x , v( y ) );
}
public static BigDecimal sub( final double x , final BigDecimal y ) {
return sub( v( x ) , y );
}
public static BigDecimal v( final double value ) {
return BigDecimal.valueOf( value );
}
}
| package jp.funnything.competition.util;
import java.util.List;
import com.google.common.collect.Lists;
public class Lists2 {
public static < T > List< T > newArrayListAsArray( final int length ) {
final List< T > list = Lists.newArrayListWithCapacity( length );
for ( int index = 0 ; index < length ; index++ ) {
list.add( null );
}
return list;
}
}
| 0 | 3,826 |
C20018 | C20051 | package template;
import java.util.Objects;
public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>{
private T1 o1;
private T2 o2;
public Pair(T1 o1, T2 o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Pair)) {return false;}
return (compareTo((Pair<T1, T2>)other) == 0);
}
@Override
public int compareTo(Pair<T1, T2> other) {
int c1 = ((Comparable<T1>) o1).compareTo(other.getO1());
if (c1 != 0) {return c1;}
int c2 = ((Comparable<T2>) o2).compareTo(other.getO2());
return c2;
}
@Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + Objects.hashCode(this.o1);
hash = 83 * hash + Objects.hashCode(this.o2);
return hash;
}
@Override
public String toString()
{
return "[" + o1 + ", " + o2 + "]";
}
public T1 getO1() {
return o1;
}
public void setO1(T1 o1) {
this.o1 = o1;
}
public T2 getO2() {
return o2;
}
public void setO2(T2 o2) {
this.o2 = o2;
}
}
| package jp.funnything.competition.util;
import java.util.List;
import com.google.common.collect.Lists;
public class Lists2 {
public static < T > List< T > newArrayListAsArray( final int length ) {
final List< T > list = Lists.newArrayListWithCapacity( length );
for ( int index = 0 ; index < length ; index++ ) {
list.add( null );
}
return list;
}
}
| 0 | 3,827 |
Subsets and Splits