結果

問題 No.1463 Hungry Kanten
ユーザー neko_the_shadowneko_the_shadow
提出日時 2021-04-06 09:12:22
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 7,043 bytes
コンパイル時間 3,308 ms
コンパイル使用メモリ 85,304 KB
実行使用メモリ 74,248 KB
最終ジャッジ日時 2024-06-10 20:01:12
合計ジャッジ時間 7,120 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
37,632 KB
testcase_01 AC 55 ms
37,496 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 796 ms
74,248 KB
testcase_06 AC 59 ms
37,612 KB
testcase_07 AC 56 ms
37,420 KB
testcase_08 AC 59 ms
37,672 KB
testcase_09 AC 56 ms
37,384 KB
testcase_10 AC 65 ms
38,036 KB
testcase_11 AC 67 ms
38,528 KB
testcase_12 AC 79 ms
39,272 KB
testcase_13 AC 64 ms
38,408 KB
testcase_14 AC 108 ms
41,436 KB
testcase_15 AC 84 ms
38,892 KB
testcase_16 AC 137 ms
44,332 KB
testcase_17 AC 201 ms
45,940 KB
testcase_18 AC 130 ms
43,980 KB
testcase_19 AC 436 ms
56,416 KB
testcase_20 AC 474 ms
58,472 KB
testcase_21 AC 80 ms
38,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package _1463;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class Main {
    public void exec() {
        int n = stdin.nextInt();
        int k = stdin.nextInt();
        List<Long> a = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            a.add(stdin.nextLong());
        }
        
        Set<Long> d = new HashSet<>();
        for (int x = k; x <= n; x++) {
            for (List<Long> c : new Combinations<>(a, x)) {
                d.add(c.stream().reduce((p, q) -> p*q).get());
                d.add(c.stream().reduce((p, q) -> p+q).get());
            }
        }
        stdout.println(d.size());
    }

    private static final Stdin stdin = new Stdin(System.in);
    private static final Stdout stdout = new Stdout(System.out);
    private static final Stderr stderr = new Stderr(System.err, false);

    public static void main(String[] args) {
        try {
            new Main().exec();
        } finally {
            stdout.flush();
        }
    }
    
    public class Combinations<T> implements Iterable<List<T>>{
        private List<T> src;
        private int n;

        public Combinations(List<T> src, int n) {
            this.src = src;
            this.n = n;
        }

        @Override
        public Iterator<List<T>> iterator() {
            return new CombinationsIterator<>(src, n);
        }

        private class CombinationsIterator<S> implements Iterator<List<S>> {
            private List<S> src;
            private int m;

            private int len;
            private int bit;
            private List<S> ptr;

            public CombinationsIterator(List<S> src, int m) {
                this.src = src;
                this.len = src.size();
                this.m = m;
                this.bit = 0;
            }

            @Override
            public boolean hasNext() {
                while (bit < (1<<len)) {
                    if (m<=0 || Integer.bitCount(bit)==m) {
                        this.ptr = new ArrayList<>();
                        for (int i = 0; i < len; i++) {
                            if ((bit&(1<<i))!=0) ptr.add(src.get(i));
                        }
                        bit++;
                        return true;
                    } else {
                        bit++;
                    }
                }
                return false;
            }

            @Override
            public List<S> next() {
                return this.ptr;
            }
        }
    }

    // ASCII ONLY
    public static class Stdin {
        private InputStream in;
        private byte[] buf;
        private int ptr;
        private int len;

        public Stdin(InputStream in) {
            this.in = in;
            this.buf = new byte[1024];
            this.ptr = 0;
            this.len = 0;
        }

        public String nextString() {
            StringBuilder sb = new StringBuilder();
            byte b;
            while ((b = read()) != -1) {
                sb.appendCodePoint(b);
            }
            return sb.toString();
        }

        public int nextInt() {
            return (int)nextLong();
        }

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

        public long nextLong() {
            boolean negative = false;
            int x = 0;

            byte b = read();
            if (b == '-') {
                negative = true;
            } else {
                x += b-'0';
            }

            while ((b=read()) != -1) {
                x *= 10;
                x += b-'0';
            }

            return negative ? -x : x;
        }

        private byte read() {
            byte b = readByte();
            if (b == '\r') {
                readByte(); // LFを読み飛ばす
                return -1;
            } else if (b == '\n' || b == ' ') {
                return -1;
            } else {
                return b;
            }
        }

        private byte readByte(){
            if (len == ptr) {
                try {
                    ptr = 0;
                    len = in.read(buf);
                    if (len == -1) return -1;
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return buf[ptr++];
        }

        public String[] nextStringArray(int n) {
            String[] a = new String[n];
            for (int i = 0; i < n; i++) a[i] = nextString();
            return a;
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public double[] nextDoubleArray(int n) {
            double[] a = new double[n];
            for (int i = 0; i < n; i++) a[i] = nextDouble();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
    }

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout(PrintStream stdout) {
            this.stdout =  new PrintWriter(stdout, false);
        }

        public void println(Object ... objs) {
            for (int i = 0, len = objs.length; i < len; i++) {
                stdout.print(objs[i]);
                if (i != len-1) stdout.print(' ');
            }
            stdout.println();
        }

        public void flush() {
            stdout.flush();
        }
    }

    public static class Stderr {
        private PrintWriter stderr;
        private boolean debug;

        public Stderr(PrintStream stderr, boolean debug) {
            this.stderr =  new PrintWriter(stderr, false);
            this.debug = debug;
        }

        public void println(Object ... objs) {
            if (!debug) return ;

            stderr.print("DEBUG: ");
            for (int i = 0, len = objs.length; i < len; i++) {
                stderr.print(deepToString(objs[i]));
                if (i != len-1) stderr.print(' ');
            }
            stderr.println();
            stderr.flush();
        }

        private String deepToString(Object o) {
            if (o == null) {
                return "null";
            }

            // 配列の場合
            if (o.getClass().isArray()) {
                int len = Array.getLength(o);
                String[] tokens = new String[len];
                for (int i = 0; i < len; i++) {
                    tokens[i] = deepToString(Array.get(o, i));
                }
                return "{" + String.join(", ", tokens) + "}";
            }

            return Objects.toString(o);
        }
    }
}
0