結果

問題 No.1463 Hungry Kanten
ユーザー tentententen
提出日時 2023-04-17 13:44:46
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 3,839 bytes
コンパイル時間 3,327 ms
コンパイル使用メモリ 96,228 KB
実行使用メモリ 762,240 KB
最終ジャッジ日時 2024-10-12 15:29:06
合計ジャッジ時間 8,196 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,464 KB
testcase_01 AC 53 ms
37,052 KB
testcase_02 RE -
testcase_03 TLE -
testcase_04 AC 118 ms
54,480 KB
testcase_05 RE -
testcase_06 AC 53 ms
37,260 KB
testcase_07 AC 56 ms
37,904 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 107 ms
44,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] values = new int[n];
        Num[] nums = new Num[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            nums[i] = new Num(values[i]);
        }
        int[] dp1 = new int[1 << n];
        Num[] dp2 = new Num[1 << n];
        dp2[0] = new Num(0);
        HashSet<Num> ans = new HashSet<>();
        for (int i = 1; i < (1 << n); i++) {
            int pop = getPop(i);
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                dp1[i] = dp1[i ^ (1 << j)] + values[j];
                dp2[i] = dp2[i ^ (1 << j)].multiply(nums[j]);
                break;
            }
            if (pop >= k) {
                ans.add(new Num(dp1[i]));
                ans.add(dp2[i]);
            }
        }
        System.out.println(ans.size());
    }
    
    static int getPop(long x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
    
    static class Num {
        int[] counts = new int[1800];
        
        public Num(int x) {
            for (int i = 2; i <= Math.sqrt(x); i++) {
                while (x % i == 0) {
                    counts[i]++;
                    x /= i;
                }
            }
            if (x > 1) {
                counts[x]++;
            }
        }
        
        public Num multiply(Num x) {
            Num ans = new Num(0);
            for (int i = 0; i < counts.length; i++) {
                ans.counts[i] = counts[i] + x.counts[i];
            }
            return ans;
        }
        
        public int hashCode() {
            return counts[2];
        }
        
        public boolean equals(Object o) {
            Num x = (Num)o;
            for (int i = 0; i < counts.length; i++) {
                if (counts[i] != x.counts[i]) {
                    return false;
                }
            }
            return true;
        }
        
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < counts.length; i++) {
                if (counts[i] > 0) {
                    sb.append("[").append(i).append(":").append(counts[i]).append("]");
                }
            }
            return sb.toString();
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0