結果

問題 No.1463 Hungry Kanten
ユーザー tentententen
提出日時 2023-08-04 08:56:41
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,797 bytes
コンパイル時間 3,187 ms
コンパイル使用メモリ 92,420 KB
実行使用メモリ 125,228 KB
最終ジャッジ日時 2024-10-14 05:58:34
合計ジャッジ時間 9,834 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
57,204 KB
testcase_01 AC 56 ms
50,364 KB
testcase_02 AC 1,237 ms
68,904 KB
testcase_03 WA -
testcase_04 AC 84 ms
51,652 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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[] inputs = new Num[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            inputs[i] = new Num(values[i]);
        }
        int[] sums = new int[1 << n];
        Num[] nums = new Num[1 << n];
        nums[0] = new Num(1);
        HashSet<Num> ans = new HashSet<>();
        for (int i = 1; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) > 0) {
                    sums[i] = sums[i ^ (1 << j)] + values[j];
                    nums[i] = nums[i ^ (1 << j)].multiply(inputs[j]);
                    break;
                }
            }
            if (getPop(i) >= k) {
                ans.add(new Num(sums[i]));
                ans.add(nums[i]);
            }
        }
        System.out.println(ans.size());
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
    
    static class Num {
        HashMap<Integer, Integer> count = new HashMap<>();
        
        public Num(int x) {
            for (int i = 2; i <= Math.sqrt(x); i++) {
                while (x % i == 0) {
                    count.put(i, count.getOrDefault(i, 0) + 1);
                    x /= i;
                }
            }
            if (x > 1) {
                count.put(x, count.getOrDefault(x, 0) + 1);
            }
        }
        
        public int hashCode() {
            return count.size();
        }
        
        public Num multiply(Num another) {
            Num ans = new Num(1);
            for (int x : count.keySet()) {
                ans.count.put(x, count.get(x));
            }
            for (int x : another.count.keySet()) {
                ans.count.put(x, ans.count.getOrDefault(x, 0) + another.count.get(x));
            }
            return ans;
        }
        
        public boolean equals(Object o) {
            Num z = (Num)o;
            if (z.count.size() != count.size()) {
                return false;
            }
            for (int x : count.keySet()) {
                if (!z.count.containsKey(x) || count.get(x) != z.count.get(x)) {
                    return false;
                }
            }
            return true;
        }
    }
} 
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