結果

提出番号 1926
提出者 tanzaku
言語 Java
提出日時 2018-08-04 14:36:53
問題名 (72)K-th DigitSum
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 151ms 130032KB
2 AC 100% 119ms 129760KB
3 AC 100% 114ms 130304KB
4 AC 100% 96ms 129936KB
5 AC 100% 143ms 129520KB
6 AC 100% 134ms 129104KB
7 AC 100% 120ms 130480KB
8 AC 100% 110ms 129136KB
9 AC 100% 149ms 129328KB
10 AC 100% 154ms 130480KB
11 AC 100% 90ms 128112KB
12 AC 100% 116ms 129728KB
13 AC 100% 98ms 129776KB
14 AC 100% 143ms 128944KB
15 AC 100% 137ms 129408KB
16 AC 100% 130ms 130032KB
17 AC 100% 138ms 129584KB
18 AC 100% 130ms 128960KB
19 AC 100% 100ms 130224KB
20 AC 100% 120ms 129728KB
21 AC 100% 133ms 129072KB
22 AC 100% 103ms 129872KB
23 AC 100% 134ms 130304KB
24 AC 100% 84ms 129584KB
25 AC 100% 82ms 130048KB
26 AC 100% 87ms 129312KB
27 AC 100% 136ms 129632KB
28 AC 100% 148ms 129520KB
29 AC 100% 112ms 129648KB
30 AC 100% 145ms 129680KB
31 AC 100% 150ms 129600KB
32 AC 100% 111ms 129776KB
33 AC 100% 115ms 129680KB
34 AC 100% 127ms 129424KB
35 AC 100% 127ms 129136KB
36 AC 100% 147ms 130016KB
37 AC 100% 123ms 130368KB
38 AC 100% 103ms 130000KB
39 AC 100% 140ms 129984KB
40 AC 100% 120ms 128960KB
41 AC 100% 81ms 128880KB
42 AC 100% 144ms 129760KB
43 AC 100% 96ms 130288KB
44 AC 100% 119ms 131024KB
45 AC 100% 126ms 129632KB
46 AC 100% 127ms 130224KB
47 AC 100% 100ms 129360KB
48 AC 100% 97ms 129200KB
49 AC 100% 137ms 129584KB
50 AC 100% 113ms 130144KB
51 AC 100% 110ms 129088KB

ソースコード

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.InputStreamReader;
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 = System.in;
        OutputStream outputStream = System.out;
        MyInput in = new MyInput(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskF solver = new TaskF();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskF {
        long[][] dp = new long[1001][1001];

        public void solve(int testNumber, MyInput in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt() - 1;

            for (long[] d : dp) Arrays.fill(d, -1L);

            char[] ans = new char[1001];
            Arrays.fill(ans, '0');
            for (int i = 1000; i > 0; i--) {
                for (int j = 0; j < 10; j++) {
                    final long v = rec(i - 1, n - j);
                    if (v > k || j == 9 || j == n) {
                        ans[ans.length - i] = (char) (j + '0');
                        n -= j;
                        break;
                    }
                    k -= v;
                }
            }
            for (int i = 0; i < ans.length; i++) {
                if (ans[i] != '0') {
                    out.println(new String(ans, i, ans.length - i));
                    break;
                }
            }
        }

        long rec(int rest, int sum) {
            if (sum < 0 || rest < 0) return 0;
            if (rest == 0) return sum == 0 ? 1 : 0;
            if (dp[rest][sum] < 0) {
                dp[rest][sum] = 0;
                for (int i = 0; i < 10; i++) {
                    dp[rest][sum] += rec(rest - 1, sum - i);
                }
                dp[rest][sum] = Math.min(Integer.MAX_VALUE, dp[rest][sum]);
            }
            return dp[rest][sum];
        }

    }

    static class MyInput {
        private final BufferedReader in;
        private static int pos;
        private static int readLen;
        private static final char[] buffer = new char[1024 * 8];
        private static char[] str = new char[500 * 8 * 2];
        private static boolean[] isDigit = new boolean[256];
        private static boolean[] isSpace = new boolean[256];
        private static boolean[] isLineSep = new boolean[256];

        static {
            for (int i = 0; i < 10; i++) {
                isDigit['0' + i] = true;
            }
            isDigit['-'] = true;
            isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
            isLineSep['\r'] = isLineSep['\n'] = true;
        }

        public MyInput(InputStream is) {
            in = new BufferedReader(new InputStreamReader(is));
        }

        public int read() {
            if (pos >= readLen) {
                pos = 0;
                try {
                    readLen = in.read(buffer);
                } catch (IOException e) {
                    throw new RuntimeException();
                }
                if (readLen <= 0) {
                    throw new MyInput.EndOfFileRuntimeException();
                }
            }
            return buffer[pos++];
        }

        public int nextInt() {
            int len = 0;
            str[len++] = nextChar();
            len = reads(len, isSpace);
            int i = 0;
            int ret = 0;
            if (str[0] == '-') {
                i = 1;
            }
            for (; i < len; i++) ret = ret * 10 + str[i] - '0';
            if (str[0] == '-') {
                ret = -ret;
            }
            return ret;
        }

        public char nextChar() {
            while (true) {
                final int c = read();
                if (!isSpace[c]) {
                    return (char) c;
                }
            }
        }

        int reads(int len, boolean[] accept) {
            try {
                while (true) {
                    final int c = read();
                    if (accept[c]) {
                        break;
                    }
                    if (str.length == len) {
                        char[] rep = new char[str.length * 3 / 2];
                        System.arraycopy(str, 0, rep, 0, str.length);
                        str = rep;
                    }
                    str[len++] = (char) c;
                }
            } catch (MyInput.EndOfFileRuntimeException e) {
            }
            return len;
        }

        static class EndOfFileRuntimeException extends RuntimeException {
        }

    }
}