結果

問題 No.623 fudan no modulus to tigau
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-04 00:28:25
言語 Java19
(openjdk 21)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,204 bytes
コンパイル時間 1,910 ms
コンパイル使用メモリ 73,836 KB
実行使用メモリ 49,768 KB
最終ジャッジ日時 2023-08-19 02:18:35
合計ジャッジ時間 3,605 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,412 KB
testcase_01 AC 42 ms
49,476 KB
testcase_02 AC 43 ms
49,760 KB
testcase_03 AC 42 ms
49,492 KB
testcase_04 AC 41 ms
49,768 KB
testcase_05 AC 41 ms
49,592 KB
testcase_06 AC 43 ms
49,380 KB
testcase_07 AC 42 ms
49,556 KB
testcase_08 AC 44 ms
49,620 KB
testcase_09 AC 43 ms
49,472 KB
testcase_10 AC 42 ms
49,624 KB
testcase_11 AC 43 ms
49,424 KB
testcase_12 AC 41 ms
47,916 KB
testcase_13 AC 40 ms
49,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
    static final long MOD=998244353;
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int[]a=new int[n+1],b=new int[n+1],c=new int[n+1];
        for(int i=2;i<=n;++i){
            c[i]=sc.nextInt();
            a[i]=sc.nextInt();
            b[i]=sc.nextInt();
        }
        int q=sc.nextInt();
        while(q-->0){
            long x=sc.nextInt();
            long[]dp=new long[n+1];
            dp[0]=1;
            dp[1]=x;
            for(int i=2;i<=n;++i){
                if(c[i]==1)
                    dp[i]=(dp[a[i]]+dp[b[i]])%MOD;
                else if(c[i]==2)
                    dp[i]=a[i]*dp[b[i]]%MOD;
                else
                    dp[i]=dp[a[i]]*dp[b[i]]%MOD;
            }
            out.println(dp[n]);
        }
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
0