結果

問題 No.1731 Product of Subsequence
ユーザー tentententen
提出日時 2021-11-29 19:35:28
言語 Java19
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,455 bytes
コンパイル時間 2,347 ms
コンパイル使用メモリ 74,720 KB
実行使用メモリ 287,876 KB
最終ジャッジ日時 2023-08-07 23:43:42
合計ジャッジ時間 26,669 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,984 ms
270,568 KB
testcase_01 AC 44 ms
49,784 KB
testcase_02 AC 43 ms
49,800 KB
testcase_03 AC 43 ms
49,428 KB
testcase_04 AC 43 ms
47,336 KB
testcase_05 AC 53 ms
50,636 KB
testcase_06 AC 56 ms
50,124 KB
testcase_07 AC 47 ms
49,636 KB
testcase_08 AC 133 ms
60,324 KB
testcase_09 AC 42 ms
49,484 KB
testcase_10 TLE -
testcase_11 AC 1,814 ms
258,532 KB
testcase_12 AC 102 ms
53,008 KB
testcase_13 AC 169 ms
61,004 KB
testcase_14 AC 1,533 ms
230,028 KB
testcase_15 AC 43 ms
49,304 KB
testcase_16 AC 43 ms
49,804 KB
testcase_17 AC 44 ms
49,532 KB
testcase_18 TLE -
testcase_19 AC 162 ms
60,864 KB
testcase_20 AC 1,612 ms
202,852 KB
testcase_21 TLE -
testcase_22 AC 97 ms
53,048 KB
testcase_23 AC 43 ms
49,800 KB
testcase_24 AC 134 ms
58,120 KB
testcase_25 AC 113 ms
56,120 KB
testcase_26 AC 491 ms
103,812 KB
testcase_27 AC 685 ms
109,488 KB
testcase_28 AC 905 ms
159,292 KB
testcase_29 AC 569 ms
103,124 KB
testcase_30 TLE -
testcase_31 AC 98 ms
55,064 KB
testcase_32 AC 42 ms
49,356 KB
testcase_33 AC 94 ms
52,716 KB
testcase_34 AC 285 ms
71,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> dp = new ArrayList<>();
    static int[] values;
    static int k;
    static final int MOD = 1000000007;
    static HashMap<Integer, HashMap<Integer, Integer>> gcdDP = new HashMap<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        if (k == 1) {
            System.out.println(pow(2, n) - 1);
            return;
        }
        values = new int[n];
        for (int i = 0; i < n; i++) {
            dp.add(new HashMap<>());
            values[i] = (int)getGCD(sc.nextLong(), k);
        }
        System.out.println((pow(2, n) - dfw(n - 1, k) + MOD) % MOD);
    }
    
    static int dfw(int idx, int key) {
        if (key == 1) {
            return 0;
        }
        if (idx < 0) {
            return 1;
        }
        if (!dp.get(idx).containsKey(key)) {
            int ans = (dfw(idx - 1, key) + dfw(idx - 1, key / getGCD(key, values[idx]))) % MOD;
            dp.get(idx).put(key, ans);
        }
        return dp.get(idx).get(key);
    }
    
    static long getGCD(long x, long y) {
        if (x % y == 0) {
            return y;
        } else {
            return getGCD(y, x % y);
        }
    }
    
    static int getGCD(int x, int y) {
            if (x % y == 0) {
                return y;
            } else {
                return getGCD(y, x % y);
            }
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0