結果

問題 No.3320 yiwiwiy
コンテスト
ユーザー kencho
提出日時 2025-11-01 00:30:47
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,651 ms / 2,000 ms
コード長 25,249 bytes
コンパイル時間 5,869 ms
コンパイル使用メモリ 91,028 KB
実行使用メモリ 69,840 KB
最終ジャッジ日時 2025-11-01 00:32:09
合計ジャッジ時間 76,702 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 73
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.*;

public class H {

    public static final int MOD998 = 998244353;
    public static final int MOD100 = 1000000007;

    public static void main(String[] args) throws Exception {
        ContestScanner sc = new ContestScanner();
        ContestPrinter cp = new ContestPrinter();
        int T = sc.nextInt();
        for (int t = 0; t < T; t++) {
            long Y = sc.nextInt();
            int I = sc.nextInt();
            int W = sc.nextInt();
            int A = sc.nextInt();
            int B = sc.nextInt();
            BigInteger max = BigInteger.valueOf(-1);
            long[] ans = new long[5];
            for (int ab = 0; ab <= Math.min(I, W); ab++) {
                int cd = I - ab;
                int e = W - ab;
                for (int a : new int[] { 0, ab / 2 - 1, ab / 2, ab / 2 + 1 }) {
                    if (a < 0 || ab < a) {
                        continue;
                    }
                    for (int c = Math.max(cd / 2, 0); c <= Math.min(cd / 2 + 1, cd); c++) {
                        long b = ab - a;
                        long d = cd - c;
                        long iwiwi = ((c >= 1 && a >= 2) ? a - 1 : 0) + ((d >= 1 && b >= 2) ? b - 1 : 0);
                        long iwibase = (Y / 2) * ((Y + 1) / 2);
                        long iwimult = calc(a + b + c + d, b + d + 1, b + d + a)
                                + calc(a + b + c + d, a + c + 1, a + c + b) + (a + c) * (b + d) * e;
                        BigInteger score = BigInteger.valueOf(iwiwi * B).add(BigInteger.valueOf(iwibase)
                                .multiply(BigInteger.valueOf(iwimult)).multiply(BigInteger.valueOf(A)));

                        if (score.compareTo(max) > 0) {
                            max = score;
                            ans = new long[] { a, b, c, d, e };
                        }
                    }
                }
            }
            System.out.println(ansToString(Y, ans));
            System.err.println(max.toString());
        }
        cp.close();
    }

    public static long calc(long n, long s, long t) {
        return n * (t - s + 1) * (t + s) / 2 - t * (t + 1) * (t * 2 + 1) / 6
                + (s - 1) * s * (s * 2 - 1) / 6;
    }

    public static String ansToString(long Y, long[] ans) {
        StringBuilder sb = new StringBuilder();
        for (long i = 0; i < Y / 2; i++) {
            sb.append('y');
        }
        for (long i = 0; i < ans[2]; i++) {
            sb.append('i');
        }
        for (long i = 0; i < ans[0]; i++) {
            sb.append('w');
            sb.append('i');
        }
        for (long i = 0; i < ans[4]; i++) {
            sb.append('w');
        }
        for (long i = 0; i < ans[1]; i++) {
            sb.append('i');
            sb.append('w');
        }
        for (long i = 0; i < ans[3]; i++) {
            sb.append('i');
        }
        for (long i = 0; i < (Y + 1) / 2; i++) {
            sb.append('y');
        }
        return sb.toString();
    }

    static class StringAlgorithm {
        private static int[] saNaive(int[] s) {
            int n = s.length;
            int[] sa = new int[n];
            for (int i = 0; i < n; i++) {
                sa[i] = i;
            }
            insertionsortUsingComparator(sa, (l, r) -> {
                while (l < n && r < n) {
                    if (s[l] != s[r])
                        return s[l] - s[r];
                    l++;
                    r++;
                }
                return -(l - r);
            });
            return sa;
        }

        public static int[] saDoubling(int[] s) {
            int n = s.length;
            int[] sa = new int[n];
            for (int i = 0; i < n; i++) {
                sa[i] = i;
            }
            int[] rnk = java.util.Arrays.copyOf(s, n);
            int[] tmp = new int[n];

            for (int k = 1; k < n; k *= 2) {
                final int _k = k;
                final int[] _rnk = rnk;
                java.util.function.IntBinaryOperator cmp = (x, y) -> {
                    if (_rnk[x] != _rnk[y])
                        return _rnk[x] - _rnk[y];
                    int rx = x + _k < n ? _rnk[x + _k] : -1;
                    int ry = y + _k < n ? _rnk[y + _k] : -1;
                    return rx - ry;
                };
                mergesortUsingComparator(sa, cmp);
                tmp[sa[0]] = 0;
                for (int i = 1; i < n; i++) {
                    tmp[sa[i]] = tmp[sa[i - 1]] + (cmp.applyAsInt(sa[i - 1], sa[i]) < 0 ? 1 : 0);
                }
                int[] buf = tmp;
                tmp = rnk;
                rnk = buf;
            }
            return sa;
        }

        private static void insertionsortUsingComparator(int[] a, java.util.function.IntBinaryOperator comparator) {
            final int n = a.length;
            for (int i = 1; i < n; i++) {
                final int tmp = a[i];
                if (comparator.applyAsInt(a[i - 1], tmp) > 0) {
                    int j = i;
                    do {
                        a[j] = a[j - 1];
                        j--;
                    } while (j > 0 && comparator.applyAsInt(a[j - 1], tmp) > 0);
                    a[j] = tmp;
                }
            }
        }

        private static void mergesortUsingComparator(int[] a, java.util.function.IntBinaryOperator comparator) {
            final int n = a.length;
            final int[] work = new int[n];
            for (int block = 1; block <= n; block <<= 1) {
                final int block2 = block << 1;
                for (int l = 0, max = n - block; l < max; l += block2) {
                    int m = l + block;
                    int r = Math.min(l + block2, n);
                    System.arraycopy(a, l, work, 0, block);
                    for (int i = l, wi = 0, ti = m;; i++) {
                        if (ti == r) {
                            System.arraycopy(work, wi, a, i, block - wi);
                            break;
                        }
                        if (comparator.applyAsInt(work[wi], a[ti]) > 0) {
                            a[i] = a[ti++];
                        } else {
                            a[i] = work[wi++];
                            if (wi == block)
                                break;
                        }
                    }
                }
            }
        }

        private static final int THRESHOLD_NAIVE = 50;
        // private static final int THRESHOLD_DOUBLING = 0;

        private static int[] sais(int[] s, int upper) {
            int n = s.length;
            if (n == 0)
                return new int[0];
            if (n == 1)
                return new int[] { 0 };
            if (n == 2) {
                if (s[0] < s[1]) {
                    return new int[] { 0, 1 };
                } else {
                    return new int[] { 1, 0 };
                }
            }
            if (n < THRESHOLD_NAIVE) {
                return saNaive(s);
            }
            // if (n < THRESHOLD_DOUBLING) {
            // return saDoubling(s);
            // }

            int[] sa = new int[n];
            boolean[] ls = new boolean[n];
            for (int i = n - 2; i >= 0; i--) {
                ls[i] = s[i] == s[i + 1] ? ls[i + 1] : s[i] < s[i + 1];
            }

            int[] sumL = new int[upper + 1];
            int[] sumS = new int[upper + 1];

            for (int i = 0; i < n; i++) {
                if (ls[i]) {
                    sumL[s[i] + 1]++;
                } else {
                    sumS[s[i]]++;
                }
            }

            for (int i = 0; i <= upper; i++) {
                sumS[i] += sumL[i];
                if (i < upper)
                    sumL[i + 1] += sumS[i];
            }

            java.util.function.Consumer<int[]> induce = lms -> {
                java.util.Arrays.fill(sa, -1);
                int[] buf = new int[upper + 1];
                System.arraycopy(sumS, 0, buf, 0, upper + 1);
                for (int d : lms) {
                    if (d == n)
                        continue;
                    sa[buf[s[d]]++] = d;
                }
                System.arraycopy(sumL, 0, buf, 0, upper + 1);
                sa[buf[s[n - 1]]++] = n - 1;
                for (int i = 0; i < n; i++) {
                    int v = sa[i];
                    if (v >= 1 && !ls[v - 1]) {
                        sa[buf[s[v - 1]]++] = v - 1;
                    }
                }
                System.arraycopy(sumL, 0, buf, 0, upper + 1);
                for (int i = n - 1; i >= 0; i--) {
                    int v = sa[i];
                    if (v >= 1 && ls[v - 1]) {
                        sa[--buf[s[v - 1] + 1]] = v - 1;
                    }
                }
            };

            int[] lmsMap = new int[n + 1];
            java.util.Arrays.fill(lmsMap, -1);
            int m = 0;
            for (int i = 1; i < n; i++) {
                if (!ls[i - 1] && ls[i]) {
                    lmsMap[i] = m++;
                }
            }

            int[] lms = new int[m];
            {
                int p = 0;
                for (int i = 1; i < n; i++) {
                    if (!ls[i - 1] && ls[i]) {
                        lms[p++] = i;
                    }
                }
            }

            induce.accept(lms);

            if (m > 0) {
                int[] sortedLms = new int[m];
                {
                    int p = 0;
                    for (int v : sa) {
                        if (lmsMap[v] != -1) {
                            sortedLms[p++] = v;
                        }
                    }
                }
                int[] recS = new int[m];
                int recUpper = 0;
                recS[lmsMap[sortedLms[0]]] = 0;
                for (int i = 1; i < m; i++) {
                    int l = sortedLms[i - 1], r = sortedLms[i];
                    int endL = (lmsMap[l] + 1 < m) ? lms[lmsMap[l] + 1] : n;
                    int endR = (lmsMap[r] + 1 < m) ? lms[lmsMap[r] + 1] : n;
                    boolean same = true;
                    if (endL - l != endR - r) {
                        same = false;
                    } else {
                        while (l < endL && s[l] == s[r]) {
                            l++;
                            r++;
                        }
                        if (l == n || s[l] != s[r])
                            same = false;
                    }
                    if (!same) {
                        recUpper++;
                    }
                    recS[lmsMap[sortedLms[i]]] = recUpper;
                }

                int[] recSA = sais(recS, recUpper);

                for (int i = 0; i < m; i++) {
                    sortedLms[i] = lms[recSA[i]];
                }
                induce.accept(sortedLms);
            }
            return sa;
        }

        public static int[] suffixArray(int[] s, int upper) {
            assert (0 <= upper);
            for (int d : s) {
                assert (0 <= d && d <= upper);
            }
            return sais(s, upper);
        }

        public static int[] suffixArray(int[] s) {
            int n = s.length;
            int[] vals = Arrays.copyOf(s, n);
            java.util.Arrays.sort(vals);
            int p = 1;
            for (int i = 1; i < n; i++) {
                if (vals[i] != vals[i - 1]) {
                    vals[p++] = vals[i];
                }
            }
            int[] s2 = new int[n];
            for (int i = 0; i < n; i++) {
                s2[i] = java.util.Arrays.binarySearch(vals, 0, p, s[i]);
            }
            return sais(s2, p);
        }

        public static int[] suffixArray(char[] s) {
            int n = s.length;
            int[] s2 = new int[n];
            for (int i = 0; i < n; i++) {
                s2[i] = s[i];
            }
            return sais(s2, 255);
        }

        public static int[] suffixArray(java.lang.String s) {
            return suffixArray(s.toCharArray());
        }

        public static int[] lcpArray(int[] s, int[] sa) {
            int n = s.length;
            assert (n >= 1);
            int[] rnk = new int[n];
            for (int i = 0; i < n; i++) {
                rnk[sa[i]] = i;
            }
            int[] lcp = new int[n - 1];
            int h = 0;
            for (int i = 0; i < n; i++) {
                if (h > 0)
                    h--;
                if (rnk[i] == 0) {
                    continue;
                }
                int j = sa[rnk[i] - 1];
                for (; j + h < n && i + h < n; h++) {
                    if (s[j + h] != s[i + h])
                        break;
                }
                lcp[rnk[i] - 1] = h;
            }
            return lcp;
        }

        public static int[] lcpArray(char[] s, int[] sa) {
            int n = s.length;
            int[] s2 = new int[n];
            for (int i = 0; i < n; i++) {
                s2[i] = s[i];
            }
            return lcpArray(s2, sa);
        }

        public static int[] lcpArray(java.lang.String s, int[] sa) {
            return lcpArray(s.toCharArray(), sa);
        }

        public static int[] zAlgorithm(int[] s) {
            int n = s.length;
            if (n == 0)
                return new int[0];
            int[] z = new int[n];
            for (int i = 1, j = 0; i < n; i++) {
                int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]);
                while (i + k < n && s[k] == s[i + k])
                    k++;
                z[i] = k;
                if (j + z[j] < i + z[i])
                    j = i;
            }
            z[0] = n;
            return z;
        }

        public static int[] zAlgorithm(char[] s) {
            int n = s.length;
            if (n == 0)
                return new int[0];
            int[] z = new int[n];
            for (int i = 1, j = 0; i < n; i++) {
                int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]);
                while (i + k < n && s[k] == s[i + k])
                    k++;
                z[i] = k;
                if (j + z[j] < i + z[i])
                    j = i;
            }
            z[0] = n;
            return z;
        }

        public static int[] zAlgorithm(String s) {
            return zAlgorithm(s.toCharArray());
        }
    }

    static class ContestScanner {
        private final java.io.InputStream in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        private static final long LONG_MAX_TENTHS = 922337203685477580L;
        private static final int LONG_MAX_LAST_DIGIT = 7;
        private static final int LONG_MIN_LAST_DIGIT = 8;

        public ContestScanner(java.io.InputStream in) {
            this.in = in;
        }

        public ContestScanner() {
            this(System.in);
        }

        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (java.io.IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) {
                    return false;
                }
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[ptr++];
            else
                return -1;
        }

        private static boolean isPrintableChar(int c) {
            return 33 <= c && c <= 126;
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr]))
                ptr++;
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new java.util.NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public long nextLong() {
            if (!hasNext())
                throw new java.util.NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    int digit = b - '0';
                    if (n >= LONG_MAX_TENTHS) {
                        if (n == LONG_MAX_TENTHS) {
                            if (minus) {
                                if (digit <= LONG_MIN_LAST_DIGIT) {
                                    n = -n * 10 - digit;
                                    b = readByte();
                                    if (!isPrintableChar(b)) {
                                        return n;
                                    } else if (b < '0' || '9' < b) {
                                        throw new NumberFormatException(
                                                String.format("%d%s... is not number", n, Character.toString(b)));
                                    }
                                }
                            } else {
                                if (digit <= LONG_MAX_LAST_DIGIT) {
                                    n = n * 10 + digit;
                                    b = readByte();
                                    if (!isPrintableChar(b)) {
                                        return n;
                                    } else if (b < '0' || '9' < b) {
                                        throw new NumberFormatException(
                                                String.format("%d%s... is not number", n, Character.toString(b)));
                                    }
                                }
                            }
                        }
                        throw new ArithmeticException(
                                String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit));
                    }
                    n = n * 10 + digit;
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
                throw new NumberFormatException();
            return (int) nl;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public long[] nextLongArray(int length) {
            long[] array = new long[length];
            for (int i = 0; i < length; i++)
                array[i] = this.nextLong();
            return array;
        }

        public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
            long[] array = new long[length];
            for (int i = 0; i < length; i++)
                array[i] = map.applyAsLong(this.nextLong());
            return array;
        }

        public int[] nextIntArray(int length) {
            int[] array = new int[length];
            for (int i = 0; i < length; i++)
                array[i] = this.nextInt();
            return array;
        }

        public int[][] nextIntArrayMulti(int length, int width) {
            int[][] arrays = new int[width][length];
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++)
                    arrays[j][i] = this.nextInt();
            }
            return arrays;
        }

        public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
            int[] array = new int[length];
            for (int i = 0; i < length; i++)
                array[i] = map.applyAsInt(this.nextInt());
            return array;
        }

        public double[] nextDoubleArray(int length) {
            double[] array = new double[length];
            for (int i = 0; i < length; i++)
                array[i] = this.nextDouble();
            return array;
        }

        public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
            double[] array = new double[length];
            for (int i = 0; i < length; i++)
                array[i] = map.applyAsDouble(this.nextDouble());
            return array;
        }

        public long[][] nextLongMatrix(int height, int width) {
            long[][] mat = new long[height][width];
            for (int h = 0; h < height; h++)
                for (int w = 0; w < width; w++) {
                    mat[h][w] = this.nextLong();
                }
            return mat;
        }

        public int[][] nextIntMatrix(int height, int width) {
            int[][] mat = new int[height][width];
            for (int h = 0; h < height; h++)
                for (int w = 0; w < width; w++) {
                    mat[h][w] = this.nextInt();
                }
            return mat;
        }

        public double[][] nextDoubleMatrix(int height, int width) {
            double[][] mat = new double[height][width];
            for (int h = 0; h < height; h++)
                for (int w = 0; w < width; w++) {
                    mat[h][w] = this.nextDouble();
                }
            return mat;
        }

        public char[][] nextCharMatrix(int height, int width) {
            char[][] mat = new char[height][width];
            for (int h = 0; h < height; h++) {
                String s = this.next();
                for (int w = 0; w < width; w++) {
                    mat[h][w] = s.charAt(w);
                }
            }
            return mat;
        }
    }

    static class ContestPrinter extends java.io.PrintWriter {
        public ContestPrinter(java.io.PrintStream stream) {
            super(stream);
        }

        public ContestPrinter() {
            super(System.out);
        }

        private static String dtos(double x, int n) {
            StringBuilder sb = new StringBuilder();
            if (x < 0) {
                sb.append('-');
                x = -x;
            }
            x += Math.pow(10, -n) / 2;
            sb.append((long) x);
            sb.append(".");
            x -= (long) x;
            for (int i = 0; i < n; i++) {
                x *= 10;
                sb.append((int) x);
                x -= (int) x;
            }
            return sb.toString();
        }

        @Override
        public void print(float f) {
            super.print(dtos(f, 20));
        }

        @Override
        public void println(float f) {
            super.println(dtos(f, 20));
        }

        @Override
        public void print(double d) {
            super.print(dtos(d, 20));
        }

        @Override
        public void println(double d) {
            super.println(dtos(d, 20));
        }

        public void printArray(int[] array, String separator) {
            int n = array.length;
            for (int i = 0; i < n - 1; i++) {
                super.print(array[i]);
                super.print(separator);
            }
            super.println(array[n - 1]);
        }

        public void printArray(int[] array) {
            this.printArray(array, " ");
        }

        public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
            int n = array.length;
            for (int i = 0; i < n - 1; i++) {
                super.print(map.applyAsInt(array[i]));
                super.print(separator);
            }
            super.println(map.applyAsInt(array[n - 1]));
        }

        public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
            this.printArray(array, " ", map);
        }

        public void printArray(long[] array, String separator) {
            int n = array.length;
            for (int i = 0; i < n - 1; i++) {
                super.print(array[i]);
                super.print(separator);
            }
            super.println(array[n - 1]);
        }

        public void printArray(long[] array) {
            this.printArray(array, " ");
        }

        public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
            int n = array.length;
            for (int i = 0; i < n - 1; i++) {
                super.print(map.applyAsLong(array[i]));
                super.print(separator);
            }
            super.println(map.applyAsLong(array[n - 1]));
        }

        public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
            this.printArray(array, " ", map);
        }

    }
}
0