実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 2 点
問題文
あなたは次の 2 種類のポリオミノを無数に持っています:
- 縦 2 マス、横 1 マスの長方形型のポリオミノ
- 一辺が 2 マスの正方形型のポリオミノから一辺が 1 マスの正方形を 1 個取り除いてできる、L 字型のポリオミノ
縦 2 マス、横 N マスのグリッドがあります。あなたは次の条件を満たすようにグリッドの全てのマスにポリオミノを敷き詰めることにしました。
- グリッドの各マスにはちょうど 1 個のポリオミノが置かれている。
- ポリオミノを置くときに回転させることができる。
条件を満たすポリオミノの配置の個数を求めてください。ただし、グリッドを回転・反転してはじめて一致する配置は別々に数えます。また、同じ形のポリオミノ同士は区別できないものとします。
制約
- 1 \leq N \leq 40
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
条件を満たすポリオミノの配置の個数を出力せよ。
入力例 1
3
出力例 1
5
条件を満たすポリオミノの配置は次の 5 通りです。

入力例 2
40
出力例 2
25366833951139
Score : 2 points
Problem Statement
You have an unlimited number of the following two types of polyominoes:
- A rectangular polyomino of size 2 (height) × 1 (width)
- An L-shaped polyomino formed by removing one 1 \times 1 square from a 2 \times 2 square
You are given a grid of size 2 (height) × N (width). You want to completely tile all cells of the grid using these polyominoes under the following conditions:
- Each cell of the grid must be covered by exactly one polyomino.
- You may rotate the polyominoes when placing them.
Find the number of ways to place the polyominoes satisfying these conditions. Note that two arrangements are considered different even if one can be obtained from the other by rotating or flipping the entire grid. Also, polyominoes of the same shape are indistinguishable.
Constraints
- 1 \leq N \leq 40
- N is an integer
Input
The input is given from standard input in the following format:
N
Output
Print the number of valid tilings.
Sample Input 1
3
Sample Output 1
5
There are 5 valid tilings as shown below.

Sample Input 2
40
Sample Output 2
25366833951139
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 2 点
問題文
頂点に 1 から N の番号が付いた N 頂点 M 辺の単純有向グラフがあります。i 番目の辺は頂点 u_i から頂点 v_i へ向かう辺です。
このグラフには閉路がありません。
頂点 1 から頂点 N へ向かうパスの本数を 998244353 で割った余りを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min\left(\frac{N(N-1)}{2}, 2 \times 10^5\right)
- 1 \leq u_i \leq N
- 1 \leq v_i \leq N
- i \neq j ならば (u_i, v_i) \neq (u_j, v_j)
- 入力で与えられるグラフは閉路のない単純有向グラフ
- 全てのテストケースに対する N の総和は 2 \times 10^5 以下
- 全てのテストケースに対する M の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、頂点 1 から頂点 N へ向かうパスの本数を 998244353 で割った余りを出力せよ。
入力例 1
3 4 4 1 2 2 3 3 4 2 4 5 4 1 2 2 3 4 5 1 3 7 18 1 6 1 4 6 4 6 3 4 3 1 5 6 5 4 5 3 5 1 2 6 2 4 2 3 2 5 2 6 7 4 7 5 7 2 7
出力例 1
2 0 24
1 番目のテストケースについて、頂点 1 から頂点 4 へ向かうパスは次の 2 本です。
- 頂点 1 \to 頂点 2 \to 頂点 3 \to 頂点 4
- 頂点 1 \to 頂点 2 \to 頂点 4
Score : 2 points
Problem Statement
You are given a simple directed graph with N vertices and M edges. The vertices are numbered from 1 to N. The i-th edge goes from vertex u_i to vertex v_i.
This graph has no cycles.
Find the number of paths from vertex 1 to vertex N, modulo 998244353.
You are given T test cases. Solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq \min\left(\frac{N(N-1)}{2}, 2 \times 10^5\right)
- 1 \leq u_i \leq N
- 1 \leq v_i \leq N
- If i \neq j, then (u_i, v_i) \neq (u_j, v_j)
- The given graph is a simple directed graph with no cycles
- The sum of N over all test cases is at most 2 \times 10^5
- The sum of M over all test cases is at most 2 \times 10^5
- All input values are integers
Input
The input is given from standard input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print T lines. For the i-th line, output the answer for the i-th test case.
For each test case, output the number of paths from vertex 1 to vertex N, modulo 998244353.
Sample Input 1
3 4 4 1 2 2 3 3 4 2 4 5 4 1 2 2 3 4 5 1 3 7 18 1 6 1 4 6 4 6 3 4 3 1 5 6 5 4 5 3 5 1 2 6 2 4 2 3 2 5 2 6 7 4 7 5 7 2 7
Sample Output 1
2 0 24
For the first test case, there are 2 paths from vertex 1 to vertex 4:
- vertex 1 \to vertex 2 \to vertex 3 \to vertex 4
- vertex 1 \to vertex 2 \to vertex 4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 2 点
問題文
英小文字からなる 3 個の文字列 S_1, S_2, S_3 が与えられます。
長さ N の英小文字からなる文字列 T であって S_1, S_2, S_3 のいずれも部分列として含まないものの個数を 998244353 で割った余りを求めてください。
制約
- 1 \leq N \leq 10^3
- S_1, S_2, S_3 は長さ 10 以下の英小文字からなる空でない文字列
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 S_3
出力
長さ N の英小文字からなる文字列 T であって S_1, S_2, S_3 のいずれも部分列として含まないものの個数を 998244353 で割った余りを出力せよ。
入力例 1
2 abc de f
出力例 1
624
長さ 2 の英小文字からなる文字列は 676 個あり、そのうち S_2 = de を含む文字列は 1 個、S_3 = f を含む文字列は 51 個で、両者は排反です。よって、676 - 1 - 51 = 624 個が答えとなります。
入力例 2
4 ab ab ab
出力例 2
453125
条件にある「部分列」は「部分文字列」とは異なる点に注意してください。例えば accb は部分列として ab を含んでいるため条件を満たしません。
入力例 3
1000 atcoder algorithm lectures
出力例 3
669410767
Score : 2 points
Problem Statement
You are given three strings S_1, S_2, S_3, each consisting of lowercase English letters.
Find the number of strings T of length N (also consisting of lowercase English letters) such that none of S_1, S_2, S_3 appear as a subsequence of T. Output the answer modulo 998244353.
Constraints
- 1 \leq N \leq 10^3
- S_1, S_2, S_3 are non-empty strings of lowercase English letters with length at most 10
- N is an integer
Input
The input is given from standard input in the following format:
N S_1 S_2 S_3
Output
Print the number of strings T of length N consisting of lowercase English letters such that none of S_1, S_2, S_3 appear as a subsequence of T, modulo 998244353.
Sample Input 1
2 abc de f
Sample Output 1
624
There are 676 strings of length 2 consisting of lowercase English letters. Among them, 1 string contains S_2 = de, and 51 strings contain S_3 = f. These sets do not overlap. Therefore, the answer is 676 - 1 - 51 = 624.
Sample Input 2
4 ab ab ab
Sample Output 2
453125
Note that subsequence in the condition is different from substring.
For example, accb contains ab as a subsequence, so it does not satisfy the condition.
Sample Input 3
1000 atcoder algorithm lectures
Sample Output 3
669410767
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 3 点
問題文
AtCoder 王国では 1 円紙幣, 10 円紙幣, 100 円紙幣, \dots, 10^{100} 円紙幣が流通しています。
Alice は Bob に N 円を支払うことにしました。Alice が Bob に支払う紙幣の枚数とお釣りとしてもらう紙幣の枚数の合計の最小値を求めてください。
厳密に述べると、N 以上の整数値を取る M に対して「M 円ちょうどを表すのに必要な最小の紙幣の枚数」と「M-N 円ちょうどを表すのに必要な最小の紙幣の枚数」の和の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 10^4
- 1 \leq N \leq 10^{18}
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
入力例 1
3 7 34 123456789123456789
出力例 1
4 7 44
1 番目のテストケースでは、
- 10 円紙幣 1 枚で 10 円を支払い、
- 1 円紙幣 3 枚で 3 円をお釣りとしてもらう
という方法が最適で、この時に受け渡される紙幣の枚数の合計は 4 枚です。
Score : 3 points
Problem Statement
In the AtCoder Kingdom, banknotes of denominations 1 yen, 10 yen, 100 yen, \dots, 10^{100} yen are used.
Alice wants to pay N yen to Bob. Find the minimum possible total number of banknotes exchanged, which is the sum of:
- the number of banknotes Alice gives to Bob, and
- the number of banknotes Bob gives back as change.
More precisely, for any integer M \geq N, consider:
- the minimum number of banknotes needed to represent exactly M yen, and
- the minimum number of banknotes needed to represent exactly M - N yen.
Find the minimum possible value of their sum.
You are given T test cases. Solve each of them.
Constraints
- 1 \leq T \leq 10^4
- 1 \leq N \leq 10^{18}
- All input values are integers
Input
The input is given from standard input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N
Output
Print T lines. For the i-th line, output the answer for the i-th test case.
Sample Input 1
3 7 34 123456789123456789
Sample Output 1
4 7 44
In the first test case:
- Alice pays 10 yen using one 10-yen banknote, and
- Bob returns 3 yen as change using three 1-yen banknotes.
In this case, the total number of banknotes exchanged is 4, which is optimal.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 3 点
問題文
あなたは今日から夏休みです。夏休みは N 日間からなります。
M 個のイベントがあります。i 個目のイベントは A_i 日目の朝にはじまって B_i 日目の夜に終わります。
Q 個のクエリに答えてください。クエリでは L, R が与えられるので、次の問いに答えてください。
あなたは L 日目から R 日目の間にできるだけたくさんのイベントに参加することにしました。
ただし、イベントは途中参加や途中抜けが出来ません。よって期間が被っている複数のイベントに参加したり、L 日目より前や R 日目より後を期間に含むイベントに参加することはできません。
参加するイベントを上手く選んだ時、最大で何個のイベントに参加できますか?
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq N
- 1 \leq L \leq R \leq N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M Q
A_1 B_1
A_2 B_2
\vdots
A_M B_M
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
L R
出力
Q 行出力せよ。i 行目には i 番目のクエリへの答えを出力せよ。
入力例 1
5 3 3 1 3 4 5 2 2 1 5 1 1 3 5
出力例 1
2 0 1
例えば 1 番目のクエリでは、1 番目のイベントと 2 番目のイベントに参加することが出来てこれが最大です。
入力例 2
9 13 8 4 6 1 5 8 9 5 6 1 7 4 9 4 6 2 8 5 6 5 8 2 6 3 7 1 3 5 7 5 7 5 7 3 4 1 8 2 6 6 8 8 9
出力例 2
1 1 1 0 2 1 0 1
Score : 3 points
Problem Statement
Your summer vacation starts today and lasts for N days.
There are M events. The i-th event starts on the morning of day A_i and ends on the evening of day B_i.
You are given Q queries. In each query, you are given two integers L and R, and you need to answer the following:
You decide to attend as many events as possible during the period from day L to day R.
However, you cannot join or leave an event in the middle. Therefore, you cannot attend multiple events whose time periods overlap, and you also cannot attend any event that starts before day L or ends after day R.
If you choose the events optimally, what is the maximum number of events you can attend?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq N
- 1 \leq L \leq R \leq N
- All input values are integers
Input
The input is given from standard input in the following format:
N M Q
A_1 B_1
A_2 B_2
\vdots
A_M B_M
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
L R
Output
Print Q lines. For the i-th line, output the answer to the i-th query.
Sample Input 1
5 3 3 1 3 4 5 2 2 1 5 1 1 3 5
Sample Output 1
2 0 1
For example, in the first query, you can attend the 1st and 2nd events, and this is the maximum.
Sample Input 2
9 13 8 4 6 1 5 8 9 5 6 1 7 4 9 4 6 2 8 5 6 5 8 2 6 3 7 1 3 5 7 5 7 5 7 3 4 1 8 2 6 6 8 8 9
Sample Output 2
1 1 1 0 2 1 0 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 4 点
問題文
(1, 2, \dots, N) の順列 P = (P_1, P_2, \dots, P_N) および長さ N の整数列 A_1, A_2, \dots, A_N が与えられます。
整数集合 \lbrace 1,2,\dots, N \rbrace の部分集合 S であって以下の条件を満たすものを 良い集合 と呼びます。
- x \lt y かつ x, y \in S である整数の組 (x, y) 全てに対して以下の条件が成り立つ。
- z を P_z = \min(P_x, P_{x+1}, \dots, P_y) を満たす整数とする。(このような z は一意に定まる。) この時、z \in S が成り立つ。
良い集合 S の コスト を \displaystyle \sum_{i \in S} A_i として定義します。
K = 1, 2, \dots, N について、サイズ K の良い集合のコストとしてあり得る値の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 5000
- 1 \leq N \leq 5000
- 1 \leq A_i \leq 10^9
- (P_1, P_2, \dots, P_N) は (1,2,\dots,N) の順列
- 全てのテストケースに対する N の総和は 5000 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N P_1 P_2 \dots P_N A_1 A_2 \dots A_N
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、K=1,2,\dots,N に対する答えを空白区切りで 1 行に出力せよ。
入力例 1
3 4 4 1 2 3 1 8 2 4 6 5 3 2 4 6 1 73 38 30 85 27 45 10 4 10 3 7 2 6 8 9 5 1 853822501 687675302 281611653 844033520 423210108 339630584 780395612 207907746 285523486 359061085
出力例 1
1 6 11 15 27 57 95 140 213 298 207907746 493431232 833061816 1192122901 1537883577 1896944662 2584619964 3365015576 4209049096 5062871597
1 番目のテストケースでは、
- K=1 の時は S=\lbrace 1 \rbrace が、
- K=2 の時は S=\lbrace 3,4 \rbrace が、
- K=3 の時は S=\lbrace 1,2,3 \rbrace が、
- K=4 の時は S=\lbrace 1,2,3,4 \rbrace が良い集合の中でコスト最小です。
Score : 4 points
Problem Statement
You are given a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N) and an integer array A_1, A_2, \dots, A_N of length N.
A subset S of the set \lbrace 1,2,\dots,N \rbrace is called a good set if it satisfies the following condition:
- For every pair of integers (x, y) such that x < y and x, y \in S, the following holds:
- Let z be the integer such that P_z = \min(P_x, P_{x+1}, \dots, P_y). (Such a z is uniquely determined.) Then, it must hold that z \in S.
The cost of a good set S is defined as \displaystyle \sum_{i \in S} A_i.
For each K = 1, 2, \dots, N, find the minimum possible cost among all good sets of size K.
You are given T test cases. Solve each of them.
Constraints
- 1 \leq T \leq 5000
- 1 \leq N \leq 5000
- 1 \leq A_i \leq 10^9
- (P_1, P_2, \dots, P_N) is a permutation of (1,2,\dots,N)
- The sum of N over all test cases is at most 5000
- All input values are integers
Input
The input is given from standard input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N P_1 P_2 \dots P_N A_1 A_2 \dots A_N
Output
Print T lines. For the i-th line, output the answer for the i-th test case.
For each test case, output the answers for K = 1,2,\dots,N in one line, separated by spaces.
Sample Input 1
3 4 4 1 2 3 1 8 2 4 6 5 3 2 4 6 1 73 38 30 85 27 45 10 4 10 3 7 2 6 8 9 5 1 853822501 687675302 281611653 844033520 423210108 339630584 780395612 207907746 285523486 359061085
Sample Output 1
1 6 11 15 27 57 95 140 213 298 207907746 493431232 833061816 1192122901 1537883577 1896944662 2584619964 3365015576 4209049096 5062871597
In the first test case:
- For K=1, S=\lbrace 1 \rbrace is optimal,
- For K=2, S=\lbrace 3,4 \rbrace is optimal,
- For K=3, S=\lbrace 1,2,3 \rbrace is optimal,
- For K=4, S=\lbrace 1,2,3,4 \rbrace is optimal.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 4 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
Q 個のクエリを処理してください。クエリでは 1 \leq i \leq N を満たす整数 i および非負整数 v が与えられるので、A_i を v に更新して、以下の問題を解いてください。(更新は永続的です。)
1 から N の番号がついた N 人の人が口を開けた状態で数直線上に並んでいます。人 i は地点 i にいます。各人は 空腹度 というパラメータを持っていて、人 i の空腹度は A_i です。
あなたはキャンディを無数に持っています。あなたはみんなにキャンディを食べさせてあげるために次の一連の行動を 1 回だけ行うことにしました。
- まず、1 \leq x \leq N を満たす整数 x を選び、地点 x に立つ。
- そして、次の操作を 0 回以上自由な回数行う。
- 今いる地点を y として、y-1, y, y+1 のいずれかの地点に移動する。ただし、人が立っていない地点に移動することは出来ない。
- 今いる地点にいる人の口にキャンディを 1 個放り込む。
行動を終了した時点で、人 i の口に放り込んだキャンディの個数を B_i とします。\displaystyle \sum_{i=1}^N \vert A_i - B_i \vert としてあり得る最小値を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 0 \leq A_i \leq 10^9
- 1 \leq i \leq N
- 0 \leq v \leq 10^9
- 入力される値は全て整数
部分点
この問題には部分点が設定されている。
- Q \leq 10 であるデータセットに正解した場合、2 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
i v
出力
Q 行出力せよ。i 行目には i 番目のクエリへの答えを出力せよ。
入力例 1
4 3 1 3 0 2 2 0 1 3 3 5
出力例 1
1 2 1
1 番目のクエリを処理することを考えます。A=(1,0,0,2) に対して問題を解けばよいです。
この時、以下の行動を取ることで目的関数の値を 1 にすることができて、これが最適です。
- x=3 を選び、地点 3 に立つ。
- 地点 4 に移動して人 4 の口にキャンディを 1 個放り込む。
- 再び地点 4 に移動して人 4 の口にキャンディを 1 個放り込む。
- 操作を終了する。操作後の B は (0,0,0,2) となる。
入力例 2
10 9 1 0 0 0 0 0 4 0 0 2 3 2 7 0 1 9 6 4 1 1 10 0 1 0 6 0 5 7
出力例 2
5 3 3 5 5 3 2 0 1
Score : 4 points
Problem Statement
You are given an integer sequence A = (A_1, A_2, \dots, A_N) of length N.
Process Q queries. In each query, you are given an integer i (1 \leq i \leq N) and a non-negative integer v. Update A_i to v, and then solve the following problem. (The updates are permanent.)
There are N people standing on a number line at positions 1 to N, each with their mouth open. Person i is at position i. Each person has a parameter called hunger, and the hunger of person i is A_i.
You have an unlimited number of candies. You decide to perform the following sequence of actions exactly once to feed them:
- First, choose an integer x such that 1 \leq x \leq N, and stand at position x.
- Then, perform the following operations any number of times (possibly zero):
- Let your current position be y. Move to position y-1, y, or y+1. However, you cannot move to a position where no person is standing.
- Throw one candy into the mouth of the person at your current position.
After finishing all operations, let B_i be the number of candies given to person i. Find the minimum possible value of \displaystyle \sum_{i=1}^N \vert A_i - B_i \vert.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 0 \leq A_i \leq 10^9
- 1 \leq i \leq N
- 0 \leq v \leq 10^9
- All input values are integers
Partial Score
This problem has partial scoring.
- If you solve the dataset with Q \leq 10, you will get 2 points.
Input
The input is given from standard input in the following format:
N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
i v
Output
Print Q lines. For the i-th line, output the answer to the i-th query.
Sample Input 1
4 3 1 3 0 2 2 0 1 3 3 5
Sample Output 1
1 2 1
Consider processing the first query. You need to solve the problem for A = (1,0,0,2).
In this case, the following actions achieve a value of 1, which is optimal:
- Choose x=3 and stand at position 3.
- Move to position 4 and give one candy to person 4.
- Stay at position 4 again and give another candy to person 4.
- End the operations. The resulting B becomes (0,0,0,2).
Sample Input 2
10 9 1 0 0 0 0 0 4 0 0 2 3 2 7 0 1 9 6 4 1 1 10 0 1 0 6 0 5 7
Sample Output 2
5 3 3 5 5 3 2 0 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 4 点
問題文
縦横 N マスのグリッドがあります。グリッドの上から i 行目、左から j 列目のマスを (i,j) と呼びます。
グリッドの各マスの状態は N 個の長さ N の文字列 S_1,S_2,\dots,S_N によって表されます。S_i の j 文字目が @ である時は (i,j) はコインが 1 枚落ちているマス、. である時は何もないマスです。
あなたはグリッドの (1,1) にいます。(1,1) は何もないマスであることが保証されています。
あなたは以下の行動を 0 回以上自由な回数行うことができます。
- 今自分がいるマスを (i,j) とする。右のマスまたは下のマス、すなわち (i,j+1) または (i+1,j) に移動する。グリッドの外への移動はできない。移動した先のマスにコインが落ちていた場合、あなたはそれを必ず拾う。
x = 0, 1, \dots, 2N-2 について次の問題を解いてください。
- あなたは 0 回以上行動してあるマス (i,j) にたどり着きました。この時、あなたはコインを x 枚持っていました。(i,j) としてあり得るマスは何マスありますか?
制約
- 2 \leq N \leq 4000
- S_1, S_2, \dots, S_N は
@と.からなる長さ N の文字列 - S_1 の 1 文字目は
. - N は整数
部分点
この問題には部分点が設定されている。
- N \leq 1500 であるデータセットに正解した場合、2 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
2N-1 行出力せよ。i 行目には x=i-1 の時の答えを出力せよ。
入力例 1
3 .@@ ..@ @..
出力例 1
5 6 3 2 0
例えば x=0 のとき、(i,j) としてあり得るのは (1,1),(2,1),(2,2),(3,2),(3,3) の 5 個です。
入力例 2
7 ...@@@. ..@@@@@ @...@.. ..@..@. .....@@ .@@@@@@ @@.@.@.
出力例 2
15 29 28 23 19 13 6 5 3 0 0 0 0
Score : 4 points
Problem Statement
You are given an N \times N grid. Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The state of each cell is described by N strings S_1, S_2, \dots, S_N, each of length N.
If the j-th character of S_i is @, then cell (i,j) contains one coin. If it is ., then the cell is empty.
You start at cell (1,1). It is guaranteed that (1,1) is empty.
You may perform the following action any number of times (including zero):
- Let your current position be (i,j). Move to either the right cell (i,j+1) or the down cell (i+1,j). You cannot move outside the grid. If the destination cell contains a coin, you must pick it up.
For each x = 0, 1, \dots, 2N-2, solve the following:
- After performing some actions (possibly zero), you reach a cell (i,j) while having exactly x coins. How many possible cells (i,j) are there?
Constraints
- 2 \leq N \leq 4000
- S_1, S_2, \dots, S_N are strings of length N consisting of
@and. - The first character of S_1 is
. - N is an integer
Partial Score
This problem has partial scoring.
- If you solve the dataset with N \leq 1500, you will get 2 points.
Input
The input is given from standard input in the following format:
N S_1 S_2 \vdots S_N
Output
Print 2N-1 lines. On the i-th line, output the answer for x = i-1.
Sample Input 1
3 .@@ ..@ @..
Sample Output 1
5 6 3 2 0
For example, when x=0, the possible cells (i,j) are (1,1), (2,1), (2,2), (3,2), (3,3), so there are 5 such cells.
Sample Input 2
7 ...@@@. ..@@@@@ @...@.. ..@..@. .....@@ .@@@@@@ @@.@.@.
Sample Output 2
15 29 28 23 19 13 6 5 3 0 0 0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 4 点
問題文
長さ N の数列 B = (B_1, B_2, \dots, B_N) に対して、prefix max の更新点 と suffix max の更新点 を次のように定義します。
- 任意の 1 \leq j \lt i について B_j \lt B_i が成り立つ整数 i を prefix max の更新点と呼ぶ。
- 任意の i \lt j \leq N について B_j \lt B_i が成り立つ整数 i を suffix max の更新点と呼ぶ。
長さ N の数列 A=(A_1, A_2, \dots, A_N) および L,R が与えられます。
A の要素を並べ替えてできる数列であって、prefix max の更新点が L 個、suffix max の更新点が R 個であるものとしてあり得る数列の個数を 998244353 で割った余りを求めてください。ただし、2 つの数列は列として一致する時に同じであるとみなします。
制約
- 1 \leq N \leq 400
- 1 \leq L \leq N
- 1 \leq R \leq N
- 1 \leq A_i \leq N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L R A_1 A_2 \dots A_N
出力
条件を満たす数列の個数を 998244353 で割った余りを出力せよ。
入力例 1
4 2 1 1 2 3 4
出力例 1
2
条件を満たす数列は次の 2 通りです。
- (3,2,1,4)
- (3,1,2,4)
入力例 2
10 3 2 6 10 4 1 5 9 8 6 5 1
出力例 2
50424
Score : 4 points
Problem Statement
For a sequence B = (B_1, B_2, \dots, B_N) of length N, define prefix max update positions and suffix max update positions as follows:
- An index i is called a prefix max update position if for all 1 \leq j < i, we have B_j < B_i.
- An index i is called a suffix max update position if for all i < j \leq N, we have B_j < B_i.
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N, and integers L and R.
Consider all sequences obtained by rearranging the elements of A. Among them, count how many sequences have exactly L prefix max update positions and exactly R suffix max update positions. Output the answer modulo 998244353.
Two sequences are considered the same if they are identical as sequences.
Constraints
- 1 \leq N \leq 400
- 1 \leq L \leq N
- 1 \leq R \leq N
- 1 \leq A_i \leq N
- All input values are integers
Input
The input is given from standard input in the following format:
N L R A_1 A_2 \dots A_N
Output
Print the number of sequences satisfying the conditions, modulo 998244353.
Sample Input 1
4 2 1 1 2 3 4
Sample Output 1
2
There are 2 such sequences:
- (3,2,1,4)
- (3,1,2,4)
Sample Input 2
10 3 2 6 10 4 1 5 9 8 6 5 1
Sample Output 2
50424
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 5 点
問題文
長さ N の正整数列 A=(A_1,A_2,\dots,A_N) と正整数 S,T が与えられます。
以下の条件を全て満たす非負整数列 (c_1,c_2,\dots,c_N) としてあり得るものの個数を 998244353 で割った余りを求めてください。
- c_1 + c_2 + \dots + c_N = S
- A_1 c_1 + A_2 c_2 + \dots + A_N c_N = T
制約
- 1 \leq N \leq 20
- 1 \leq S \leq 10^{18}
- 1 \leq T \leq 10^{18}
- 1 \leq A_i \leq 200
- \displaystyle \sum_{i=1}^N A_i \leq 200
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N S T A_1 A_2 \dots A_N
出力
条件を満たす非負整数列 (c_1,c_2,\dots,c_N) としてあり得るものの個数を 998244353 で割った余りを出力せよ。
入力例 1
4 4 10 1 2 3 4
出力例 1
5
条件を満たす (c_1, c_2, c_3, c_4) は次の 5 個です。
- (0,2,2,0)
- (0,3,0,1)
- (1,0,3,0)
- (1,1,1,1)
- (2,0,0,2)
入力例 2
10 34016955048482281 206283256471036323 38 35 28 54 3 15 4 16 6 1
出力例 2
230312634
Score : 5 points
Problem Statement
You are given a sequence of positive integers A = (A_1, A_2, \dots, A_N) of length N, and positive integers S and T.
Find the number of sequences of non-negative integers (c_1, c_2, \dots, c_N) that satisfy all of the following conditions, modulo 998244353:
- c_1 + c_2 + \dots + c_N = S
- A_1 c_1 + A_2 c_2 + \dots + A_N c_N = T
Constraints
- 1 \leq N \leq 20
- 1 \leq S \leq 10^{18}
- 1 \leq T \leq 10^{18}
- 1 \leq A_i \leq 200
- \displaystyle \sum_{i=1}^N A_i \leq 200
- All input values are integers
Input
The input is given from standard input in the following format:
N S T A_1 A_2 \dots A_N
Output
Print the number of sequences of non-negative integers (c_1, c_2, \dots, c_N) that satisfy the conditions, modulo 998244353.
Sample Input 1
4 4 10 1 2 3 4
Sample Output 1
5
The following 5 sequences satisfy the conditions:
- (0,2,2,0)
- (0,3,0,1)
- (1,0,3,0)
- (1,1,1,1)
- (2,0,0,2)
Sample Input 2
10 34016955048482281 206283256471036323 38 35 28 54 3 15 4 16 6 1
Sample Output 2
230312634
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 5 点
問題文
長さ N の整数列 A = (A_1, A_2, \dots, A_N) があります。
また、あなたは整数 x を持っています。はじめ x=0 です。
あなたは次の N 回の操作を行います。
- i = 1, 2, \dots, N の順に、次の 3 種類の操作のうちどれか 1 つを行う。
- x を x + A_i に置き換える。
- x を \vert x - A_i \vert に置き換える。
- 何もしない。
N 回の操作が終了した時点での x の値としてあり得るものの個数を求めてください。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq A_i \leq 2 \times 10^6
- \displaystyle 1 \leq \sum_{i=1}^N A_i \leq 2 \times 10^6
- 入力される値は全て整数
部分点
この問題には部分点が設定されている。
- \displaystyle 1 \leq \sum_{i=1}^N A_i \leq 5 \times 10^5 であるデータセットに正解した場合、4 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
N 回の操作が終了した時点での x の値としてあり得るものの個数を出力せよ。
入力例 1
3 2 7 5
出力例 1
10
例えば次の操作を行うことで操作後の x を 9 にすることができます。
- はじめ、x=0 である。
- i=1 の時、x を \vert x-A_i \vert = \vert 0-2 \vert = 2 に置き換える。
- i=2 の時、x を x+A_i=2+7=9 に置き換える。
- i=3 の時、何もしない。
入力例 2
10 49 85 36 30 71 65 38 45 73 27
出力例 2
454
Score : 5 points
Problem Statement
You are given an integer sequence A = (A_1, A_2, \dots, A_N) of length N.
You also have an integer x, which is initially 0.
You will perform the following N operations:
- For i = 1, 2, \dots, N, choose exactly one of the following three operations:
- Replace x with x + A_i.
- Replace x with \vert x - A_i \vert.
- Do nothing.
Find the number of possible values of x after all N operations are completed.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq A_i \leq 2 \times 10^6
- \displaystyle 1 \leq \sum_{i=1}^N A_i \leq 2 \times 10^6
- All input values are integers
Partial Score
This problem has partial scoring.
- If you solve the dataset with \displaystyle 1 \leq \sum_{i=1}^N A_i \leq 5 \times 10^5, you will get 4 points.
Input
The input is given from standard input in the following format:
N A_1 A_2 \dots A_N
Output
Print the number of possible values of x after all N operations are completed.
Sample Input 1
3 2 7 5
Sample Output 1
10
For example, you can obtain x = 9 by performing the following operations:
- Initially, x = 0.
- At i = 1, replace x with \vert x - A_i \vert = \vert 0 - 2 \vert = 2.
- At i = 2, replace x with x + A_i = 2 + 7 = 9.
- At i = 3, do nothing.
Sample Input 2
10 49 85 36 30 71 65 38 45 73 27
Sample Output 2
454
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 5 点
問題文
正整数 N と (1, 2, \dots, N) の順列 A=(A_1,A_2,\dots,A_N) が与えられます。
頂点に 1 から N の番号が付いた N 頂点の有向グラフ G を考えます。1 \leq i \lt j \leq N を満たす正整数の組 (i,j) それぞれについて、頂点 i から頂点 j に向かう辺が \mathrm{LCM}(A_i, A_j) 本存在します(\mathrm{LCM} は最小公倍数を意味する関数)。G にそれ以外の辺はありません。
v = 2, 3, \dots, N について、頂点 1 から頂点 v へ向かうパスの本数を 998244353 で割った余りを求めてください。ここで、2 本のパスは通った辺の集合が異なる時に区別して数えます。
制約
- 2 \leq N \leq 2 \times 10^5
- (A_1, A_2, \dots, A_N) は (1, 2, \dots, N) の順列
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
N-1 行出力せよ。i 行目には v=i+1 の時の答えを出力せよ。
入力例 1
4 3 2 1 4
出力例 1
6 15 96
G に含まれる有向辺を列挙すると次の通りです。
- 頂点 1 から頂点 2 へ向かう辺: 6 本
- 頂点 1 から頂点 3 へ向かう辺: 3 本
- 頂点 1 から頂点 4 へ向かう辺: 12 本
- 頂点 2 から頂点 3 へ向かう辺: 2 本
- 頂点 2 から頂点 4 へ向かう辺: 4 本
- 頂点 3 から頂点 4 へ向かう辺: 4 本
入力例 2
13 12 3 2 4 10 9 5 8 1 6 11 7 13
出力例 2
12 84 492 11100 1018368 45948480 913466263 559040529 720204824 935993195 339767199 889449065
Score : 5 points
Problem Statement
You are given a positive integer N and a permutation A = (A_1, A_2, \dots, A_N) of (1, 2, \dots, N).
Consider a directed graph G with N vertices numbered from 1 to N. For every pair of integers (i, j) such that 1 \leq i < j \leq N, there are \mathrm{LCM}(A_i, A_j) directed edges from vertex i to vertex j. (Here, \mathrm{LCM} denotes the least common multiple.) There are no other edges in G.
For each v = 2, 3, \dots, N, find the number of paths from vertex 1 to vertex v, modulo 998244353. Here, two paths are considered different if the sets of edges they pass through are different.
Constraints
- 2 \leq N \leq 2 \times 10^5
- (A_1, A_2, \dots, A_N) is a permutation of (1, 2, \dots, N)
- 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 N-1 lines. On the i-th line, output the answer for v = i+1.
Sample Input 1
4 3 2 1 4
Sample Output 1
6 15 96
The directed edges in G are as follows:
- From vertex 1 to vertex 2: 6 edges
- From vertex 1 to vertex 3: 3 edges
- From vertex 1 to vertex 4: 12 edges
- From vertex 2 to vertex 3: 2 edges
- From vertex 2 to vertex 4: 4 edges
- From vertex 3 to vertex 4: 4 edges
Sample Input 2
13 12 3 2 4 10 9 5 8 1 6 11 7 13
Sample Output 2
12 84 492 11100 1018368 45948480 913466263 559040529 720204824 935993195 339767199 889449065
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 5 点
問題文
1, 2, \dots, 9 からなる長さ 2^N の文字列 S が与えられます。
0 \leq n \leq 2^N-1 を満たす非負整数 n に対して f(n) を以下の手順に従って定義します。
- n \ \mathrm{OR} \ i = n を満たす非負整数 i を全て列挙して昇順に並べたものを i_1, i_2, \dots, i_k と置く。ここで \mathrm{OR} はビットごとの論理和を意味する。
- S の i_1+1 文字目, i_2+1 文字目, \dots, i_k+1 文字目をこの順に並べてできる文字列を T とする。
- T を 10 進整数とみなした時の値を X とする。そして、f(n) = X \bmod 998244353 とする。
f(0), f(1), \dots, f(2^N-1) を全て計算してください。そして、次式の値を出力してください。ここで \mathrm{XOR} はビットごとの排他的論理和を意味します。
制約
- 1 \leq N \leq 22
- S は
1,2, \dots,9からなる長さ 2^N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
\displaystyle \sum_{n=0}^{2^N-1} \left(f(n) \ \mathrm{XOR} \ n\right) を出力せよ。
入力例 1
2 1234
出力例 1
1262
0 \leq n \leq 2^N-1 を満たす n について f(n) を列挙すると以下の通りです。
- n=0 の時 f(n) = 1 です。
- n=1 の時 f(n) = 12 です。
- n=2 の時 f(n) = 13 です。
- n=3 の時 f(n) = 1234 です。
よって、(1 \ \mathrm{XOR} \ 0) + (12 \ \mathrm{XOR} \ 1) + (13 \ \mathrm{XOR} \ 2) + (1234 \ \mathrm{XOR} \ 3) = 1 + 13 + 15 + 1233 = 1262 を出力してください。
入力例 2
4 6415986517946482
出力例 2
790534415
Score : 5 points
Problem Statement
You are given a string S of length 2^N consisting of characters 1, 2, \dots, 9.
For each non-negative integer n such that 0 \leq n \leq 2^N - 1, define f(n) as follows:
- List all non-negative integers i such that n \ \mathrm{OR} \ i = n, and sort them in increasing order. Let them be i_1, i_2, \dots, i_k. Here, \mathrm{OR} denotes the bitwise OR operation.
- Let T be the string formed by taking the (i_1+1)-th, (i_2+1)-th, \dots, (i_k+1)-th characters of S in this order.
- Interpret T as a decimal integer, and let its value be X. Then define f(n) = X \bmod 998244353.
Compute all values f(0), f(1), \dots, f(2^N - 1), and output the value of the following expression. Here, \mathrm{XOR} denotes the bitwise exclusive OR operation.
Constraints
- 1 \leq N \leq 22
- S is a string of length 2^N consisting of
1,2, \dots,9
Input
The input is given from standard input in the following format:
N S
Output
Print \displaystyle \sum_{n=0}^{2^N-1} \left(f(n) \ \mathrm{XOR} \ n\right).
Sample Input 1
2 1234
Sample Output 1
1262
For all n such that 0 \leq n \leq 2^N-1, the values of f(n) are:
- When n=0, f(n) = 1
- When n=1, f(n) = 12
- When n=2, f(n) = 13
- When n=3, f(n) = 1234
Therefore, output (1 \ \mathrm{XOR} \ 0) + (12 \ \mathrm{XOR} \ 1) + (13 \ \mathrm{XOR} \ 2) + (1234 \ \mathrm{XOR} \ 3) = 1 + 13 + 15 + 1233 = 1262.
Sample Input 2
4 6415986517946482
Sample Output 2
790534415
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 6 点
注意
この問題は他の問題に比べてマニアックな知識を問う問題です。
満点解法に心当たりの無い方は部分点が取れたら次の問題に行くことを推奨します。
問題文
N 種類の品物がそれぞれ無数にあります。i 種類目の品物は重さ i、価値 v_i です。
Q 個のクエリに答えてください。各クエリでは正整数 W が与えられるので、重さの総和がちょうど W になるように品物を集めた時の、集めた品物の価値の総和としてあり得る最大値を求めてください。
制約
- 1 \leq N \leq 4000
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq v_i \leq 10^9
- 1 \leq W \leq 10^9
- 入力される値は全て整数
部分点
この問題には部分点が設定されている。
- N \leq 300 であるデータセットに正解した場合、4 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N Q
v_1 v_2 \dots v_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
W
出力
Q 行出力せよ。i 行目には i 番目のクエリの答えを出力せよ。
入力例 1
4 9 2 1 7 4 1 2 3 4 5 6 7 8 9
出力例 1
2 4 7 9 11 14 16 18 21
例えば 6 番目のクエリでは W=6 です。この時、3 種類目の品物を 2 個集めると価値の総和は 7 + 7 = 14 になり、これが最大です。
入力例 2
10 10 104 231 361 478 661 765 963 1132 1402 1552 1 10 15 27 48 100 853822501 687675302 281611653 844033520
出力例 2
104 1552 2213 4206 7460 15572 133006571782 107124530332 43868837466 131481666104
Score : 6 points
Note
This problem requires more specialized knowledge compared to the others.
If you do not have an idea for a full solution, it is recommended to aim for partial points and then move on to the next problem.
Problem Statement
There are infinitely many items of each of N types. The i-th type of item has weight i and value v_i.
You are given Q queries. In each query, you are given a positive integer W.
Find the maximum possible total value when you choose some items such that the total weight is exactly W.
Constraints
- 1 \leq N \leq 4000
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq v_i \leq 10^9
- 1 \leq W \leq 10^9
- All input values are integers
Partial Score
This problem has partial scoring.
- If you solve the dataset with N \leq 300, you will get 4 points.
Input
The input is given from standard input in the following format:
N Q
v_1 v_2 \dots v_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
W
Output
Print Q lines. On the i-th line, output the answer for the i-th query.
Sample Input 1
4 9 2 1 7 4 1 2 3 4 5 6 7 8 9
Sample Output 1
2 4 7 9 11 14 16 18 21
For example, in the 6th query, W=6.
If you take two items of type 3, the total value becomes 7 + 7 = 14, which is the maximum.
Sample Input 2
10 10 104 231 361 478 661 765 963 1132 1402 1552 1 10 15 27 48 100 853822501 687675302 281611653 844033520
Sample Output 2
104 1552 2213 4206 7460 15572 133006571782 107124530332 43868837466 131481666104
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 7 点
問題文
あなたはゲームをすることにしました。ゲームは N 日間の行動からなります。あなたは毎日整数 x \in \lbrace 0,1,2 \rbrace を 1 つ選び、x 円を支払って行動 x を行います。i 日目に行動 x を行うと A_{i,x} の経験値を得ます。1 日に複数回行動を行うことは出来ません。
Q 個のクエリに答えてください。クエリでは 1 \leq d \leq N, 0 \leq b \leq 2d を満たす整数の組 (d,b) が与えられるので、次の問題を解いてください。
- 1 日目から d 日目までに合計でちょうど b 円支払ったとする。この時、1 日目から d 日目までに得た経験値の総和としてあり得る最大値を求めよ。
T 個のテストケースが与えられるので、それぞれについて問題を解いてください。
制約
- 1 \leq T \leq 10^4
- 1 \leq N \leq 2.5 \times 10^5
- 1 \leq Q \leq 10^4
- 0 \leq A_{i,x} \leq 10^9
- 1 \leq d \leq N
- 0 \leq b \leq 2d
- 全てのテストケースに対する N の総和は 2.5 \times 10^5 以下
- 全てのテストケースに対する Q の総和は 10^4 以下
- 入力される値は全て整数
部分点
この問題には部分点が設定されている。
- 全てのクエリに対して d = N が成り立つデータセットに正解した場合、5 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N Q
A_{1,0} A_{1,1} A_{1,2}
A_{2,0} A_{2,1} A_{2,2}
\vdots
A_{N,0} A_{N,1} A_{N,2}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
d b
出力
各テストケースの答えをテストケースが与えられた順に出力せよ。
各テストケースでは Q 行出力せよ。i 行目には i 番目のクエリへの答えを出力せよ。
入力例 1
2 3 3 1 3 2 4 8 1 1 6 9 1 1 2 3 3 3 5 5 45 58 82 47 39 94 36 54 74 80 61 95 61 57 69 2 4 5 7 4 1 5 5 3 0
出力例 1
3 10 18 176 387 226 371 128
例えば 1 番目のテストケースの 3 番目のクエリを考えます。d=3,b=3 です。
このとき、以下の方法で合計 18 の経験値を得ることが出来て、これが最大です。
- 1 日目:x=0 を選ぶ。0 円支払って A_{1,0}=1 の経験値を得る。
- 2 日目:x=1 を選ぶ。1 円支払って A_{2,1}=8 の経験値を得る。
- 3 日目:x=2 を選ぶ。2 円支払って A_{3,2}=9 の経験値を得る。
入力例 2
1 10 10 76 30 16 30 94 48 60 67 90 43 63 47 49 33 66 14 49 79 39 62 37 34 79 96 29 86 85 59 42 69 10 16 10 13 10 8 10 20 10 2 10 5 10 4 10 0 10 15 10 19
出力例 2
764 770 724 633 554 664 634 433 780 679
Score : 7 points
Problem Statement
You are going to play a game. The game consists of actions over N days. Each day, you choose an integer x \in {0,1,2}, pay x yen, and perform action x. If you perform action x on day i, you gain A_{i,x} experience points. You cannot perform more than one action per day.
You are given Q queries. In each query, you are given a pair of integers (d, b) such that 1 \leq d \leq N and 0 \leq b \leq 2d. Solve the following problem:
- Suppose you spend exactly b yen in total from day 1 to day d. What is the maximum possible total experience you can gain during these d days?
You are given T test cases. Solve each of them.
Constraints
- 1 \leq T \leq 10^4
- 1 \leq N \leq 2.5 \times 10^5
- 1 \leq Q \leq 10^4
- 0 \leq A_{i,x} \leq 10^9
- 1 \leq d \leq N
- 0 \leq b \leq 2d
- The sum of N over all test cases is at most 2.5 \times 10^5
- The sum of Q over all test cases is at most 10^4
- All input values are integers
Partial Score
This problem has partial scoring.
- If all queries satisfy d = N, you will get 5 points.
Input
The input is given from standard input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N Q
A_{1,0} A_{1,1} A_{1,2}
A_{2,0} A_{2,1} A_{2,2}
\vdots
A_{N,0} A_{N,1} A_{N,2}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
d b
Output
Output the answers for all test cases in order.
For each test case, print Q lines. On the i-th line, output the answer to the i-th query.
Sample Input 1
2 3 3 1 3 2 4 8 1 1 6 9 1 1 2 3 3 3 5 5 45 58 82 47 39 94 36 54 74 80 61 95 61 57 69 2 4 5 7 4 1 5 5 3 0
Sample Output 1
3 10 18 176 387 226 371 128
For example, consider the 3-rd query of the first test case: d=3, b=3.
You can achieve a total experience of 18 in the following way, which is optimal:
- Day 1: choose x=0. Pay 0 yen and gain A_{1,0}=1 experience.
- Day 2: choose x=1. Pay 1 yen and gain A_{2,1}=8 experience.
- Day 3: choose x=2. Pay 2 yen and gain A_{3,2}=9 experience.
Sample Input 2
1 10 10 76 30 16 30 94 48 60 67 90 43 63 47 49 33 66 14 49 79 39 62 37 34 79 96 29 86 85 59 42 69 10 16 10 13 10 8 10 20 10 2 10 5 10 4 10 0 10 15 10 19
Sample Output 2
764 770 724 633 554 664 634 433 780 679
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 7 点
問題文
正整数 n に対して f(n) を以下の手順によって得られる整数とします。
- n が 10 進表記で d 桁であるとする。長さ d の数列 A=(A_1,\dots,A_d) を以下のように定める。
- A_i は「n の 10 進表記を文字列とみなした時の左から i 文字目の数字」を数とみなした値である。
- A の最長狭義増加部分列の長さを f(n) とする。
例えば f(347) = 3, f(1192) = 2, f(10123456789) = 10, f(11111) = 1 です。
正整数 N が与えられます。
正整数 x であって、x を x+f(x) に変換する操作を 0 回以上繰り返して N を得ることが出来るものの個数を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 2 \times 10^4
- 1 \leq N \leq 10^{18}
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N
出力
T 行出力せよ。i 行目には i 番目のテストケースへの答えを出力せよ。
入力例 1
6 7 110 1000000000000000000 567784738694904180 555056967895592095 942135357890920474
出力例 1
7 5 1000000000000000000 23644 22551 60795
例えば、2 番目のテストケースについて、x = 102 からはじめて x を x + f(x) に変換する操作を繰り返すと
- 102 \to 104 \to 106 \to 108 \to 110
となり N=110 を得ることが出来ます。条件を満たす x は x=102,104,106,108,110 の 5 個のみです。
Score : 7 points
Problem Statement
For a positive integer n, define f(n) as follows:
- Let n have d digits in decimal representation. Define a sequence A = (A_1, \dots, A_d) of length d as follows:
- A_i is the i-th digit from the left in the decimal representation of n.
- Let f(n) be the length of the longest strictly increasing subsequence of A.
For example, f(347) = 3, f(1192) = 2, f(10123456789) = 10, and f(11111) = 1.
You are given a positive integer N.
Count the number of positive integers x such that by repeatedly applying the operation x \to x + f(x) zero or more times, you can obtain N.
You are given T test cases. Solve each of them.
Constraints
- 1 \leq T \leq 2 \times 10^4
- 1 \leq N \leq 10^{18}
- All input values are integers
Input
The input is given from standard input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N
Output
Print T lines. On the i-th line, output the answer for the i-th test case.
Sample Input 1
6 7 110 1000000000000000000 567784738694904180 555056967895592095 942135357890920474
Sample Output 1
7 5 1000000000000000000 23644 22551 60795
For example, in the second test case, starting from x = 102 and repeatedly applying the operation x \to x + f(x):
- 102 \to 104 \to 106 \to 108 \to 110
we can obtain N = 110. The only values of x satisfying the condition are x = 102, 104, 106, 108, 110, so there are 5 in total.
実行時間制限: 10 sec / メモリ制限: 16 MiB
配点 : 7 点
注意
この問題はメモリ制限が非常に厳しいです。
問題文
整数 M および N 個の整数の組 (L_1, R_1), \dots, (L_N, R_N) が与えられます。ここで任意の 1 \leq i \leq N を満たす整数 i について 1 \leq L_i \leq R_i \leq M が成り立ちます。
K = 1, 2, \dots, M に対して以下の問題の答えを求めてください。
S \subseteq \lbrace 1, 2,\dots, N \rbrace である集合 S としてあり得るものは 2^N 通りありますが、そのうち以下の条件を満たすものは何個ありますか?答えを 998244353 で割った余りを求めてください。
- 1 以上 M 以下の整数 m のうち、以下を満たすものがちょうど K 個である。
- ある整数 i \in S が存在して、L_i \leq m \leq R_i が成り立つ。
制約
- 1 \leq N \leq 2000
- 1 \leq M \leq 4000
- 1 \leq L_i \leq R_i \leq M
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M L_1 R_1 L_2 R_2 \vdots L_N R_N
出力
M 行出力せよ。i 行目には K=i の時の答えを出力せよ。
入力例 1
3 4 1 2 3 4 2 4
出力例 1
0 2 2 3
K=1 の時、条件を満たす S は 0 個です。
K=2 の時、条件を満たす S は \lbrace 1 \rbrace, \lbrace 2 \rbrace の 2 個です。
K=3 の時、条件を満たす S は \lbrace 3 \rbrace, \lbrace 2,3 \rbrace の 2 個です。
K=4 の時、条件を満たす S は \lbrace 1,2 \rbrace, \lbrace 1,3 \rbrace, \lbrace 1,2,3 \rbrace の 3 個です。
入力例 2
8 10 6 10 1 4 5 9 6 8 1 5 7 9 4 6 4 8
出力例 2
0 0 3 2 15 25 26 20 72 92
Score : 7 points
Note
This problem has a very strict memory limit.
Problem Statement
You are given an integer M and N pairs of integers (L_1, R_1), \dots, (L_N, R_N). For each i (1 \leq i \leq N), it holds that 1 \leq L_i \leq R_i \leq M.
For each K = 1, 2, \dots, M, solve the following problem:
There are 2^N possible subsets S \subseteq \lbrace 1,2,\dots,N\rbrace. Among them, how many satisfy the following condition? Output the answer modulo 998244353.
- The number of integers m (1 \leq m \leq M) satisfying the following condition is exactly K:
- There exists an integer i \in S such that L_i \leq m \leq R_i.
Constraints
- 1 \leq N \leq 2000
- 1 \leq M \leq 4000
- 1 \leq L_i \leq R_i \leq M
- All input values are integers
Input
The input is given from standard input in the following format:
N M L_1 R_1 L_2 R_2 \vdots L_N R_N
Output
Print M lines. On the i-th line, output the answer for K = i.
Sample Input 1
3 4 1 2 3 4 2 4
Sample Output 1
0 2 2 3
When K=1, there are 0 subsets S that satisfy the condition.
When K=2, there are 2 such subsets: \lbrace 1\rbrace , \lbrace 2\rbrace.
When K=3, there are 2 such subsets: \lbrace 3\rbrace , \lbrace 2,3\rbrace.
When K=4, there are 3 such subsets: \lbrace 1,2\rbrace , \lbrace 1,3\rbrace , \lbrace 1,2,3\rbrace.
Sample Input 2
8 10 6 10 1 4 5 9 6 8 1 5 7 9 4 6 4 8
Sample Output 2
0 0 3 2 15 25 26 20 72 92
実行時間制限: 3.5 sec / メモリ制限: 1024 MiB
配点 : 7 点
問題文
非負整数 u, v に対して u \subseteq v を u\ \mathrm{OR}\ v = v という意味で用います。ここで \mathrm{OR} はビットごとの論理和です。
非負整数の 3 つ組の列 ((u_1,v_1,w_1),(u_2,v_2,w_2),\dots,(u_k,v_k,w_k)) が以下の条件を全て満たす時に 良い列 と呼びます。
- k \geq 2 である。
- 1 \leq i \leq k-1 を満たす整数 i 全てに対して u_i \subseteq w_{i+1} \subseteq v_i が成り立つ。
非負整数の 3 つ組の列 A = ((x_1,y_1,z_1), (x_2,y_2,z_2), \dots, (x_N,y_N,z_N)) が与えられます。ここで 1 \leq i \leq N を満たす整数 i 全てにおいて x_i \subseteq y_i が成り立ちます。
A の長さ 2 以上の部分列は取り出す位置の違いを区別すると 2^N - N - 1 通りありますが、それらの中に含まれる良い列の個数を 998244353 で割った余りを求めてください。
制約
- 2 \leq N \leq 3 \times 10^5
- 0 \leq x_i \lt 2^{24}
- 0 \leq y_i \lt 2^{24}
- 0 \leq z_i \lt 2^{24}
- x_i \ \mathrm{OR}\ y_i = y_i
- 入力される値は全て整数
部分点
この問題には部分点が設定されている。
- 1 \leq i \leq N を満たす整数 i 全てに対して \max(x_i, y_i, z_i) \lt 2^{18} が成り立つデータセットに正解した場合、4 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 z_1 x_2 y_2 z_2 \vdots x_N y_N z_N
出力
A の長さ 2 以上の部分列である良い列の個数を 998244353 で割った余りを出力せよ。
入力例 1
3 2 6 3 0 7 4 1 5 2
出力例 1
2
条件を満たす列は次の 2 通りです。
- ((2,6,3),(1,5,2))
- ((0,7,4),(1,5,2))
入力例 2
8 0 9 5 0 16 3 0 8 4 0 5 23 0 20 29 0 12 26 0 6 0 0 19 8
出力例 2
9
入力例 3
13 0 16777215 14827221 8390656 12582911 5535137 4325888 16711679 2641072 8849409 16776703 15212885 8389632 16514814 9612654 0 12517373 13419138 0 16777215 5271292 14464 16236535 6997452 131072 15727483 10218943 16 16646111 15524257 1181961 14647295 5595336 8388768 16775147 5472548 0 16777215 297940
出力例 3
34
Score : 7 points
Problem Statement
For non-negative integers u, v, we write u \subseteq v to mean u\ \mathrm{OR}\ v = v. Here, \mathrm{OR} denotes the bitwise OR operation.
A sequence of triples of non-negative integers ((u_1,v_1,w_1),(u_2,v_2,w_2),\dots,(u_k,v_k,w_k)) is called a good sequence if it satisfies all of the following conditions:
- k \geq 2.
- For all integers i such that 1 \leq i \leq k-1, it holds that u_i \subseteq w_{i+1} \subseteq v_i.
You are given a sequence of triples A = ((x_1,y_1,z_1), (x_2,y_2,z_2), \dots, (x_N,y_N,z_N)).
For all i such that 1 \leq i \leq N, it holds that x_i \subseteq y_i.
There are 2^N - N - 1 subsequences of A with length at least 2 (counting different choices of indices separately).
Among them, find the number of good sequences, modulo 998244353.
Constraints
- 2 \leq N \leq 3 \times 10^5
- 0 \leq x_i < 2^{24}
- 0 \leq y_i < 2^{24}
- 0 \leq z_i < 2^{24}
- x_i \ \mathrm{OR}\ y_i = y_i
- All input values are integers
Partial Score
This problem has partial scoring.
- If you solve the dataset where \max(x_i, y_i, z_i) < 2^{18} holds for every integer i such that 1 \leq i \leq N, you will get 4 points.
Input
The input is given from standard input in the following format:
N x_1 y_1 z_1 x_2 y_2 z_2 \vdots x_N y_N z_N
Output
Print the number of good sequences that are subsequences of A with length at least 2, modulo 998244353.
Sample Input 1
3 2 6 3 0 7 4 1 5 2
Sample Output 1
2
There are 2 sequences that satisfy the condition:
- ((2,6,3),(1,5,2))
- ((0,7,4),(1,5,2))
Sample Input 2
8 0 9 5 0 16 3 0 8 4 0 5 23 0 20 29 0 12 26 0 6 0 0 19 8
Sample Output 2
9
Sample Input 3
13 0 16777215 14827221 8390656 12582911 5535137 4325888 16711679 2641072 8849409 16776703 15212885 8389632 16514814 9612654 0 12517373 13419138 0 16777215 5271292 14464 16236535 6997452 131072 15727483 10218943 16 16646111 15524257 1181961 14647295 5595336 8388768 16775147 5472548 0 16777215 297940
Sample Output 3
34
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 8 点
問題文
縦横 N マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
辺で隣接するマスの間の状態は文字 c で表されて、
- c =
.の時、マスの間には何もなく、自由に通行可能です。 - c =
Dの時、マスの間には扉があり、自由に通行可能です。 - c =
Aの時、マスの間には A と呼ばれる扉があり、自由に通行可能です。 - c =
Bの時、マスの間には B と呼ばれる扉があり、自由に通行可能です。 - c =
#の時、マスの間には壁があり、通行は不可能です。
ここで 扉 A, 扉 B はグリッド上にそれぞれちょうど 1 枚あります。
グリッド上の辺で隣接するマスの間の状態は文字列 S_1, S_2, \dots, S_{N-1}, T_1, T_2, \dots, T_N によって表されて、
- 1 \leq i \leq N-1, 1 \leq j \leq N について (i,j) と (i+1,j) の間の状態は S_{i,j}、
- 1 \leq i \leq N, 1 \leq j \leq N-1 について (i,j) と (i,j+1) の間の状態は T_{i,j}
となっています。
(1, 1) からスタートして、通行が不可能な場所を通らないように上下左右のマスへの移動を繰り返して (N, N) まで移動することができるグリッドの状態を 良い状態 と呼びます。
あなたは次の条件を満たすように、扉 A, 扉 B 以外の扉のうちいくつかを封鎖して通行不可能にします。
- グリッドは良い状態である。
- 追加で 扉 A, 扉 B のちょうど一方を封鎖しても、グリッドは良い状態である。
- 追加で 扉 A, 扉 B の両方を封鎖した時に、グリッドは良い状態でなくなる。
条件を満たすことは可能ですか?可能である場合は最小で何枚の扉を封鎖すれば条件を満たすかを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 10^5
- 2 \leq N \leq 40
- S_i は
.,D,A,B,#からなる長さ N の文字列 - T_i は
.,D,A,B,#からなる長さ N-1 の文字列 AとBは S_{i,j}, T_{i,j} の中にそれぞれちょうど 1 回現れる- 全てのテストケースに対する N^2 の総和は 2 \times 10^5 以下
- 全てのテストケースに対する N^4 の総和は 40^4 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N
S_1
S_2
\vdots
S_{N-1}
T_1
T_2
\vdots
T_N
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、条件を満たすことが可能である場合は封鎖する必要がある扉の最小枚数を、不可能である場合は -1 を出力せよ。
入力例 1
6 2 .A . B 2 .A # B 3 #D. #BD .A .# .. 4 DDBD ..#D #D.D #DD #DD .A. #.# 4 D.#D DD#. D.B. #D. .#D ... .DA 9 DDD.D#DDD DDDADDDDD DD.DDDDDD DDDD#.DDD DD#DDDD.# DDDDD#.#D DD.#..DDD DDDDD#D.D DDD...DD D#.D#D#D D#DD#D.# DDD#DD## BDDD.D#D DD#DDDDD DDDDD#DD DDD#DDDD ##DDD.#D
出力例 1
0 -1 0 3 -1 7
例えば、1 番目のテストケースはすでに条件を満たしています。
Score : 8 points
Problem Statement
You are given an N \times N grid. Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The state between adjacent cells (sharing an edge) is represented by a character c:
- If c =
., there is nothing between the cells, and you can pass freely. - If c =
D, there is a door, and you can pass freely. - If c =
A, there is a special door called A, and you can pass freely. - If c =
B, there is a special door called B, and you can pass freely. - If c =
#, there is a wall, and you cannot pass.
Here, there is exactly one door A and one door B in the grid.
The states between adjacent cells are given by strings S_1, S_2, \dots, S_{N-1} and T_1, T_2, \dots, T_N:
- For 1 \leq i \leq N-1, 1 \leq j \leq N, the state between (i,j) and (i+1,j) is S_{i,j}.
- For 1 \leq i \leq N, 1 \leq j \leq N-1, the state between (i,j) and (i,j+1) is T_{i,j}.
A grid is called a good state if you can start from (1,1) and reach (N,N) by moving up, down, left, or right without passing through walls.
You will block some of the doors (except A and B), making them impassable, so that the following conditions are satisfied:
- The grid is in a good state.
- Even if you additionally block exactly one of A or B, the grid is still in a good state.
- If you additionally block both A and B, the grid is no longer in a good state.
Is it possible to satisfy these conditions? If it is possible, find the minimum number of doors you need to block.
You are given T test cases. Solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 2 \leq N \leq 40
- Each S_i is a string of length N consisting of
.,D,A,B,# - Each T_i is a string of length N-1 consisting of
.,D,A,B,# AandBeach appear exactly once among all S_{i,j} and T_{i,j}- The sum of N^2 over all test cases is at most 2 \times 10^5
- The sum of N^4 over all test cases is at most 40^4
Input
The input is given from standard input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N
S_1
S_2
\vdots
S_{N-1}
T_1
T_2
\vdots
T_N
Output
Print T lines. On the i-th line, output the answer for the i-th test case.
For each test case, if it is possible to satisfy the conditions, output the minimum number of doors that need to be blocked; otherwise, output -1.
Sample Input 1
6 2 .A . B 2 .A # B 3 #D. #BD .A .# .. 4 DDBD ..#D #D.D #DD #DD .A. #.# 4 D.#D DD#. D.B. #D. .#D ... .DA 9 DDD.D#DDD DDDADDDDD DD.DDDDDD DDDD#.DDD DD#DDDD.# DDDDD#.#D DD.#..DDD DDDDD#D.D DDD...DD D#.D#D#D D#DD#D.# DDD#DD## BDDD.D#D DD#DDDDD DDDDD#DD DDD#DDDD ##DDD.#D
Sample Output 1
0 -1 0 3 -1 7
For example, in the first test case, the conditions are already satisfied.
実行時間制限: 10 sec / メモリ制限: 1024 MiB
配点 : 10 点
問題文
頂点に 1 から N の番号が付いた N 頂点の木が与えられます。i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。
頂点の集合 S が次の条件を満たす時、S を 独立集合 と呼びます。
- S に含まれる任意の異なる 2 頂点 u, v について、u と v は木の上で隣接していない。
頂点 v について、v \in S を満たす独立集合全てからなる集合を F_v と表します。
Q 個のクエリを処理してください。クエリでは 1 \leq v \leq N を満たす整数 v および整数 q が与えられるので、
を計算してください。ここで |S| は集合のサイズを意味します。
制約
- 2 \leq N \leq 1.3 \times 10^5
- 1 \leq Q \leq 1.3 \times 10^5
- 1 \leq u_i \lt v_i \leq N
- 入力で与えられるグラフは木
- 1 \leq v \leq N
- 1 \leq q \lt 998244353
- 入力される値は全て整数
部分点
この問題には部分点が設定されている。
- 全てのクエリにおいて v=1 が成り立つデータセットに正解した場合、5 点が与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N Q
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
v q
出力
Q 行出力せよ。i 行目には i 番目のクエリへの答えを出力せよ。
入力例 1
4 2 1 2 1 3 2 4 1 1 2 3
出力例 1
2 12
1 番目のクエリについて考えます。頂点 1 を含む独立集合は \lbrace 1 \rbrace と \lbrace 1, 4 \rbrace の 2 個です。よって 1^1 + 1^2 = 2 を出力します。
入力例 2
10 10 1 2 2 3 1 4 1 5 1 6 6 7 6 8 5 9 1 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10
出力例 2
16 162 768 2500 6480 14406 28672 52488 90000 146410
入力例 3
10 10 1 2 1 3 2 4 4 5 4 6 2 7 5 8 8 9 6 10 5 844033520 8 780395612 2 285523486 6 13801767 3 487663185 3 667406485 7 672229269 7 207478896 5 769551740 7 806405364
出力例 3
665599367 675643489 193550820 987507475 230555342 555586355 204648376 83113599 299301383 545057926
Score : 10 points
Problem Statement
You are given a tree with N vertices, numbered from 1 to N. The i-th edge connects vertices u_i and v_i.
A set of vertices S is called an independent set if it satisfies the following condition:
- For any two distinct vertices u, v \in S, u and v are not adjacent in the tree.
For a vertex v, let F_v be the set of all independent sets S such that v \in S.
You are given Q queries. In each query, you are given an integer v (1 \leq v \leq N) and an integer q. Compute:
Here, |S| denotes the size of the set S.
Constraints
- 2 \leq N \leq 1.3 \times 10^5
- 1 \leq Q \leq 1.3 \times 10^5
- 1 \leq u_i < v_i \leq N
- The given graph is a tree
- 1 \leq v \leq N
- 1 \leq q < 998244353
- All input values are integers
Partial Score
This problem has partial scoring.
- If all queries satisfy v=1, you will get 5 points.
Input
The input is given from standard input in the following format:
N Q
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format:
v q
Output
Print Q lines. On the i-th line, output the answer to the i-th query.
Sample Input 1
4 2 1 2 1 3 2 4 1 1 2 3
Sample Output 1
2 12
Consider the first query. The independent sets that contain vertex 1 are {1} and {1, 4}, so there are 2 such sets. Therefore, output 1^1 + 1^2 = 2.
Sample Input 2
10 10 1 2 2 3 1 4 1 5 1 6 6 7 6 8 5 9 1 10 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10
Sample Output 2
16 162 768 2500 6480 14406 28672 52488 90000 146410
Sample Input 3
10 10 1 2 1 3 2 4 4 5 4 6 2 7 5 8 8 9 6 10 5 844033520 8 780395612 2 285523486 6 13801767 3 487663185 3 667406485 7 672229269 7 207478896 5 769551740 7 806405364
Sample Output 3
665599367 675643489 193550820 987507475 230555342 555586355 204648376 83113599 299301383 545057926