結果

問題 No.2313 Product of Subsequence (hard)
ユーザー dango
提出日時 2023-05-24 21:23:16
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 1,348 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 113,220 KB
実行使用メモリ 381,124 KB
最終ジャッジ日時 2024-12-23 21:39:10
合計ジャッジ時間 11,139 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 RE * 24 TLE * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main(string[] args)
    {
        var input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int n = input[0];
        int k = input[1];
        var a = Console.ReadLine().Split().Select(int.Parse).ToList();
        var dp = new List<Dictionary<int, int>>();
        for (int i = 0; i <= n; i++)
            dp.Add(new Dictionary<int, int>());
        dp[0][1] = 1;
        for (int x = 1; x <= n; x++)
        {
            int ai = Gcd(a[x - 1], k);
            foreach (var y in dp[x - 1].Keys.ToList())
            {
                if (!dp[x].ContainsKey(y))
                    dp[x][y] = 0;
                dp[x][y] += dp[x - 1][y];
                dp[x][y] %= 998244353;
                int z = Gcd(y * ai, k);
                if (!dp[x].ContainsKey(z))
                    dp[x][z] = 0;
                dp[x][z] += dp[x - 1][y];
                dp[x][z] %= 998244353;
            }
        }
        int ans = dp[n][k];
        if (k == 1)
            ans -= 1;
        Console.WriteLine(ans);
    }

    static int Gcd(int a, int b)
    {
        if (a < b)
            return Gcd(b, a);
        while (b != 0)
        {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }
}
0