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
C20063
C20026
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
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);}}} }
0
3,700
C20063
C20078
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
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,701
C20063
C20057
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
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,702
C20063
C20030
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
/* * 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,703
C20063
C20002
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
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,704
C20063
C20056
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
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,705
C20063
C20018
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
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,706
C20063
C20051
package jp.funnything.competition.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class AnswerWriter { private static final String DEFAULT_FORMAT = "Case #%d: %s\n"; private final BufferedWriter _writer; private final String _format; public AnswerWriter( final File output , final String format ) { try { _writer = new BufferedWriter( new FileWriter( output ) ); _format = format != null ? format : DEFAULT_FORMAT; } catch ( final IOException e ) { throw new RuntimeException( e ); } } public void close() { IOUtils.closeQuietly( _writer ); } public void write( final int questionNumber , final Object result ) { write( questionNumber , result.toString() , true ); } public void write( final int questionNumber , final String result ) { write( questionNumber , result , true ); } public void write( final int questionNumber , final String result , final boolean tee ) { try { final String content = String.format( _format , questionNumber , result ); if ( tee ) { System.out.print( content ); System.out.flush(); } _writer.write( content ); } catch ( final IOException e ) { throw new RuntimeException( e ); } } }
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,707
C20015
C20050
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,708
C20015
C20085
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
0
3,709
C20015
C20020
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
0
3,710
C20015
C20062
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,711
C20015
C20059
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,712
C20015
C20064
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,713
C20015
C20076
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,714
C20015
C20026
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
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);}}} }
0
3,715
C20015
C20078
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
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,716
C20015
C20057
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
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,717
C20015
C20030
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
/* * 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,718
C20015
C20002
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
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,719
C20015
C20056
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
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,720
C20015
C20018
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
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,721
C20015
C20051
package qualification; import java.io.*; import java.util.Scanner; /** * @author Roman Elizarov */ public class D { public static void main(String[] args) throws IOException { new D().go(); } Scanner in; PrintWriter out; private void go() throws IOException { in = new Scanner(new File("src\\qualification\\d.in")); out = new PrintWriter(new File("src\\qualification\\d.out")); int t = in.nextInt(); for (int tn = 1; tn <= t; tn++) { System.out.println("Case #" + tn); out.println("Case #" + tn + ": " + solveCase()); } in.close(); out.close(); } int h; int w; int d; char[][] c; int a00 = 1; int a01; int a10; int a11 = 1; int b0; int b1; char get(int x, int y) { return c[a00 * x + a01 * y + b0][a10 * x + a11 * y + b1]; } void printC() { System.out.println("--- C ---"); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) System.out.print(c[i][j]); System.out.println(); } } void printV(String hdr) { System.out.println("--- " + hdr + " ---"); for (int y = 3; y >= -4; y--) { System.out.print(y == 0 ? "_" : " "); for (int x = 0; x <= 5; x++) try { System.out.print(get(x, y)); } catch (ArrayIndexOutOfBoundsException e) { System.out.print('?'); } System.out.println(); } } // Rotate 90 deg ccw around point (0.5, 0.5) void rotateCCW() { int d00 = -a01; int d01 = a00; int d10 = -a11; int d11 = a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Rotate 90 deg cw around point (0.5, 0.5) void rotateCW() { int d00 = a01; int d01 = -a00; int d10 = a11; int d11 = -a10; a00 = d00; a01 = d01; a10 = d10; a11 = d11; } // Mirror around x = p void mirrorX(int p) { b0 += a00 * (2 * p - 1); b1 += a10 * (2 * p - 1); a00 = -a00; a10 = -a10; } // Mirror around y = q void mirrorY(int q) { b0 += a01 * (2 * q - 1); b1 += a11 * (2 * q - 1); a01 = -a01; a11 = -a11; } // Mirror around y = 0.5 void mirrorYC() { a01 = -a01; a11 = -a11; } private int solveCase() { h = in.nextInt(); w = in.nextInt(); d = in.nextInt(); c = new char[h][]; for (int i = 0; i < h; i++) { c[i] = in.next().toCharArray(); assert c[i].length == w; } printC(); find: for (b0 = 0; b0 < h; b0++) for (b1 = 0; b1 < w; b1++) if (c[b0][b1] == 'X') break find; int cnt = 0; for (int i = 0; i < 4; i++) { cnt += solveRay(1, 1); cnt += solveRangeX(1, 1, -1, 1, 1); rotateCCW(); } return cnt; } // (0.5, 0.5) -> (x, y) int solveRay(int x, int y) { int cnt = 0; if (y <= 0) { mirrorYC(); cnt = solveRay(x, -y + 1); mirrorYC(); return cnt; } if (!possible(x, y)) return 0; assert x > 0; switch (get(x, y)) { case '#': char c1 = get(x - 1, y); char c2 = get(x, y - 1); if (c1 == '#' && c2 == '#') { // reflected straight back if (good2(x, y)) cnt++; } else if (c1 == '#') { mirrorY(y); cnt = solveRay(x, y); mirrorY(y); } else if (c2 == '#') { mirrorX(x); cnt = solveRay(x, y); mirrorX(x); } // otherwise -> destroyed break; case 'X': if (x == y) { if (good(x, y)) cnt++; break; } // fall-through case '.': if (x < y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x, y + 1); else if (x > y) cnt = solveRay2(2 * x - 1, 2 * y - 1, x + 1, y); else // x == y cnt = solveRay(x + 1, y + 1); break; default: assert false; } return cnt; } int solveRay2(int x0, int y0, int x, int y) { if (!possible(x, y)) return 0; int cnt = 0; int ccw; switch (get(x, y)) { case '#': ccw = ccw(x0, y0, 2 * x - 1, 2 * y - 1); if (ccw > 0) { mirrorY(y); cnt = solveRay2(x0, y0, x, y); mirrorY(y); } else if (ccw < 0) { mirrorX(x); cnt = solveRay2(x0, y0, x, y); mirrorX(x); } else cnt = solveRay(x, y); // hit corner break; case 'X': if (ccw(x0, y0, x, y) == 0) { if (good(x, y)) cnt++; break; } case '.': ccw = ccw(x0, y0, 2 * x + 1, 2 * y + 1); if (ccw > 0) cnt = solveRay2(x0, y0, x + 1, y); else if (ccw < 0) cnt = solveRay2(x0, y0, x, y + 1); else cnt = solveRay(x + 1, y + 1); // hit corner break; default: assert false; } return cnt; } // (0.5, 0.5) -> (p, y') between (x0, y0) and (x1, y1) vectors int solveRangeX(int p, int x0, int y0, int x1, int y1) { //printV("solveRangeX(" + p + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); assert ccw(x0, y0, x1, y1) > 0; if (p > d) return 0; int q = projectRay(p, x0, y0); assert ccw(2 * p - 1, 2 * q - 1, x0, y0) >= 0; assert ccw(x1, y1, 2 * p - 1, 2 * q + 1) >= 0; int cnt = 0; switch (get(p, q)) { case '#': mirrorX(p); cnt += solveRangeX(p, x0, y0, x1, y1); mirrorX(p); break; case 'X': if (ccw(x0, y0, p, q) > 0 && ccw(p, q, x1, y1) > 0) { if (good(p, q)) cnt++; cnt += solveRangeX(p, x0, y0, p, q); cnt += solveRangeX(p, p, q, x1, y1); break; } // fall-through case '.': if (q <= 0 && ccw(x0, y0, 2 * p + 1, 2 * q - 1) > 0) { if (ccw(2 * p + 1, 2 * q - 1, x1, y1) > 0) { cnt += solveRangeY(q, x0, y0, 2 * p + 1, 2 * q - 1); cnt += solveRay(p + 1, q); x0 = 2 * p + 1; y0 = 2 * q - 1; } else { cnt += solveRangeY(q, x0, y0, x1, y1); break; } } if (q >= 0 && ccw(2 * p + 1, 2 * q + 1, x1, y1) > 0) { if (ccw(x0, y0, 2 * p + 1, 2 * q + 1) > 0) { cnt += solveRangeY(q + 1, 2 * p + 1, 2 * q + 1, x1, y1); cnt += solveRay(p + 1, q + 1); x1 = 2 * p + 1; y1 = 2 * q + 1; } else { cnt += solveRangeY(q + 1, x0, y0, x1, y1); break; } } cnt += solveRangeX(p + 1, x0, y0, x1, y1); break; default: assert false; } return cnt; } private boolean possible(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(2 * d); } private boolean good(int x, int y) { return sqr(x) + sqr(y) <= sqr(d); } boolean good2(int x, int y) { return sqr(2 * x - 1) + sqr(2 * y - 1) <= sqr(d); } int solveRangeY(int q, int x0, int y0, int x1, int y1) { //printV("solveRangeY(" + q + "," + x0 + "," + y0 + "," + x1 + "," + y1 + ")"); int cnt; if (q <= 0) { rotateCCW(); cnt = solveRangeX(-q + 1, -y0, x0, -y1, x1); rotateCW(); } else { rotateCW(); cnt = solveRangeX(q, y0, -x0, y1, -x1); rotateCCW(); } return cnt; } static int projectRay(int p, int x0, int y0) { return div(x0 + y0 * (2 * p - 1), 2 * x0); } static int div(int a, int b) { assert b > 0; return a >= 0 ? a / b : -((-a + b - 1)/ b); } static int ccw(int x0, int y0, int x1, int y1) { return x0 * y1 - x1 * y0; } static int sqr(int x) { return x * x; } }
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,722
C20050
C20085
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
0
3,723
C20050
C20020
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
0
3,724
C20050
C20062
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
1
3,725
C20050
C20059
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,726
C20050
C20064
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,727
C20050
C20076
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,728
C20050
C20026
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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);}}} }
0
3,729
C20050
C20078
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,730
C20050
C20057
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,731
C20050
C20030
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
/* * 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,732
C20050
C20002
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,733
C20050
C20056
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,734
C20050
C20018
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,735
C20050
C20051
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,736
C20085
C20020
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
0
3,737
C20085
C20062
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,738
C20085
C20059
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,739
C20085
C20064
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,740
C20085
C20076
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,741
C20085
C20026
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
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);}}} }
0
3,742
C20085
C20078
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
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,743
C20085
C20057
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
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,744
C20085
C20030
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
/* * 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,745
C20085
C20002
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
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,746
C20085
C20056
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
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,747
C20085
C20018
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
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,748
C20085
C20051
import java.util.Scanner; import java.io.IOException; import java.util.Arrays; import java.util.ArrayList; import java.io.PrintStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream; try { inputStream = new FileInputStream("D-large.in"); } catch (IOException e) { throw new RuntimeException(e); } OutputStream outputStream; try { outputStream = new FileOutputStream("gcj4.out"); } catch (IOException e) { throw new RuntimeException(e); } Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); GCJ4 solver = new GCJ4(); solver.solve(1, in, out); out.close(); } } class GCJ4 { int[] DX = {1,1,-1,-1}; int[] DY = {1,-1,1,-1}; double EPS = 1e-9; public void solve(int testNumber, Scanner in, PrintWriter out) { int cases = in.nextInt(); for(int caseNum =0;caseNum<cases;caseNum++) { int res = 0; int I = in.nextInt(); int J = in.nextInt(); int D = in.nextInt(); int SX = -1; int SY = -1; in.nextLine(); String[] m = new String[I]; for(int i=0;i<I;i++) { m[i] = in.nextLine(); for(int j=0;j<J;j++) { if(m[i].charAt(j)=='X') { SX = j; SY = i; } } } for(int y=0;y<=50;y++) { for(int x=0;x<=50;x++) { next_try: for(int k=0;k<4;k++) { if(x==0 && y==0)continue; if(IntLib.gcd(x,y)>1)continue; int dx = DX[k]; int dy = DY[k]; if(dx==-1 && x==0 || dy==-1 && y==0)continue; if(x==0)dx=0; if(y==0)dy=0; double dist = 0; int curX = SX; int curY = SY; int ddx = 0; int ddy = 0; //debug("NEXT",x,y); while(ddx*ddx+ddy*ddy<=D*D) { //if(x==3 && y==7)debug(ddx,ddy); boolean goRight = false; boolean goUp = false; if(y==0)goRight = true; else if(x==0)goUp = true; else { int minAnchor = Math.min(ddx/x,ddy/y); int fromAnchorX = ddx-x*minAnchor; int fromAnchorY = ddy-y*minAnchor; if(x%2==1 && y%2==1 && fromAnchorX==(x+1)/2-1 && fromAnchorY==(y+1)/2-1) { goRight = goUp = true; } else { if(GeomLib.cross2D(0.,0.,x,y,ddx+.5,ddy+.5)>0) { goRight = true; } else goUp = true; } } int nextX = curX; int nextY = curY; if(goRight) { nextX+=dx; } if(goUp) { nextY+=dy; } if(m[nextY].charAt(nextX)=='#') { if(goUp && goRight) { if(m[curY].charAt(nextX)=='#' && m[nextY].charAt(curX)=='#') { int ma = Math.min(ddx/x,ddy/y); double fx = x*ma; double fy = y*ma; double smx = x*.5; double smy = y*.5; if(Math.sqrt(fx*fx+fy*fy)+Math.sqrt(smx*smx+smy*smy)-EPS<D*.5) { //debug("CORNER",x*DX[k],y*DY[k],curX,curY); res++; } continue next_try; } else if(m[curY].charAt(nextX)=='#') { nextX -=dx; dx=-dx; } else if(m[nextY].charAt(curX)=='#') { nextY -= dy; dy=-dy; } else continue next_try;//lost ray } else if (goUp) { if(x==0) { if(Math.abs(SY-nextY)*2-1<=D) { //debug("VER REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextY-=dy; dy=-dy; } else if (goRight) { if(y==0) { if(Math.abs(SX-nextX)*2-1<=D) { //debug("HOR REFLECT",x*DX[k],y*DY[k]); res++; } continue next_try; } nextX-=dx; dx=-dx; } else throw new RuntimeException(); } if(goRight) { ddx++; } if(goUp) { ddy++; } if(nextY == SY && nextX == SX) { int ma = Math.min(ddx/x,ddy/y); if(ddx==ma*x && ddy==ma*y) { if(ddx*ddx +ddy*ddy <=D*D) { //debug("BOUNCY",x*DX[k],y*DY[k]); res++; } continue next_try; } } curX = nextX; curY = nextY; } } } } out.println("Case #"+(caseNum+1)+": "+res); debug("Case #"+(caseNum+1)+": ",m); } } void debug(Object...os) { // BEGIN CUT HERE System.out.println(Arrays.deepToString(os)); // END CUT HERE } } class IntLib { public static int gcd(int a, int b) { if(b==0)return a; else return gcd(b,a%b); } } class GeomLib { public static double cross2D(double ax, double ay, double bx, double by, double cx, double cy) { double abx = bx-ax; double aby = by-ay; double acx = cx-bx; double acy = cy-by; return abx*acy - aby*acx; } }
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,749
C20020
C20062
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
0
3,750
C20020
C20059
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,751
C20020
C20064
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,752
C20020
C20076
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,753
C20020
C20026
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
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);}}} }
0
3,754
C20020
C20078
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
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,755
C20020
C20057
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
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,756
C20020
C20030
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
/* * 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,757
C20020
C20002
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
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,758
C20020
C20056
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
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,759
C20020
C20018
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
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,760
C20020
C20051
package template; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Utils { static String logfile = ""; public static BufferedWriter newBufferedWriter(String filename, boolean append) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename, append)); } catch (IOException ex) { die("Exception in newBufferedWriter"); } return bw; } public static void writeLn(BufferedWriter bw, String line) { try { bw.write(line); bw.newLine(); } catch (IOException ex) { die("Exception in writeLn"); } } public static void closeBw(BufferedWriter bw) { try { bw.flush(); bw.close(); } catch (IOException ex) { die("Exception in closeBw"); } } public static BufferedReader newBufferedReader(String filename) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); } catch (IOException ex) { die("Exception in newBufferedReader"); } return br; } public static String readLn(BufferedReader br) { String s = null; try { s = br.readLine(); } catch (IOException ex) { die("Exception in readLn"); } return s; } public static Integer readInteger(BufferedReader br) { return new Integer(readLn(br)); } public static ArrayList<Integer> readIntegerList(BufferedReader br) { return readIntegerList(br, null); } public static ArrayList<Integer> readIntegerList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Integer(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readIntegerList"); } } return l; } public static ArrayList<Integer> readMultipleIntegers(BufferedReader br, Integer rows) { ArrayList<Integer> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Integer(s)); } return l; } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows) { return readIntegerMatrix(br, rows, null); } public static ArrayList<ArrayList<Integer>> readIntegerMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Integer>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readIntegerList(br, expectedLength)); } return l; } public static Double readDouble(BufferedReader br) { return new Double(readLn(br)); } public static ArrayList<Double> readDoubleList(BufferedReader br) { return readDoubleList(br, null); } public static ArrayList<Double> readDoubleList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Double(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readDoubleList"); } } return l; } public static ArrayList<Double> readMultipleDoubles(BufferedReader br, Integer rows) { ArrayList<Double> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Double(s)); } return l; } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows) { return readDoubleMatrix(br, rows, null); } public static ArrayList<ArrayList<Double>> readDoubleMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Double>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readDoubleList(br, expectedLength)); } return l; } public static Long readLong(BufferedReader br) { return new Long(readLn(br)); } public static ArrayList<Long> readLongList(BufferedReader br) { return readLongList(br, null); } public static ArrayList<Long> readLongList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Long(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readLongList"); } } return l; } public static ArrayList<Long> readMultipleLongs(BufferedReader br, Integer rows) { ArrayList<Long> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Long(s)); } return l; } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows) { return readLongMatrix(br, rows, null); } public static ArrayList<ArrayList<Long>> readLongMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Long>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readLongList(br, expectedLength)); } return l; } public static String readString(BufferedReader br) { return new String(readLn(br)); } public static ArrayList<String> readStringList(BufferedReader br) { return readStringList(br, null); } public static ArrayList<String> readStringList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new String(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readStringList"); } } return l; } public static ArrayList<String> readMultipleStrings(BufferedReader br, Integer rows) { ArrayList<String> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new String(s)); } return l; } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows) { return readStringMatrix(br, rows, null); } public static ArrayList<ArrayList<String>> readStringMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<String>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readStringList(br, expectedLength)); } return l; } public static Boolean readBoolean(BufferedReader br) { return new Boolean(readLn(br)); } public static ArrayList<Boolean> readBooleanList(BufferedReader br) { return readBooleanList(br, null); } public static ArrayList<Boolean> readBooleanList(BufferedReader br, Integer expectedLength) { String[] s = readLn(br).split(" "); ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < s.length; x++) { l.add(new Boolean(s[x])); } if (expectedLength != null) { if (l.size() != expectedLength.intValue()) { die("Incorrect length in readBooleanList"); } } return l; } public static ArrayList<Boolean> readMultipleBooleans(BufferedReader br, Integer rows) { ArrayList<Boolean> l = new ArrayList<>(); for (int x = 0; x < rows; x++) { String s = readLn(br); l.add(new Boolean(s)); } return l; } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows) { return readBooleanMatrix(br, rows, null); } public static ArrayList<ArrayList<Boolean>> readBooleanMatrix(BufferedReader br, Integer rows, Integer expectedLength) { ArrayList<ArrayList<Boolean>> l = new ArrayList<>(); for (int x = 0; x < rows.intValue(); x++) { l.add(readBooleanList(br, expectedLength)); } return l; } public static void closeBr(BufferedReader br) { try { br.close(); } catch (IOException ex) { die("Exception in closeBr"); } } public static void die(String reason) { sout("Die: " + reason); System.exit(0); } public static void sout(String s) { System.out.println(s); } public static void sout(int i) { System.out.println(i); } public static void sout(Object o) { System.out.println(o); } public static void log(String line) { BufferedWriter bw = newBufferedWriter(logfile, true); writeLn(bw, line); closeBw(bw); } public static void clearFile(String filename) { BufferedWriter bw = newBufferedWriter(filename, false); closeBw(bw); } public static int minInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() < i) { i = j.intValue(); } } return i; } public static int maxInt(ArrayList<Integer> l) { int i = l.get(0).intValue(); for (Integer j : l) { if (j.intValue() > i) { i = j.intValue(); } } return i; } public static String joinArray(ArrayList l, String delim) { String s = ""; for (int i = 0; i < l.size(); i++) { s += l.get(i).toString(); if (i < (l.size() - 1)) { s += delim; } } return s; } public static ArrayList<String> splitToChars(String source) { ArrayList<String> chars = new ArrayList<>(); for (int i = 0; i < source.length(); i++) { chars.add(source.substring(i, i + 1)); } return chars; } public static ArrayList<ArrayList<Integer>> allPairs(int lower1, int upper1, int lower2, int upper2, int style) { //Style: //0 all pairs //1 (1) <= (2) //2 (1) < (2) ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int index1 = lower1; index1 <= upper1; index1++) { for (int index2 = lower2; index2 <= upper2; index2++) { ArrayList<Integer> thisPair = new ArrayList<>(); thisPair.add(new Integer(index1)); thisPair.add(new Integer(index2)); switch (style) { case 0: out.add(thisPair); break; case 1: if (index1 <= index2) { out.add(thisPair); } break; case 2: if (index1 < index2) { out.add(thisPair); } break; default: die("Unrecognised case in allPairs"); } } } return out; } public static ArrayList<ArrayList<Integer>> cloneALALI(ArrayList<ArrayList<Integer>> in) { ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (ArrayList<Integer> inALI : in) { ArrayList<Integer> outALI = new ArrayList<>(inALI); out.add(outALI); } return out; } public static int[][] cloneInt2D(int[][] in) { if (in.length == 0) {return new int[0][0];} int[][] out = new int[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new int[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static double[][] cloneDouble2D(double[][] in) { if (in.length == 0) {return new double[0][0];} double[][] out = new double[in.length][]; for (int i = 0; i < in.length; i++) { out[i] = new double[in[i].length]; System.arraycopy(in[i], 0, out[i], 0, in[i].length); } return out; } public static ArrayList<ArrayList<Integer>> allPerms(int n) { //returns an arraylist of arraylists of integers //showing all permutations of Integers 0 to n-1 //works realistically up to n=10 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allPerms_recurse(n, n); } public static ArrayList<ArrayList<Integer>> allPerms_recurse(int level, int n) { if (level == 1) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(n - level)); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); list.add(single); return list; } ArrayList<ArrayList<Integer>> prev = allPerms_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int placeAt = 0; placeAt < level; placeAt++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(placeAt, new Integer(n - level)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<ArrayList<Integer>> allCombs(int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 //works realistically up to n=7 if (n == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(n, n); } public static ArrayList<ArrayList<Integer>> nCombs(int count, int n) { //returns an arraylist of arraylists of integers //showing all combinations of Integers 0 to n-1 --- of length "count" //i.e. base "n" counting up to (n^count - 1). In order. if (count == 0) { return new ArrayList<ArrayList<Integer>>(); } return allCombs_recurse(count, n); } public static ArrayList<ArrayList<Integer>> allCombs_recurse(int level, int n) { if (level == 1) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> single = new ArrayList<>(); single.add(new Integer(i)); list.add(single); } return list; } ArrayList<ArrayList<Integer>> prev = allCombs_recurse(level - 1, n); ArrayList<ArrayList<Integer>> out = new ArrayList<>(); for (int initial = 0; initial < n; initial++) { //clone prev ArrayList<ArrayList<Integer>> prevClone = cloneALALI(prev); //insert for (ArrayList<Integer> prevItem : prevClone) { prevItem.add(0, new Integer(initial)); } //append to out out.addAll(prevClone); } return out; } public static ArrayList<String> grepFull(ArrayList<String> inList, String pattern) { //pattern must match full text ArrayList<String> outList = new ArrayList<>(); for (String s : inList) { if (s.matches(pattern)) { outList.add(new String(s)); } } return outList; } public static int[] randomIntegerArray(int count, int low, int high, long seed) { //a list of "count" ints from low to high inclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextInt(high - low + 1) + low; } return out; } public static double[] randomDoubleArray(int count, double low, double high, long seed) { //a list of "count" ints from low inclusive to high exclusive. Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } double[] out = new double[count]; for (int x = 0; x < count; x++) { out[x] = rng.nextDouble() * (high - low) + low; } return out; } public static int[] randomPermutation(int count, long seed) { //random permutation of the array 0..(count-1). Random rng = new Random(); if (seed != -1) { rng.setSeed(seed); } int[] out = new int[count]; for (int x = 0; x < count; x++) { out[x] = x; } for (int x = 0; x < count - 1; x++) { int takeFrom = rng.nextInt(count - x) + x; int tmp = out[takeFrom]; out[takeFrom] = out[x]; out[x] = tmp; } return out; } public static ArrayList randomiseArray(ArrayList in, long seed) { //alternatively, Collections.shuffle(in, new Random(seed)) ArrayList out = new ArrayList(); int[] r = randomPermutation(in.size(), seed); for (int i : r) { out.add(in.get(i)); } return out; } public static ArrayList<Integer> arrayToArrayList(int[] in) { ArrayList<Integer> out = new ArrayList<>(); for (int i : in) { out.add(i); } return out; } public static ArrayList<Double> arrayToArrayList(double[] in) { ArrayList<Double> out = new ArrayList<>(); for (double d : in) { out.add(d); } return out; } public static int[] arrayListToArrayInt(ArrayList<Integer> in) { int[] out = new int[in.size()]; int x = 0; for (Integer i : in) { out[x] = i.intValue(); x++; } return out; } public static double[] arrayListToArrayDouble(ArrayList<Double> in) { double[] out = new double[in.size()]; int x = 0; for (Double d : in) { out[x] = d.doubleValue(); x++; } return out; } public static String toString(int[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(double[] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += in[x] + ", "; } s += in[in.length - 1] + "]"; return s; } public static String toString(int[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } public static String toString(double[][] in) { if (in.length == 0) { return "[]"; } String s = "["; for (int x = 0; x < in.length - 1; x++) { s += toString(in[x]) + ", "; } s += toString(in[in.length - 1]) + "]"; return s; } }
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,761
C20062
C20059
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
0
3,762
C20062
C20064
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,763
C20062
C20076
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,764
C20062
C20026
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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);}}} }
0
3,765
C20062
C20078
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,766
C20062
C20057
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,767
C20062
C20030
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
/* * 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,768
C20062
C20002
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,769
C20062
C20056
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,770
C20062
C20018
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,771
C20062
C20051
package jp.funnything.competition.util; public enum Direction { UP , DOWN , LEFT , RIGHT; public int dx() { switch ( this ) { case UP: case DOWN: return 0; case LEFT: return -1; case RIGHT: return 1; default: throw new RuntimeException( "assert" ); } } public int dy() { switch ( this ) { case UP: return -1; case DOWN: return 1; case LEFT: case RIGHT: return 0; default: throw new RuntimeException( "assert" ); } } public Direction reverese() { switch ( this ) { case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; default: throw new RuntimeException( "assert" ); } } public Direction turnLeft() { switch ( this ) { case UP: return LEFT; case DOWN: return RIGHT; case LEFT: return DOWN; case RIGHT: return UP; default: throw new RuntimeException( "assert" ); } } public Direction turnRight() { switch ( this ) { case UP: return RIGHT; case DOWN: return LEFT; case LEFT: return UP; case RIGHT: return DOWN; default: throw new RuntimeException( "assert" ); } } }
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,772
C20059
C20064
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
0
3,773
C20059
C20076
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,774
C20059
C20026
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
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);}}} }
0
3,775
C20059
C20078
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
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,776
C20059
C20057
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
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,777
C20059
C20030
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
/* * 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,778
C20059
C20002
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
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,779
C20059
C20056
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
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,780
C20059
C20018
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
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,781
C20059
C20051
package jp.funnything.competition.util; import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.io.FileUtils; public class CompetitionIO { private final QuestionReader _reader; private final AnswerWriter _writer; /** * Default constructor. Use latest "*.in" as input */ public CompetitionIO() { this( null , null , null ); } public CompetitionIO( final File input , final File output ) { this( input , output , null ); } public CompetitionIO( File input , File output , final String format ) { if ( input == null ) { for ( final File file : FileUtils.listFiles( new File( "." ) , new String[] { "in" } , false ) ) { if ( input == null || file.lastModified() > input.lastModified() ) { input = file; } } if ( input == null ) { throw new RuntimeException( "No *.in found" ); } } if ( output == null ) { final String inPath = input.getPath(); if ( !inPath.endsWith( ".in" ) ) { throw new IllegalArgumentException(); } output = new File( inPath.replaceFirst( ".in$" , ".out" ) ); } _reader = new QuestionReader( input ); _writer = new AnswerWriter( output , format ); } public CompetitionIO( final String format ) { this( null , null , format ); } public void close() { _reader.close(); _writer.close(); } public String read() { return _reader.read(); } public BigDecimal[] readBigDecimals() { return _reader.readBigDecimals(); } public BigDecimal[] readBigDecimals( final String separator ) { return _reader.readBigDecimals( separator ); } public BigInteger[] readBigInts() { return _reader.readBigInts(); } public BigInteger[] readBigInts( final String separator ) { return _reader.readBigInts( separator ); } /** * Read line as single integer */ public int readInt() { return _reader.readInt(); } /** * Read line as multiple integer, separated by space */ public int[] readInts() { return _reader.readInts(); } /** * Read line as multiple integer, separated by 'separator' */ public int[] readInts( final String separator ) { return _reader.readInts( separator ); } /** * Read line as single integer */ public long readLong() { return _reader.readLong(); } /** * Read line as multiple integer, separated by space */ public long[] readLongs() { return _reader.readLongs(); } /** * Read line as multiple integer, separated by 'separator' */ public long[] readLongs( final String separator ) { return _reader.readLongs( separator ); } /** * Read line as token list, separated by space */ public String[] readTokens() { return _reader.readTokens(); } /** * Read line as token list, separated by 'separator' */ public String[] readTokens( final String separator ) { return _reader.readTokens( separator ); } public void write( final int questionNumber , final Object result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result ) { _writer.write( questionNumber , result ); } public void write( final int questionNumber , final String result , final boolean tee ) { _writer.write( questionNumber , result , tee ); } }
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,782
C20064
C20076
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
0
3,783
C20064
C20026
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
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);}}} }
0
3,784
C20064
C20078
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
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,785
C20064
C20057
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
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,786
C20064
C20030
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
/* * 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,787
C20064
C20002
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
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,788
C20064
C20056
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
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,789
C20064
C20018
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
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,790
C20064
C20051
package jp.funnything.competition.util; public class Prof { private long _start; public Prof() { reset(); } private long calcAndReset() { final long ret = System.currentTimeMillis() - _start; reset(); return ret; } private void reset() { _start = System.currentTimeMillis(); } @Override public String toString() { return String.format( "Prof: %f (s)" , calcAndReset() / 1000.0 ); } public String toString( final String head ) { return String.format( "%s: %f (s)" , head , calcAndReset() / 1000.0 ); } }
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,791
C20076
C20026
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
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);}}} }
0
3,792
C20076
C20078
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
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,793
C20076
C20057
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
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,794
C20076
C20030
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
/* * 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,795
C20076
C20002
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
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,796
C20076
C20056
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
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,797
C20076
C20018
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
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,798
C20076
C20051
import java.io.*; import java.math.*; import java.util.*; public class D { final static boolean DEBUG = true; class S implements Comparable<S> { int a; int [] b; public int compareTo(S s) { if (a != s.a) return a - s.a; else return b[0] - s.b[0]; } boolean merge(S s) { if (a == s.a && b[1] >= s.b[0]) { b[0] = Math.min(b[0], s.b[0]); b[1] = Math.max(b[1], s.b[1]); return true; } else return false; } public String toString() { return "a=" + a + "; b=" + Arrays.toString(b); } } void add(List<S> L, int a, int b1, int b2) { S m = new S(); m.a = a; m.b = new int [] { b1, b2 }; L.add(m); } List<S> normalize(List<S> L) { List<S> tmp = new ArrayList<S>(); Collections.sort(L); S c = null; for (S s : L) { if (c == null) c = s; else { if (!c.merge(s)) { tmp.add(c); c = s; } } } assert(c != null); tmp.add(c); return tmp; } public Object solve () throws IOException { int ZZ = 300; int H = 2 * sc.nextInt(); int W = 2 * sc.nextInt(); int D = 2 * sc.nextInt(); int MH = H-4, MW = W-4; int [] ME = null; S[] LV,UV,LH,UH; { List<S> _LH = new ArrayList<S>(); List<S> _LV = new ArrayList<S>(); List<S> _UH = new ArrayList<S>(); List<S> _UV = new ArrayList<S>(); add(_LV, 0, -1, MH+1); add(_UV, MW, -1, MH+1); add(_LH, 0, -1, MW+1); add(_UH, MH, -1, MW+1); sc.nextLine(); for (int i = 0; i < MH/2; ++i) { char [] R = sc.nextChars(); for (int j = 0; j < MW/2; ++j) { char z = R[j+1]; if (z == 'X') ME = new int [] { 2*j+1, 2*i+1 }; if (z == '#') { int vl = 2*i, vu = 2*i+2, hl = 2*j, hu = 2*j+2; int dvl = 0, dvu = 0, dhl = 0, dhu = 0; if (vl == 0) ++dvl; if(vu == MH) ++dvu; if (hl == 0) ++dhl; if (hu == MW) ++dhu; add(_LV, hu, vl-dvl, vu+dvu); add(_UV, hl, vl-dvl, vu+dvu); add(_LH, vu, hl-dhl, hu+dhu); add(_UH, vl, hl-dhl, hu+dhu); } } } sc.nextLine(); _LV = normalize(_LV); _UV = normalize(_UV); _LH = normalize(_LH); _UH = normalize(_UH); LV = _LV.toArray(new S[0]); UV = _UV.toArray(new S[0]); LH = _LH.toArray(new S[0]); UH = _UH.toArray(new S[0]); } int cnt = 0; for (int ax = -ZZ; ax <= ZZ; ++ax) for (int ay = -ZZ; ay <= ZZ; ++ay) if (gcd(ax, ay) == 1) { double d = 0, px = ME[0], py = ME[1]; int tax = ax, tay = ay; while (d < D) { X slv = new X(), suv = new X(), slh = new X(), suh = new X(); if (tax < 0) slv = T(LV, px, py, tax, tay, ME[0], ME[1], MH); if (tax > 0) suv = T(UV, px, py, tax, tay, ME[0], ME[1], MH); if (tay < 0) slh = T(LH, py, px, tay, tax, ME[1], ME[0], MW); if (tay > 0) suh = T(UH, py, px, tay, tax, ME[1], ME[0], MW); double minvd = 1e20, minhd = 1e20, mind = 1e20; minvd = Math.min(slv.d, minvd); minvd = Math.min(suv.d, minvd); minhd = Math.min(slh.d, minhd); minhd = Math.min(suh.d, minhd); mind = Math.min(minvd, minhd); d += Math.sqrt(mind); if (d > D + seps) break; if (slv.d == mind && slv.kill) { if (slv.me) ++cnt; break; } if (suv.d == mind && suv.kill) { if (suv.me) ++cnt; break; } if (slh.d == mind && slh.kill) { if (slh.me) ++cnt; break; } if (suh.d == mind && suh.kill) { if (suh.me) ++cnt; break; } if (Math.abs(minvd - minhd) < eps) { if (d < D/2.0 + seps) ++cnt; break; } if (slv.d == mind) { px = slv.pa; py = slv.pb; tax = slv.aa; tay = slv.ab; }; if (suv.d == mind) { px = suv.pa; py = suv.pb; tax = suv.aa; tay = suv.ab; }; if (slh.d == mind) { px = slh.pb; py = slh.pa; tax = slh.ab; tay = slh.aa; }; if (suh.d == mind) { px = suh.pb; py = suh.pa; tax = suh.ab; tay = suh.aa; }; } } return cnt; } class X { double pa, pb, d = 1e20; int aa, ab; boolean me = false, kill = false; public String toString() { if (d == 1e20) return "NO"; if (me) return "ME; d=" + d; else if (kill) return "KILL; d=" + d; return "d=" + Math.sqrt(d) + "; pa=" + pa + "; pb=" + pb + "; aa=" + aa + "ab=" + ab; } } double eps = 1e-9; double seps = Math.sqrt(eps); X T(S[] L, double pa, double pb, int aa, int ab, double mea, double meb, int M) { double aaa = Math.abs(aa); double nab = ab/aaa, naa = aa/aaa; double dme = 1e20; double tme = (mea - pa)/naa; if (tme > 0 && Math.abs(meb - (tme*nab + pb)) < eps) dme = (mea-pa)*(mea-pa) + (meb-pb)*(meb-pb); X x = new X(); int P = -1, Q = L.length; int p = P, q = Q, m; while (q - p > 1) { m = (p+q)/2; if (m == P) ++m; if (L[m].a > pa) q = m; else p = m; } int z = naa > 0 ? q : p; int da = naa > 0 ? 1 : -1; int db = nab > 0 ? 1 : -1; for (; ; z += da) { S s = L[z]; double t = Math.abs(s.a - pa); // double sa = pa + t * naa; double sb = pb + t * nab; double d = (s.a-pa)*(s.a-pa) + (sb-pb)*(sb-pb); if (d > dme) { x.me = x.kill = true; x.d = dme; return x; } if (sb < -eps || sb > M + eps) return x; if (Math.abs(sb - s.b[(1-db)/2]) < eps) { x.kill = true; x.d = d; return x; } if (sb > s.b[0] && sb < s.b[1]) { x.pa = s.a; x.pb = sb; x.aa = -aa; x.ab = ab; x.d = d; return x; } } //throw new Error("NO WAY!"); } int gcd (int a, int b) { a = Math.abs(a); b = Math.abs(b); if (a*b != 0) return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue(); else return a+b; } void init () { } //////////////////////////////////////////////////////////////////////////////////// public D () throws IOException { init(); int N = sc.nextInt(); start(); for (int n = 1; n <= N; ++n) print("Case #" + n + ": " + solve()); exit(); } static MyScanner sc; static long t; static void print (Object o) { System.out.println(o); if (DEBUG) System.err.println(o + " (" + ((millis() - t) / 1000.0) + ")"); } static void exit () { if (DEBUG) { System.err.println(); System.err.println((millis() - t) / 1000.0); } System.exit(0); } static void run () throws IOException { sc = new MyScanner (); new D(); } public static void main(String[] args) throws IOException { run(); } static long millis() { return System.currentTimeMillis(); } static void start() { t = millis(); } static class MyScanner { String next() throws IOException { newLine(); return line[index++]; } char [] nextChars() throws IOException { return next().toCharArray(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine() throws IOException { line = null; return r.readLine(); } String [] nextStrings() throws IOException { line = null; return r.readLine().split(" "); } int [] nextInts() throws IOException { String [] L = nextStrings(); int [] res = new int [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } long [] nextLongs() throws IOException { String [] L = nextStrings(); long [] res = new long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } boolean eol() { return index == line.length; } ////////////////////////////////////////////// private final BufferedReader r; MyScanner () throws IOException { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) throws IOException { this.r = r; } private String [] line; private int index; private void newLine() throws IOException { if (line == null || index == line.length) { line = r.readLine().split(" "); index = 0; } } } static class MyWriter { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); void print(Object o) { pw.print(o); } void println(Object o) { pw.println(o); } void println() { pw.println(); } public String toString() { return sw.toString(); } } char [] toArray (Character [] C) { char [] c = new char[C.length]; for (int i = 0; i < C.length; ++i) c[i] = C[i]; return c; } char [] toArray (Collection<Character> C) { char [] c = new char[C.size()]; int i = 0; for (char z : C) c[i++] = z; return c; } Object [] toArray(int [] a) { Object [] A = new Object[a.length]; for (int i = 0; i < A.length; ++i) A[i] = a[i]; return A; } String toString(Object [] a) { StringBuffer b = new StringBuffer(); for (Object o : a) b.append(" " + o); return b.toString().trim(); } String toString(int [] a) { return toString(toArray(a)); } }
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,799