Showing the changes to java/tests/EgorTask1Test.java (1 of 3 files). Show all
Unified · Split
| @@ -0,0 +1,237 @@ | |||
| 1 | |||
| 2 | import static utils.Utils.*; | ||
| 3 | |||
| 4 | import java.io.OutputStreamWriter; | ||
| 5 | import java.io.BufferedWriter; | ||
| 6 | import java.util.Comparator; | ||
| 7 | import java.io.OutputStream; | ||
| 8 | import java.io.PrintWriter; | ||
| 9 | import java.io.Writer; | ||
| 10 | import java.util.List; | ||
| 11 | import java.io.IOException; | ||
| 12 | import java.util.Arrays; | ||
| 13 | import java.util.InputMismatchException; | ||
| 14 | import java.util.ArrayList; | ||
| 15 | import java.util.NoSuchElementException; | ||
| 16 | import java.math.BigInteger; | ||
| 17 | import java.io.InputStream; | ||
| 18 | |||
| 19 | /// This test is based on Egor Kulikov task solution from | ||
| 20 | /// http://codeforces.com/submissions/Egor | ||
| 21 | /// http://codeforces.com/problemset/problem/336/E | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Built using CHelper plug-in | ||
| 25 | * Actual solution is at the top | ||
| 26 | * @author Egor Kulikov (egor@egork.net) | ||
| 27 | */ | ||
| 28 | public class EgorTask1Test { | ||
| 29 | public static void main(String[] args) { | ||
| 30 | InputStream inputStream = System.in; | ||
| 31 | OutputStream outputStream = System.out; | ||
| 32 | InputReader in = new InputReader(inputStream); | ||
| 33 | OutputWriter out = new OutputWriter(outputStream); | ||
| 34 | TaskE solver = new TaskE(); | ||
| 35 | solver.solve(1, in, out); | ||
| 36 | out.close(); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | class TaskE { | ||
| 41 | static final long MOD = 1000000007; | ||
| 42 | |||
| 43 | long[][][] result; | ||
| 44 | long[][][] resultSmall; | ||
| 45 | long[][] c = IntegerUtils.generateBinomialCoefficients(13); | ||
| 46 | |||
| 47 | public void solve(int testNumber, InputReader in, OutputWriter out) { | ||
| 48 | int size = in.readInt(); | ||
| 49 | int count = in.readInt(); | ||
| 50 | result = new long[size + 1][count + 1][16]; | ||
| 51 | resultSmall = new long[size + 1][count + 1][16]; | ||
| 52 | ArrayUtils.fill(result, -1); | ||
| 53 | ArrayUtils.fill(resultSmall, -1); | ||
| 54 | long answer = calculate(size, count, 0); | ||
| 55 | answer *= IntegerUtils.factorial(count, MOD); | ||
| 56 | answer %= MOD; | ||
| 57 | out.printLine(answer); | ||
| 58 | } | ||
| 59 | |||
| 60 | private long calculate(int size, int count, int mask) { | ||
| 61 | if (count < 0) | ||
| 62 | return 0; | ||
| 63 | if (result[size][count][mask] != -1) | ||
| 64 | return result[size][count][mask]; | ||
| 65 | long current = 0; | ||
| 66 | if (mask == 0) | ||
| 67 | current += 2 * calculate(0, count - 2, 15); | ||
| 68 | for (int i = 0; i < 4; i++) { | ||
| 69 | int j = (i + 1) & 3; | ||
| 70 | if ((mask >> i & 1) != 0 || (mask >> j & 1) != 0) | ||
| 71 | continue; | ||
| 72 | int remaining = 15 - mask - (1 << i) - (1 << j); | ||
| 73 | for (int k = remaining; ; k = (k - 1) & remaining) { | ||
| 74 | current += calculateSmall(size, count - 1 - Integer.bitCount(k), mask + (1 << i) + (1 << j) + k); | ||
| 75 | if (k == 0) | ||
| 76 | break; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | int remaining = 15 - mask; | ||
| 80 | for (int k = remaining; ; k = (k - 1) & remaining) { | ||
| 81 | current += calculateSmall(size, count - Integer.bitCount(k), mask + k); | ||
| 82 | if (k == 0) | ||
| 83 | break; | ||
| 84 | } | ||
| 85 | return result[size][count][mask] = current % MOD; | ||
| 86 | } | ||
| 87 | |||
| 88 | private long calculateSmall(int size, int count, int mask) { | ||
| 89 | if (count < 0) | ||
| 90 | return 0; | ||
| 91 | if (resultSmall[size][count][mask] != -1) | ||
| 92 | return resultSmall[size][count][mask]; | ||
| 93 | if (size == 0) | ||
| 94 | return resultSmall[size][count][mask] = count == 0 ? 1 : 0; | ||
| 95 | int max = 3 * Integer.bitCount(15 - mask); | ||
| 96 | int pairCount = 0; | ||
| 97 | for (int i = 0; i < 4; i++) { | ||
| 98 | int j = (i + 1) & 3; | ||
| 99 | if ((mask >> i & 1) == 0 && ((mask >> j & 1) == 0)) | ||
| 100 | pairCount++; | ||
| 101 | } | ||
| 102 | long current = 0; | ||
| 103 | for (int j = 0; j <= pairCount; j++) { | ||
| 104 | for (int i = 0; i <= max - 2 * j; i++) | ||
| 105 | current += calculate(size - 1, count - i - j, mask) * c[max - 2 * j][i] * c[pairCount][j]; | ||
| 106 | } | ||
| 107 | return resultSmall[size][count][mask] = current % MOD; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | class IntegerUtils { | ||
| 112 | |||
| 113 | public static long[][] generateBinomialCoefficients(int n) { | ||
| 114 | long[][] result = new long[n + 1][n + 1]; | ||
| 115 | for (int i = 0; i <= n; i++) { | ||
| 116 | result[i][0] = 1; | ||
| 117 | for (int j = 1; j <= i; j++) | ||
| 118 | result[i][j] = result[i - 1][j - 1] + result[i - 1][j]; | ||
| 119 | } | ||
| 120 | return result; | ||
| 121 | } | ||
| 122 | |||
| 123 | public static long factorial(int n, long mod) { | ||
| 124 | long result = 1; | ||
| 125 | for (int i = 2; i <= n; i++) | ||
| 126 | result = result * i % mod; | ||
| 127 | return result % mod; | ||
| 128 | } | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | class InputReader { | ||
| 133 | |||
| 134 | private InputStream stream; | ||
| 135 | private byte[] buf = new byte[1024]; | ||
| 136 | private int curChar; | ||
| 137 | private int numChars; | ||
| 138 | private SpaceCharFilter filter; | ||
| 139 | |||
| 140 | public InputReader(InputStream stream) { | ||
| 141 | this.stream = stream; | ||
| 142 | } | ||
| 143 | |||
| 144 | public int read() { | ||
| 145 | if (numChars == -1) | ||
| 146 | throw new InputMismatchException(); | ||
| 147 | if (curChar >= numChars) { | ||
| 148 | curChar = 0; | ||
| 149 | try { | ||
| 150 | numChars = stream.read(buf); | ||
| 151 | } catch (IOException e) { | ||
| 152 | throw new InputMismatchException(); | ||
| 153 | } | ||
| 154 | if (numChars <= 0) | ||
| 155 | return -1; | ||
| 156 | } | ||
| 157 | return buf[curChar++]; | ||
| 158 | } | ||
| 159 | |||
| 160 | public int readInt() { | ||
| 161 | int c = read(); | ||
| 162 | while (isSpaceChar(c)) | ||
| 163 | c = read(); | ||
| 164 | int sgn = 1; | ||
| 165 | if (c == '-') { | ||
| 166 | sgn = -1; | ||
| 167 | c = read(); | ||
| 168 | } | ||
| 169 | int res = 0; | ||
| 170 | do { | ||
| 171 | if (c < '0' || c > '9') | ||
| 172 | throw new InputMismatchException(); | ||
| 173 | res *= 10; | ||
| 174 | res += c - '0'; | ||
| 175 | c = read(); | ||
| 176 | } while (!isSpaceChar(c)); | ||
| 177 | return res * sgn; | ||
| 178 | } | ||
| 179 | |||
| 180 | public boolean isSpaceChar(int c) { | ||
| 181 | if (filter != null) | ||
| 182 | return filter.isSpaceChar(c); | ||
| 183 | return isWhitespace(c); | ||
| 184 | } | ||
| 185 | |||
| 186 | public static boolean isWhitespace(int c) { | ||
| 187 | return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | ||
| 188 | } | ||
| 189 | |||
| 190 | public interface SpaceCharFilter { | ||
| 191 | public boolean isSpaceChar(int ch); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | class OutputWriter { | ||
| 196 | private final PrintWriter writer; | ||
| 197 | |||
| 198 | public OutputWriter(OutputStream outputStream) { | ||
| 199 | writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); | ||
| 200 | } | ||
| 201 | |||
| 202 | public OutputWriter(Writer writer) { | ||
| 203 | this.writer = new PrintWriter(writer); | ||
| 204 | } | ||
| 205 | |||
| 206 | public void print(Object...objects) { | ||
| 207 | for (int i = 0; i < objects.length; i++) { | ||
| 208 | if (i != 0) | ||
| 209 | writer.print(' '); | ||
| 210 | writer.print(objects[i]); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | public void printLine(Object...objects) { | ||
| 215 | print(objects); | ||
| 216 | writer.println(); | ||
| 217 | } | ||
| 218 | |||
| 219 | public void close() { | ||
| 220 | writer.close(); | ||
| 221 | } | ||
| 222 | |||
| 223 | } | ||
| 224 | |||
| 225 | class ArrayUtils { | ||
| 226 | |||
| 227 | public static void fill(long[][] array, long value) { | ||
| 228 | for (long[] row : array) | ||
| 229 | Arrays.fill(row, value); | ||
| 230 | } | ||
| 231 | |||
| 232 | public static void fill(long[][][] array, long value) { | ||
| 233 | for (long[][] row : array) | ||
| 234 | fill(row, value); | ||
| 235 | } | ||
| 236 | |||
| 237 | } | ||