티스토리 뷰

알고리즘/Java

[SWEA] 9659 다항식 계산

세진짱 2020. 4. 3. 20:29

 

문제를 푸는것보다 이해하는게 더 어려운 문제같다

문제를 풀려면 그냥 쓰여져있는대로 코딩하면 된다!

 

하라는대로!!

 

a와 b가 i-1이하기 때문에 무조건 이전에 계산한 식을 통해 값을 구할 수 있다

그럼 for문을 통해 N-1개의 값들을 먼저 저장하고 그걸 이용해서 f(x)값을 구해보자!

 

소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Scanner;
 
public class Solution {
    static class Node {
        int tx,ax,bx;
        Node(int _tx,int _ax,int _bx){
            this.tx=_tx;
            this.ax=_ax;
            this.bx=_bx;
        }
    }
 
    public static final long mod = 998244353;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int tc=1;tc<=t;tc++) {
            int n = sc.nextInt();
            Node[] arr = new Node[n+1];
            for(int i=2;i<=n;i++) {
                int tx = sc.nextInt();
                int ax = sc.nextInt();
                int bx = sc.nextInt();
                Node nNode = new Node(tx,ax,bx);
                arr[i]=nNode;
            }
            
            int m = sc.nextInt();
            long[] ans = new long[m+1];
            for(int i=0;i<m;i++) {
                int x = sc.nextInt();
                long[] num = new long[n+1];
                num[0]=1; num[1]=x;
                
                for(int j=2;j<=n;j++) {
                    if(arr[j].tx==1) num[j] = num[arr[j].ax]+num[arr[j].bx];
                    else if(arr[j].tx==2) num[j]=arr[j].ax*num[arr[j].bx];
                    else num[j]=num[arr[j].ax]*num[arr[j].bx];
                    num[j]%=mod;
                }
                ans[i]=num[n];
            }
            System.out.print("#"+tc+" ");
            for(int j=0;j<m;j++System.out.print(ans[j]+" ");
            System.out.println();
        }
    }
}
 
 
 

'알고리즘 > Java' 카테고리의 다른 글

[SWEA] 4050 재관이의 대량 할인  (0) 2020.04.29
[백준] 16234 인구이동  (0) 2020.04.24
[SWEA] 1244 최대상금  (0) 2020.02.17
[JUNGOL] 1828 냉장고  (0) 2020.02.12
[SWEA] 1233 사칙연산 유효성 검사  (0) 2020.02.11
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함