実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
整数 A, B が与えられます。 A^B の値を出力してください。
制約
- 1 \leq A, B \leq 9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
答えを出力せよ。
入力例 1
4 3
出力例 1
64
4^3 = 64 であるので、64 を出力します。
入力例 2
5 5
出力例 2
3125
入力例 3
8 1
出力例 3
8
Score : 100 points
Problem Statement
Given integers A and B, print the value A^B.
Constraints
- 1 \leq A, B \leq 9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print the answer.
Sample Input 1
4 3
Sample Output 1
64
4^3 = 64, so 64 should be printed.
Sample Input 2
5 5
Sample Output 2
3125
Sample Input 3
8 1
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の英小文字からなる文字列 S と英小文字 c_1,c_2 が与えられます。
S の文字のうち c_1 であるもの 以外 を全て c_2 に置き換えた文字列を求めてください。
制約
- 1\le N\le 100
- N は整数
- c_1,c_2 は英小文字
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N c_1 c_2 S
出力
答えを出力せよ。
入力例 1
3 b g abc
出力例 1
gbg
S= abc のうち、 b でない a と c を g に置き換えた結果 gbg になります。したがって、 gbg を出力してください。
入力例 2
1 s h s
出力例 2
s
置き換えた後の文字列が元の文字列と変わらない場合もあります。
入力例 3
7 d a atcoder
出力例 3
aaaadaa
入力例 4
10 b a acaabcabba
出力例 4
aaaabaabba
Score : 100 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters, along with lowercase English letters c_1 and c_2.
Find the string obtained by replacing every character of S that is not c_1 with c_2.
Constraints
- 1\le N\le 100
- N is an integer.
- c_1 and c_2 are lowercase English letters.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given in the following format from Standard Input:
N c_1 c_2 S
Output
Print the answer.
Sample Input 1
3 b g abc
Sample Output 1
gbg
Replacing a and c (which are not b) with g in S= abc results in gbg, so print gbg.
Sample Input 2
1 s h s
Sample Output 2
s
It is possible that the resulting string after replacement is the same as the original string.
Sample Input 3
7 d a atcoder
Sample Output 3
aaaadaa
Sample Input 4
10 b a acaabcabba
Sample Output 4
aaaabaabba
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は電卓を持っています。電卓には最初 1 が表示されています。
高橋君は電卓に対して N 回操作を行います。
i 回目 (1\leq i\leq N) の操作では、その時点で画面に表示されている数に正の整数 A_i をかけます。
しかし、電卓には K 桁までしか表示できないため、計算結果が (K+1) 桁以上になる場合、代わりに 1 が画面に表示されます。
そうでない場合は正しく計算結果が表示されます。
N 回の操作の後に電卓に表示されている数を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq K \leq 18
- 1 \leq A_i < 10^K
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
N 回の操作の後に電卓に表示されている数を出力せよ。
入力例 1
5 2 7 13 3 2 5
出力例 1
10
今回電卓は 2 桁まで表示することができ、最初 1 が表示されています。これに対して、次のように高橋君は操作を行います。
- 1 回目の操作で、7 をかけます。1\times 7=7 であり、電卓には 7 が表示されます。
- 2 回目の操作で、13 をかけます。7\times 13=91 であり、電卓には 91 が表示されます。
- 3 回目の操作で、3 をかけます。91\times 3=273 であり、3 桁になってしまうため、電卓には 1 が表示されます。
- 4 回目の操作で、2 をかけます。1\times 2=2 であり、電卓には 2 が表示されます。
- 5 回目の操作で、5 をかけます。2\times 5=10 であり、電卓には 10 が表示されます。
入力例 2
2 1 2 5
出力例 2
1
Score : 200 points
Problem Statement
Takahashi has a calculator that initially displays 1.
He will perform N operations on the calculator.
In the i-th operation (1 \leq i \leq N), he multiplies the currently displayed number by a positive integer A_i.
However, the calculator can display at most K digits.
If the result of the multiplication has (K+1) or more digits, the display shows 1 instead; otherwise, the result is shown correctly.
Find the number showing on the calculator after the N operations.
Constraints
- 1 \leq N \leq 100
- 1 \leq K \leq 18
- 1 \leq A_i < 10^K
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Output the number shown on the calculator after the N operations.
Sample Input 1
5 2 7 13 3 2 5
Sample Output 1
10
This calculator can display at most two digits and initially shows 1. Takahashi operates as follows:
- The 1st operation multiplies by 7. Since 1 \times 7 = 7, the calculator shows 7.
- The 2nd operation multiplies by 13. Since 7 \times 13 = 91, the calculator shows 91.
- The 3rd operation multiplies by 3. Since 91\times 3=273, which has three digits, the calculator shows 1.
- The 4th operation multiplies by 2. Since 1\times 2=2, the calculator shows 2.
- The 5th operation multiplies by 5. Since 2\times 5=10, the calculator shows 10.
Sample Input 2
2 1 2 5
Sample Output 2
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋くんは回転寿司で N 皿の料理を食べました。 i 皿目の色は文字列 C_i で表されます。
また、料理の価格は皿の色と対応し、 i=1,\ldots,M のそれぞれについて、色が文字列 D_i の皿の料理は一皿 P_i 円です。また、D_1,\ldots,D_M のいずれとも異なる色の皿の料理は一皿 P_0 円です。
高橋くんが食べた料理の価格の合計を求めてください。
制約
- 1\leq N,M\leq 100
- C_i,D_i は英小文字からなる長さ 1 以上 20 以下の文字列
- D_1,\ldots,D_M はすべて異なる
- 1\leq P_i\leq 10000
- N,M,P_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N M C_1 \ldots C_N D_1 \ldots D_M P_0 P_1 \ldots P_M
出力
答えを整数として出力せよ。
入力例 1
3 2 red green blue blue red 800 1600 2800
出力例 1
5200
blue の皿は P_1 = 1600 円、red の皿は P_2 = 2800 円、green の皿は P_0 = 800 円です。
高橋くんが食べた料理の価格の合計は、2800+800+1600=5200 円です。
入力例 2
3 2 code queen atcoder king queen 10 1 1
出力例 2
21
Score : 200 points
Problem Statement
Takahashi ate N plates of sushi at a sushi restaurant. The color of the i-th plate is represented by a string C_i.
The price of a sushi corresponds to the color of the plate. For each i=1,\ldots,M, the sushi on a plate whose color is represented by a string D_i is worth P_i yen a plate (yen is the currency of Japan). If the color does not coincide with any of D_1,\ldots, and D_M, it is worth P_0 yen a plate.
Find the total amount of the prices of sushi that Takahashi ate.
Constraints
- 1\leq N,M\leq 100
- C_i and D_i are strings of length between 1 and 20, inclusive, consisting of lowercase English letters.
- D_1,\ldots, and D_M are distinct.
- 1\leq P_i\leq 10000
- N, M, and P_i are integers.
Input
The input is given from Standard Input in the following format:
N M C_1 \ldots C_N D_1 \ldots D_M P_0 P_1 \ldots P_M
Output
Print the answer as an integer.
Sample Input 1
3 2 red green blue blue red 800 1600 2800
Sample Output 1
5200
A blue plate, red plate, and green plate are worth P_1 = 1600, P_2 = 2800, and P_0 = 800 yen, respectively.
The total amount of the prices of the sushi that he ate is 2800+800+1600=5200 yen.
Sample Input 2
3 2 code queen atcoder king queen 10 1 1
Sample Output 2
21
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
あなたは以下の操作を 0 回以上好きな順番で好きな回数行うことができます:
- A_k=A_{k+1}=A_{k+2}=A_{k+3} を満たす 1 以上 |A|-3 以下の整数 k を選び、A から A_k,A_{k+1},A_{k+2},A_{k+3} を削除する。(より厳密には、 A を (A_1,A_2,\ldots,A_{k-1},A_{k+4},A_{k+5},\ldots,A_N) に置き換える。)
ここで、 |A| は整数列 A の長さを表します。
操作を繰り返した後の最終的な |A| としてあり得る最小値を求めてください。
制約
- 1\le N\le 2\times 10^5
- 1\le A_i\le N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
操作を繰り返した後の最終的な |A| としてあり得る最小値を出力せよ。
入力例 1
10 1 1 1 4 4 4 4 1 2 3
出力例 1
2
以下のように 2 回操作することで |A|=2 にすることができます。
- k=4 を選ぶ。A_4=A_5=A_6=A_7=4 が成り立つためこの選択は正当である。 A=(1,1,1,1,2,3) となる。
- k=1 を選ぶ。A_1=A_2=A_3=A_4=1 が成り立つためこの選択は正当である。 A=(2,3) となる。
|A| を 2 未満にすることはできないため、 2 を出力してください。
入力例 2
3 2 1 3
出力例 2
3
はじめから操作を行うことができません。
入力例 3
13 1 1 4 4 4 1 1 1 1 4 1 4 1
出力例 3
5
Score : 300 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
You can perform the following operation zero or more times in any order:
- Choose an integer k between 1 and |A|-3, inclusive, such that A_k=A_{k+1}=A_{k+2}=A_{k+3}, and remove A_k,A_{k+1},A_{k+2},A_{k+3} from A. (More formally, replace A with (A_1,A_2,\ldots,A_{k-1},A_{k+4},A_{k+5},\ldots,A_N).)
Here, |A| represents the length of the integer sequence A.
Find the minimum possible value of the final |A| after repeating the operation.
Constraints
- 1\le N\le 2\times 10^5
- 1\le A_i\le N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Output the minimum possible value of the final |A| after repeating the operation.
Sample Input 1
10 1 1 1 4 4 4 4 1 2 3
Sample Output 1
2
You can make |A|=2 with two operations as follows:
- Choose k=4. This choice is valid since A_4=A_5=A_6=A_7=4 holds. A=(1,1,1,1,2,3) is obtained.
- Choose k=1. This choice is valid since A_1=A_2=A_3=A_4=1 holds. A=(2,3) is obtained.
It is impossible to make |A| less than 2, so output 2.
Sample Input 2
3 2 1 3
Sample Output 2
3
You cannot perform any operation from the beginning.
Sample Input 3
13 1 1 4 4 4 1 1 1 1 4 1 4 1
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
整数列 A=(A_1,A_2,\dots,A_N) があります。 あなたは次の操作を好きな回数(0 回でもよい)行うことができます。
- 1\leq i,j \leq N を満たす整数 i,j を選ぶ。A_i を 1 減らし、A_j を 1 増やす。
A の最小値と最大値の差を 1 以下にするために必要な最小の操作回数を求めてください。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
4 4 7 3 7
出力例 1
3
以下のように 3 回の操作を行うことで、A の最小値と最大値の差を 1 以下にすることができます。
- i=2,j=3 として操作を行う。A=(4,6,4,7) になる。
- i=4,j=1 として操作を行う。A=(5,6,4,6) になる。
- i=4,j=3 として操作を行う。A=(5,6,5,5) になる。
3 回未満の操作で A の最小値と最大値の差を 1 以下にすることはできません。よって答えは 3 です。
入力例 2
1 313
出力例 2
0
入力例 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
出力例 3
2499999974
Score : 400 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).
- Choose integers i and j with 1\leq i,j \leq N. Decrease A_i by one and increase A_j by one.
Find the minimum number of operations required to make the difference between the minimum and maximum values of A at most one.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
4 4 7 3 7
Sample Output 1
3
By the following three operations, the difference between the minimum and maximum values of A becomes at most one.
- Choose i=2 and j=3 to make A=(4,6,4,7).
- Choose i=4 and j=1 to make A=(5,6,4,6).
- Choose i=4 and j=3 to make A=(5,6,5,5).
You cannot make the difference between maximum and minimum values of A at most one by less than three operations, so the answer is 3.
Sample Input 2
1 313
Sample Output 2
0
Sample Input 3
10 999999997 999999999 4 3 2 4 999999990 8 999999991 999999993
Sample Output 3
2499999974
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
整数 N,M が与えられるので、 \displaystyle \sum_{k=0}^{N} \rm{popcount}(k \mathbin{\&} M) を 998244353 で割った余りを求めてください。
ただし、 \mathbin{\&} はビット単位 \rm{AND} 演算を表します。
ビット単位 \rm{AND} 演算とは?
非負整数 a と非負整数 b とのビット単位 \rm{AND} 演算の結果 x = a \mathbin{\&} b は次のように定義されます。- x は全ての非負整数 k について以下の条件を満たすただ 1 つの非負整数である。
- a を 2 進法で書き下した際の 2^k の位と b を 2 進法で書き下した際の 2^k の位が共に 1 なら、 x を 2 進法で書き下した際の 2^k の位は 1 である。
- そうでないとき、 x を 2 進法で書き下した際の 2^k の位は 0 である。
\rm{popcount} とは?
\rm{popcount}(x) は、 x を 2 進法で書き下した際に登場する 1 の個数を表します。例えば 13=1101_{(2)} なので、 \rm{popcount}(13) = 3 となります。
制約
- N は 0 以上 2^{60} 未満の整数
- M は 0 以上 2^{60} 未満の整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
答えを整数として出力せよ。
入力例 1
4 3
出力例 1
4
- \rm{popcount}(0\mathbin{\&}3) = 0
- \rm{popcount}(1\mathbin{\&}3) = 1
- \rm{popcount}(2\mathbin{\&}3) = 1
- \rm{popcount}(3\mathbin{\&}3) = 2
- \rm{popcount}(4\mathbin{\&}3) = 0
であり、これらの和は 4 です。
入力例 2
0 0
出力例 2
0
N=0 である場合や M=0 である場合もあります。
入力例 3
1152921504606846975 1152921504606846975
出力例 3
499791890
998244353 で割った余りを求めることに注意してください。
Score : 400 points
Problem Statement
Given integers N and M, compute the sum \displaystyle \sum_{k=0}^{N} \rm{popcount}(k \mathbin{\&} M), modulo 998244353.
Here, \mathbin{\&} represents the bitwise \rm{AND} operation.
What is the bitwise \rm{AND} operation?
The result x = a \mathbin{\&} b of the bitwise \rm{AND} operation between non-negative integers a and b is defined as follows:- x is the unique non-negative integer that satisfies the following conditions for all non-negative integers k:
- If the 2^k place in the binary representation of a and the 2^k place in the binary representation of b are both 1, then the 2^k place in the binary representation of x is 1.
- Otherwise, the 2^k place in the binary representation of x is 0.
What is \rm{popcount}?
\rm{popcount}(x) represents the number of 1s in the binary representation of x.For example, 13=1101_{(2)}, so \rm{popcount}(13) = 3.
Constraints
- N is an integer between 0 and 2^{60} - 1, inclusive.
- M is an integer between 0 and 2^{60} - 1, inclusive.
Input
The input is given from Standard Input in the following format:
N M
Output
Print the answer as an integer.
Sample Input 1
4 3
Sample Output 1
4
- \rm{popcount}(0\mathbin{\&}3) = 0
- \rm{popcount}(1\mathbin{\&}3) = 1
- \rm{popcount}(2\mathbin{\&}3) = 1
- \rm{popcount}(3\mathbin{\&}3) = 2
- \rm{popcount}(4\mathbin{\&}3) = 0
The sum of these values is 4.
Sample Input 2
0 0
Sample Output 2
0
It is possible that N = 0 or M = 0.
Sample Input 3
1152921504606846975 1152921504606846975
Sample Output 3
499791890
Remember to compute the result modulo 998244353.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
別世界の AtCoder では現在、 AAC, ..., AJC の 10 種類のコンテストが開催されており、これから N 回のコンテストが開催されます。
各コンテストの種類は文字列 S として与えられ、 S の i 文字目が x なら i 回目には AxC が開催されます。
シカの AtCoDeer くんは、 N 個のコンテストから 1 個以上いくつか選んで、以下の条件を満たすように選んで出場します。
- 出るコンテストを順番を保ったまま抜き出したとき、コンテストの種類ごとにひとかたまりとなっている。
- 厳密には、 AtCoDeer くんが x 個のコンテストに出場し、そのうち i 回目のコンテストの種類が T_i であるとき、全ての 1 \le i < j < k \le x を満たす整数組 (i,j,k) に対して、 T_i=T_k であるならば T_i=T_j でなければならない。
AtCoDeer くんが出場するコンテストの選び方として考えられるものの総数を 998244353 で割った余りを求めてください。
ただし、 2 つのコンテストの選び方が異なるとは、あるコンテスト c が存在して、片方の選び方では c に出場するがもう片方の選び方では出場しないということを指します。
制約
- 1 \le N \le 1000
- |S|=N
- S は
AからJまでの英大文字のみからなる
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを整数として出力せよ。
入力例 1
4 BGBH
出力例 1
13
例えば、 1,3 回目のコンテストに出場する、 2,4 回目のコンテストに出場するという選び方は条件を満たします。
一方、 1,2,3,4 回目のコンテストに出場する場合、 ABC への出場がひとかたまりになっておらず、整数組 (i,j,k)=(1,2,3) について条件に違反します。
また、全てのコンテストに出場しないということも認められません。
問題文の条件に適する出場するコンテストの選び方は 13 通りあります。
入力例 2
100 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF
出力例 2
330219020
総数を 998244353 で割った余りを求めることに注意してください。
Score : 500 points
Problem Statement
AtCoder in another world holds 10 types of contests called AAC, ..., AJC. There will be N contests from now on.
The types of these N contests are given to you as a string S: if the i-th character of S is x, the i-th contest will be AxC.
AtCoDeer will choose and participate in one or more contests from the N so that the following condition is satisfied.
- In the sequence of contests he will participate in, the contests of the same type are consecutive.
- Formally, when AtCoDeer participates in x contests and the i-th of them is of type T_i, for every triple of integers (i,j,k) such that 1 \le i < j < k \le x, T_i=T_j must hold if T_i=T_k.
Find the number of ways for AtCoDeer to choose contests to participate in, modulo 998244353.
Two ways to choose contests are considered different when there is a contest c such that AtCoDeer participates in c in one way but not in the other.
Constraints
- 1 \le N \le 1000
- |S|=N
- S consists of uppercase English letters from
AthroughJ.
Input
Input is given from Standard Input in the following format:
N S
Output
Print the answer as an integer.
Sample Input 1
4 BGBH
Sample Output 1
13
For example, participating in the 1-st and 3-rd contests is valid, and so is participating in the 2-nd and 4-th contests.
On the other hand, participating in the 1-st, 2-nd, 3-rd, and 4-th contests is invalid, since the participations in ABCs are not consecutive, violating the condition for the triple (i,j,k)=(1,2,3).
Additionally, it is not allowed to participate in zero contests.
In total, there are 13 valid ways to participate in some contests.
Sample Input 2
100 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF
Sample Output 2
330219020
Be sure to find the count modulo 998244353.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N,Q および A=(A_1,\ldots,A_N) が与えられます。
以下のクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。
1 x v: A_x を v に更新する。2 x: B_i=\sum_{j=1}^{i}A_j、C_i=\sum_{j=1}^{i}B_j、D_i=\sum_{j=1}^{i}C_j としたときの D_x を \bmod\ 998244353 で出力する。
制約
- 1 \leq N \leq 2\times10^5
- 1 \leq Q \leq 2\times10^5
- 0 \leq A_i \leq 10^9
- 1 \leq x \leq N
- 0 \leq v \leq 10^9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。ここで {\rm query}_i は i 番目に処理するクエリである。
N Q
A_1 A_2 \ldots A_N
{\rm query}_1
{\rm query}_2
\vdots
{\rm query}_Q
各クエリは以下の 2 種類のいずれかの形式で与えられる。
1 x v
2 x
出力
クエリへの答えを改行区切りで出力せよ。
入力例 1
3 3 1 2 3 2 3 1 2 0 2 3
出力例 1
15 9
1 番目のクエリの時点で A=(1,2,3) であるため、B=(1,3,6)、C=(1,4,10)、D=(1,5,15) となり、D_3=15 です。
3 番目のクエリの時点で A=(1,0,3) であるため、B=(1,1,4)、C=(1,2,6)、D=(1,3,9) となり、D_3=9 です。
入力例 2
2 1 998244353 998244353 2 1
出力例 2
0
Score : 500 points
Problem Statement
You are given N, Q, and A=(A_1,\ldots,A_N).
Process Q queries, each of which is of one of the following two kinds:
1 x v: update A_x to v.2 x: let B_i=\sum_{j=1}^{i}A_j, C_i=\sum_{j=1}^{i}B_j, and D_i=\sum_{j=1}^{i}C_j. Print D_x modulo 998244353.
Constraints
- 1 \leq N \leq 2\times10^5
- 1 \leq Q \leq 2\times10^5
- 0 \leq A_i \leq 10^9
- 1 \leq x \leq N
- 0 \leq v \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format, where {\rm query}_i denotes the i-th query to be processed:
N Q
A_1 A_2 \ldots A_N
{\rm query}_1
{\rm query}_2
\vdots
{\rm query}_Q
Each query is given in one of the following two formats:
1 x v
2 x
Output
Print the answer to the queries, with newlines in between.
Sample Input 1
3 3 1 2 3 2 3 1 2 0 2 3
Sample Output 1
15 9
When the 1-st query is given, A=(1,2,3), so B=(1,3,6), C=(1,4,10), and D=(1,5,15); thus, D_3=15.
When the 3-rd query is given, A=(1,0,3), so B=(1,1,4), C=(1,2,6), and D=(1,3,9); thus, D_3=9.
Sample Input 2
2 1 998244353 998244353 2 1
Sample Output 2
0