結果

問題 No.1731 Product of Subsequence
ユーザー tentententen
提出日時 2021-11-29 19:14:28
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,098 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 75,140 KB
実行使用メモリ 291,484 KB
最終ジャッジ日時 2023-09-15 09:05:52
合計ジャッジ時間 14,718 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 44 ms
49,484 KB
testcase_02 AC 45 ms
49,844 KB
testcase_03 AC 46 ms
49,552 KB
testcase_04 AC 46 ms
49,652 KB
testcase_05 AC 55 ms
50,412 KB
testcase_06 AC 57 ms
50,264 KB
testcase_07 AC 48 ms
49,576 KB
testcase_08 AC 136 ms
57,864 KB
testcase_09 WA -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 105 ms
54,256 KB
testcase_13 AC 178 ms
61,168 KB
testcase_14 AC 1,883 ms
244,596 KB
testcase_15 AC 44 ms
49,432 KB
testcase_16 WA -
testcase_17 AC 49 ms
47,532 KB
testcase_18 TLE -
testcase_19 AC 176 ms
58,832 KB
testcase_20 AC 1,901 ms
217,604 KB
testcase_21 TLE -
testcase_22 AC 99 ms
55,052 KB
testcase_23 WA -
testcase_24 AC 138 ms
57,752 KB
testcase_25 AC 113 ms
56,600 KB
testcase_26 AC 544 ms
102,636 KB
testcase_27 AC 828 ms
112,320 KB
testcase_28 AC 1,193 ms
167,764 KB
testcase_29 AC 690 ms
108,320 KB
testcase_30 TLE -
testcase_31 AC 96 ms
52,716 KB
testcase_32 WA -
testcase_33 AC 102 ms
53,216 KB
testcase_34 AC 343 ms
80,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Long, Integer>> dp = new ArrayList<>();
    static int[] values;
    static int k;
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        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, long 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 / (int)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 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