import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.math.BigInteger; import java.nio.file.Files; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map.Entry; import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.DoubleUnaryOperator; import java.util.function.IntPredicate; import java.util.function.LongToDoubleFunction; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.random.RandomGenerator; import java.util.stream.IntStream; import java.util.stream.Stream; class FastScanner { private static FastScanner instance = null; private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private FastScanner() { } public static FastScanner getInstance() { if (instance == null) { instance = new FastScanner(); } return instance; } private boolean hasNextByte() { if (ptr < buflen) { return true; } ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } return buflen > 0; } private int readByte() { if (hasNextByte()) { return buffer[ptr++]; } else { return -1; } } private boolean isPrintableChar(int c) { return (33 <= c) && (c <= 126); } public boolean hasNext() { while (hasNextByte() && (!isPrintableChar(buffer[ptr]))) { ptr++; } return hasNextByte(); } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } while ((b >= '0') && (b <= '9')) { // n = n * 10 + (b - '0'); n = ((n << 1) + (n << 3)) + (b - '0'); b = readByte(); } return minus ? -n : n; } public int nextInt() { return ((int) (nextLong())); } } class MergeFiles {} class MyPrintWriter extends PrintWriter { private static MyPrintWriter instance = null; private MyPrintWriter() { super(System.out); } public static MyPrintWriter getInstance() { if (instance == null) { instance = new MyPrintWriter(); } return instance; } public void println(boolean[][] a) { for (int i = 0; i < a.length; i++) { println(a[i], " "); } } public void println(boolean[] a, String separator) { for (int i = 0; i < a.length; ++i) { super.print((a[i] ? 1 : 0) + (i == (a.length - 1) ? "\n" : separator)); } } } /* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * Factory for {@link IntStream}. *
* Only a factory for now but could hold other functionality. *
* * @since 3.13.0 */ class IntStreams { /** * Shorthand for {@code IntStream.range(0, i)}. * * @param endExclusive * the exclusive upper bound. * @return a sequential {@link IntStream} for the range of {@code int} elements. */ public static IntStream range(final int endExclusive) { return IntStream.range(0, endExclusive); } } class MathUtils { /** * C(n,i)=C(n,i-1)(n-i+1)/iで計算 * * @param n * @param k * @return */ public static long comb(int n, int k) { if ((k < 0) || ((n - k) < 0)) { return 0; } if (k > (n / 2)) { return MathUtils.comb(n, n - k); } if (k == 0) { return 1; } if (k == 1) { return n; } if (k == 2) { return ((1L * n) * (n - 1)) / 2; } if (k > (n - k)) { return MathUtils.comb(n, n - k); } long ans = 1; for (int i = 1; i <= k; i++) { // C(n,i)=C(n,i-1)(n-i+1)/i ans = (ans * ((n - i) + 1)) / i; } return ans; } } public class Main implements Runnable { public static void main(String[] args) throws IOException { Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1)); // Runtime runtime = Runtime.getRuntime(); // new Thread(null, new Main(), "MainThreadWithLargeStack", (1024 * 1024) * 1024).start(); // new Main().test(); // new Main().gen(); new Main().run(); // long usedMemory = runtime.totalMemory() - runtime.freeMemory(); // System.err.printf("使用メモリ: %.2f MB%n", usedMemory / 1024.0 / 1024.0); MyPrintWriter.getInstance().flush(); } @Override public void run() { Random rnd = new Random(); FastScanner sc = FastScanner.getInstance(); MyPrintWriter pw = MyPrintWriter.getInstance(); // String f="c"; // ArrayList// * Only a factory for now but could hold other functionality. // *
// * // * @since 3.13.0 // */ // class IntStreams { // // /** // * Null-safe version of {@link IntStream#of(int[])}. // * // * @param values the elements of the new stream, may be {@code null}. // * @return the new stream on {@code values} or {@link IntStream#empty()}. // * @since 3.18.0 // */ // @SafeVarargs // Creating a stream from an array is safe // public static IntStream of(final int... values) { // return values == null ? IntStream.empty() : IntStream.of(values); // } // // /** // * Shorthand for {@code IntStream.range(0, i)}. // * // * @param endExclusive the exclusive upper bound. // * @return a sequential {@link IntStream} for the range of {@code int} elements. // */ // public static IntStream range(final int endExclusive) { // return IntStream.range(0, endExclusive); // } // // /** // * Shorthand for {@code IntStream.rangeClosed(0, i)}. // * // * @param endInclusive the inclusive upper bound. // * @return a sequential {@link IntStream} for the range of {@code int} elements. // */ // public static IntStream rangeClosed(final int endInclusive) { // return IntStream.rangeClosed(0, endInclusive); // } // // /** // * Make private in 4.0. // * // * @deprecated TODO Make private in 4.0. // */ // @Deprecated // public IntStreams() { // // empty // } // } //