実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
ある人物の名前 S が与えられます。S の先頭の文字は英大文字であり、先頭以外の文字は英小文字です。
この人物の侍女の名前は、S の頭文字を英小文字に直し、先頭に Of をつけて得られる文字列です。この侍女の名前を答えてください。
制約
- S は長さ 1 以上 10 以下の文字列
- S の先頭の文字は英大文字
- S の先頭以外の文字は英小文字
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを 1 行に出力せよ。
入力例 1
Glen
出力例 1
Ofglen
Glen の頭文字を英小文字に直すと glen となり、さらに先頭に Of をつけると Ofglen となります。
入力例 2
I
出力例 2
Ofi
入力例 3
Fred
出力例 3
Offred
Score : 100 points
Problem Statement
You are given the name S of a certain person. The first character of S is an uppercase English letter, and the other characters are lowercase English letters.
The name of this person's handmaid is the string obtained by converting the first letter of S to lowercase and adding Of to the beginning. Find the name of this handmaid.
Constraints
- S is a string of length between 1 and 10, inclusive.
- The first character of S is an uppercase English letter.
- The characters of S other than the first are lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer on one line.
Sample Input 1
Glen
Sample Output 1
Ofglen
Converting the first letter of Glen to lowercase gives glen, and adding Of to the beginning gives Ofglen.
Sample Input 2
I
Sample Output 2
Ofi
Sample Input 3
Fred
Sample Output 3
Offred
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人の客がおり、1 から N までの番号が付けられています。また、M 本の缶ジュースがあり、1 から M までの番号が付けられています。
客 i (1 \leq i \leq N) は長さ L_i の希望リストを持っています。客 i の希望リストの先頭から j 番目 (1 \leq j \leq L_i) は缶ジュース X_{i,j} です。任意の客 i に対して、客 i の希望リストに載っている番号 X_{i, 1}, \dots, X_{i, L_i} は相異なります。
これから客 1, \dots, N が番号の小さいほうから順に、以下にしたがって自分が飲む飲料を選びます。
- その時点で誰にも選ばれていない缶ジュースの番号が自分の希望リストに存在する場合、そのうち先頭に最も近い番号の缶ジュースを選ぶ。そうでない場合は水を選ぶ。
それぞれの客がどの飲料を得るかを求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq L_i \leq M (1 \leq i \leq N)
- 1 \leq X_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq L_i)
- X_{i, 1}, \dots, X_{i, L_i} は相異なる (1 \leq i \leq N)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M
L_1
X_{1,1} X_{1,2} \cdots X_{1,L_1}
L_2
X_{2,1} X_{2,2} \cdots X_{2,L_2}
\vdots
L_N
X_{N,1} X_{N,2} \cdots X_{N,L_N}
出力
N 行出力せよ。i 行目 (1 \leq i \leq N) には、客 i が缶ジュースを得る場合はその番号を、水を得る場合は 0 を出力せよ。
入力例 1
4 5 3 3 1 2 3 3 2 1 2 2 3 4 2 5 3 1
出力例 1
3 2 0 5
客 1 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 3,1,2 です。このうち先頭に最も近いのは 3 なので、客 1 は缶ジュース 3 を選びます。
客 2 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 2,1 です。このうち先頭に最も近いのは 2 なので、客 2 は缶ジュース 2 を選びます。
客 3 の希望リストにある番号について、対応する缶ジュースはすべてその時点で誰かに選ばれています。よって客 3 は水を選びます。
客 4 の希望リストにある番号のうち、対応する缶ジュースが誰にも選ばれていないのは 5,1 です。このうち先頭に最も近いのは 5 なので、客 4 は缶ジュース 5 を選びます。
入力例 2
6 5 1 3 2 3 5 5 5 3 1 4 2 5 5 1 3 4 2 5 3 4 1 5 2 5 5 1 3 2 4
出力例 2
3 5 1 4 2 0
Score : 200 points
Problem Statement
There are N customers numbered 1 to N, and M canned juices numbered 1 to M.
Customer i (1 \leq i \leq N) has a wish list of length L_i. The j-th item (1 \leq j \leq L_i) from the top of customer i's wish list is canned juice X_{i,j}. For any customer i, the numbers X_{i, 1}, \dots, X_{i, L_i} on customer i's wish list are distinct.
Customers 1, \dots, N, in this order, will now choose their beverages, following the procedure below.
- If the customer's wish list contains a canned juice that has not yet been chosen by anyone at that point, they choose the canned juice whose number appears earliest in their wish list. Otherwise, they choose water.
Determine which beverage each customer gets.
Constraints
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq L_i \leq M (1 \leq i \leq N)
- 1 \leq X_{i,j} \leq M (1 \leq i \leq N, 1 \leq j \leq L_i)
- X_{i, 1}, \dots, X_{i, L_i} are distinct. (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
L_1
X_{1,1} X_{1,2} \cdots X_{1,L_1}
L_2
X_{2,1} X_{2,2} \cdots X_{2,L_2}
\vdots
L_N
X_{N,1} X_{N,2} \cdots X_{N,L_N}
Output
Output N lines. The i-th line (1 \leq i \leq N) should contain the number of the canned juice customer i gets if they get one, or 0 if customer i gets water.
Sample Input 1
4 5 3 3 1 2 3 3 2 1 2 2 3 4 2 5 3 1
Sample Output 1
3 2 0 5
Among the numbers on customer 1's wish list, the canned juices not yet chosen by anyone are 3, 1, 2. The one appearing earliest in the list is 3, so customer 1 chooses canned juice 3.
Among the numbers on customer 2's wish list, the canned juices not yet chosen by anyone are 2, 1. The one appearing earliest in the list is 2, so customer 2 chooses canned juice 2.
For the numbers on customer 3's wish list, all corresponding canned juices have already been chosen by someone at that point. Thus, customer 3 chooses water.
Among the numbers on customer 4's wish list, the canned juices not yet chosen by anyone are 5, 1. The one appearing earliest in the list is 5, so customer 4 chooses canned juice 5.
Sample Input 2
6 5 1 3 2 3 5 5 5 3 1 4 2 5 5 1 3 4 2 5 3 4 1 5 2 5 5 1 3 2 4
Sample Output 2
3 5 1 4 2 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
AtCoder レストランは開店してから N 日間営業しました。
開店してから i 日目 (1\leq i\leq N) には次の行動が行われました。
- i 日目の朝に、A_i 個の卵を仕入れる。
- i 日目の昼に、B_i 個の卵を使用する。このとき、卵は 在庫にある卵の中で仕入れた順に使用される 。
- i 日目の夜に、仕入れてから D 日間以上経過した卵をすべて処分する。
なお、1 日目の朝の前の時点で卵の在庫は無く、それぞれの日の昼に卵が足りなくなることはありませんでした。
N 日目の夜の行動の後で、レストランに何個の卵が残っているか求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T\leq 2\times 10^5
- 1 \leq D \leq N \leq 2\times 10^5
- 1 \leq A_i,B_i \leq 10
- N 日間のそれぞれの昼において、卵が足りなくなることはない。
- それぞれの入力において、各テストケースにおける N の総和は 2\times 10^5 以下である。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 個目のテストケースを表す。
各テストケースは以下の形式で与えられる。
N D A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
T 行出力せよ。
i 行目 (1\leq i\leq T) には、i 個目のテストケースに対する答えを出力せよ。
入力例 1
3 3 1 7 2 3 1 3 2 3 2 7 2 3 1 3 2 2 1 2 1 1 2
出力例 1
3 5 0
1 個目のテストケースにおいて次の行動が行われます。
- 最初、AtCoder レストランには卵がありません。
- 1 日目の朝に卵を 7 個仕入れます。レストランには 1 日目に仕入れた卵が 7 個あります。
- 1 日目の昼に卵を 1 個使用します。レストランには 1 日目に仕入れた卵が 6 個残っています。
- 1 日目の夜に処分する卵はありません。レストランには 1 日目に仕入れた卵が 6 個残っています。
- 2 日目の朝に卵を 2 個仕入れます。レストランには 1 日目に仕入れた卵が 6 個、2 日目に仕入れた卵が 2 個あります。
- 2 日目の昼に卵を 3 個使用します。レストランには 1 日目に仕入れた卵が 3 個、2 日目に仕入れた卵が 2 個残っています。
- 2 日目の夜に、1 日目に仕入れた卵を処分します。レストランには 2 日目に仕入れた卵が 2 個残っています。
- 3 日目の朝に卵を 3 個仕入れます。レストランには 2 日目に仕入れた卵が 2 個、3 日目に仕入れた卵が 3 個あります。
- 3 日目の昼に卵を 2 個使用します。レストランには 3 日目に仕入れた卵が 3 個残っています。
- 3 日目の夜に処分する卵はありません。(2 日目に仕入れた卵は全て使用されているためです。)レストランには 3 日目に仕入れた卵が 3 個残っています。
よって、3 日目の夜の行動の後で、卵は 3 個残っているため、1 行目には 3 を出力します。
2 個目のテストケースにおいては、3 日目の夜に 1 日目に仕入れた卵を処分した後の個数を出力することに注意してください。
Score : 300 points
Problem Statement
AtCoder Restaurant was open for N days after opening.
On day i (1\leq i\leq N) after opening, the following actions were performed.
- In the morning of day i, A_i eggs are purchased.
- At noon on day i, B_i eggs are used. Here, eggs in stock are used in the order they were purchased.
- In the evening of day i, all eggs that have been stocked for D or more days are discarded.
There were no eggs in stock before the morning of day 1, and eggs never ran out at noon on any day.
Find how many eggs remain in the restaurant after the evening action on day N.
T test cases are given; solve each.
Constraints
- 1 \leq T\leq 2\times 10^5
- 1 \leq D \leq N \leq 2\times 10^5
- 1 \leq A_i,B_i \leq 10
- Eggs never run out at noon on any of the N days.
- For each input, the sum of N 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
\mathrm{case}_i represents the i-th test case.
Each test case is given in the following format:
N D A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Output T lines.
The i-th line (1\leq i\leq T) should contain the answer for the i-th test case.
Sample Input 1
3 3 1 7 2 3 1 3 2 3 2 7 2 3 1 3 2 2 1 2 1 1 2
Sample Output 1
3 5 0
In the first test case, the following actions are performed.
- Initially, AtCoder Restaurant has no eggs.
- In the morning of day 1, 7 eggs are purchased. The restaurant has 7 eggs stocked on day 1.
- At noon on day 1, 1 egg is used. The restaurant has 6 eggs stocked on day 1 remaining.
- In the evening of day 1, no eggs are discarded. The restaurant has 6 eggs stocked on day 1 remaining.
- In the morning of day 2, 2 eggs are purchased. The restaurant has 6 eggs stocked on day 1 and 2 eggs stocked on day 2.
- At noon on day 2, 3 eggs are used. The restaurant has 3 eggs stocked on day 1 and 2 eggs stocked on day 2 remaining.
- In the evening of day 2, the eggs stocked on day 1 are discarded. The restaurant has 2 eggs stocked on day 2 remaining.
- In the morning of day 3, 3 eggs are purchased. The restaurant has 2 eggs stocked on day 2 and 3 eggs stocked on day 3.
- At noon on day 3, 2 eggs are used. The restaurant has 3 eggs stocked on day 3 remaining.
- In the evening of day 3, no eggs are discarded. (This is because all eggs stocked on day 2 have already been used.) The restaurant has 3 eggs stocked on day 3 remaining.
Thus, 3 eggs remain after the evening action on day 3, so output 3 on the first line.
For the second test case, remember to output the number of eggs after discarding the eggs stocked on day 1 in the evening of day 3.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
整数列 A の部分列 B=(B_1,B_2,\ldots,B_{|B|}) であって、以下の条件を満たすものの長さの最大値を求めてください。
- 全ての 1\le i\le |B| - 1 を満たす整数 i に対し、 B_i+1=B_{i+1} が成り立つ。
部分列とは
数列 A の部分列とは、A の要素を 0 個以上選んで削除し、残った要素を元の順序を保って並べた数列のことを指します。制約
- 1\le N\le 2\times 10^5
- 1\le A_i\le 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
7 3 4 3 5 7 6 2
出力例 1
4
B=(3,4,5,6) は A の部分列であり条件を満たし、その長さは 4 です。
条件を満たす部分列であって長さが 4 より長いものは存在しないので、 4 を出力してください。
入力例 2
5 5 4 3 2 1
出力例 2
1
入力例 3
10 1 2 3 4 5 6 7 8 9 10
出力例 3
10
Score : 400 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
Find the maximum length of a subsequence B=(B_1,B_2,\ldots,B_{|B|}) of integer sequence A that satisfies the following condition.
- B_i+1=B_{i+1} for all integers i satisfying 1\le i\le |B| - 1.
What is a subsequence
A subsequence of sequence A is a sequence obtained by choosing zero or more elements of A to delete, and arranging the remaining elements in their original order.Constraints
- 1\le N\le 2\times 10^5
- 1\le A_i\le 10^9
- 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 answer.
Sample Input 1
7 3 4 3 5 7 6 2
Sample Output 1
4
B=(3,4,5,6) is a subsequence of A that satisfies the condition, and its length is 4.
There is no subsequence satisfying the condition with length greater than 4, so output 4.
Sample Input 2
5 5 4 3 2 1
Sample Output 2
1
Sample Input 3
10 1 2 3 4 5 6 7 8 9 10
Sample Output 3
10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
0 \leq x,y \leq M-1 を満たす整数組 (x, y) のうち、以下の漸化式で表される無限長の数列 (s_1, s_2, \dots) が M の倍数を全く含まないようなものは何通りありますか?
- s_1 = x
- s_2 = y
- s_n = A s_{n-1} + B s_{n-2} (n \geq 3)
制約
- 2 \leq M \leq 1000
- 0 \leq A, B \leq M-1
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
M A B
出力
答えを 1 行に出力せよ。
入力例 1
4 1 2
出力例 1
7
問題文中の条件を満たす整数組は (x,y) = (1,1), (1,3), (2,1), (2,2), (2,3), (3,1), (3,3) の 7 通りです。
たとえば (x,y) = (2,1) としたとき、対応する数列は (2,1,5,7,17,31,65,127,\dots) となります。この数列は 4 の倍数を全く含みません。よって、(x,y) = (2,1) は問題文中の条件を満たします。
一方で (x,y) = (3,2) としたとき、対応する数列は (3,2,8,12,28,52,108,212,\dots) となります。この数列の第 3 項は 8 であり、これは 4 の倍数です。よって、(x,y) = (3,2) は問題文中の条件を満たしません。
入力例 2
446 1 1
出力例 2
0
問題文中の条件を満たす整数組は存在しません。
入力例 3
1000 784 385
出力例 3
995373
Score : 450 points
Problem Statement
Among integer pairs (x, y) satisfying 0 \leq x,y \leq M-1, how many are there such that the infinite sequence (s_1, s_2, \dots) defined by the following recurrence relation contains no multiples of M?
- s_1 = x
- s_2 = y
- s_n = A s_{n-1} + B s_{n-2} (n \geq 3)
Constraints
- 2 \leq M \leq 1000
- 0 \leq A, B \leq M-1
- All input values are integers.
Input
The input is given from Standard Input in the following format:
M A B
Output
Output the answer on one line.
Sample Input 1
4 1 2
Sample Output 1
7
The integer pairs satisfying the condition in the problem statement are (x,y) = (1,1), (1,3), (2,1), (2,2), (2,3), (3,1), (3,3), for a total of seven pairs.
For example, when (x,y) = (2,1), the corresponding sequence is (2,1,5,7,17,31,65,127,\dots). This sequence contains no multiples of 4. Thus, (x,y) = (2,1) satisfies the condition in the problem statement.
On the other hand, when (x,y) = (3,2), the corresponding sequence is (3,2,8,12,28,52,108,212,\dots). The third term of this sequence is 8, which is a multiple of 4. Thus, (x,y) = (3,2) does not satisfy the condition in the problem statement.
Sample Input 2
446 1 1
Sample Output 2
0
No integer pairs satisfy the condition in the problem statement.
Sample Input 3
1000 784 385
Sample Output 3
995373
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の有向グラフ G が与えられます。
G の頂点は頂点 1 、頂点 2 、\ldots 、頂点 N と番号付けられており、
i 番目 (1\leq i\leq M) の辺は頂点 U_i から頂点 V_i へ向かう辺です。
k=1,2,\ldots,N について以下の問題を解いてください。
高橋君の目標は、G から(0 個でも良い)いくつかの頂点、およびそれらの頂点を始点または終点として持つすべての辺を削除することで、次の条件をみたすようにすることです。
- 頂点 1 から 0 本以上のいくつかの辺を辿って到達可能な頂点が頂点 1,2,\ldots,k のみである。
高橋君が目標を達成することが可能か判定し、可能な場合は目標を達成するために高橋君が最低何個の頂点を削除する必要があるか求めてください。
制約
- 2\leq N\leq 3\times 10^5
- 1\leq M\leq 3\times 10^5
- 1\leq U_i,V_i\leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
N 行出力せよ。
i 行目 (1\leq i\leq N) には、k=i のときの問題の答えを出力せよ。
ここで、各問題に対する答えとして、高橋君が目標を達成することが不可能な場合は -1 を、そうでない場合は目標を達成するために削除する必要のある頂点の個数の最小値を出力せよ。
入力例 1
5 5 1 2 2 4 2 5 4 3 4 5
出力例 1
1 2 -1 1 0
- k=1 のとき、頂点 2 を削除することによって目標を達成できます。このとき、頂点 3,4,5 は削除せずとも頂点 1 から到達できないことに注意してください。1 つも頂点を削除せずに目標を達成することはできないため、1 を 1 行目に出力します。
- k=2 のとき、頂点 4,5 を削除することにより目標を達成できます。1 つ以下の頂点を削除することによって目標を達成することは不可能なので、2 を 2 行目に出力します。
- k=3 のとき、高橋君はどのように頂点を削除しても目標を達成できません。よって、-1 を 3 行目に出力します。
- k=4 のとき、頂点 5 を削除することによって目標を達成できます。1 つも頂点を削除せずに目標を達成することはできないため、1 を 4 行目に出力します。
- k=5 のとき、頂点を削除することなく目標を達成できます。よって、0 を 5 行目に出力します。
入力例 2
5 5 1 1 1 2 3 1 3 1 3 5
出力例 2
1 0 -1 -1 -1
G は連結とは限りません。また、G は多重辺や自己ループを持つこともあります。
Score : 500 points
Problem Statement
You are given a directed graph G with N vertices and M edges.
The vertices of G are numbered as vertex 1, vertex 2, \ldots, vertex N, and the i-th edge (1\leq i\leq M) goes from vertex U_i to vertex V_i.
Solve the following problem for k=1,2,\ldots,N.
Takahashi's goal is to delete some (possibly zero) vertices from G, along with all edges having those vertices as an endpoint, so that the following condition is satisfied.
- The only vertices reachable from vertex 1 by traversing zero or more edges are vertices 1,2,\ldots,k.
Determine whether he can achieve his goal, and if so, find the minimum number of vertices he needs to delete to achieve it.
Constraints
- 2\leq N\leq 3\times 10^5
- 1\leq M\leq 3\times 10^5
- 1\leq U_i,V_i\leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
Output N lines.
The i-th line (1\leq i\leq N) should contain the answer to the problem for k=i.
Here, for each problem, output -1 if it is impossible for Takahashi to achieve his goal, and otherwise output the minimum number of vertices that need to be deleted to achieve his goal.
Sample Input 1
5 5 1 2 2 4 2 5 4 3 4 5
Sample Output 1
1 2 -1 1 0
- For k=1, the goal can be achieved by deleting vertex 2. Note that vertices 3,4,5 are already unreachable from vertex 1 without deleting them. The goal cannot be achieved without deleting at least one vertex, so output 1 on the first line.
- For k=2, the goal can be achieved by deleting vertices 4 and 5. It is impossible to achieve the goal by deleting one or fewer vertices, so output 2 on the second line.
- For k=3, Takahashi cannot achieve the goal no matter how he deletes vertices. Thus, output -1 on the third line.
- For k=4, the goal can be achieved by deleting vertex 5. The goal cannot be achieved without deleting at least one vertex, so output 1 on the fourth line.
- For k=5, the goal can be achieved without deleting any vertices. Thus, output 0 on the fifth line.
Sample Input 2
5 5 1 1 1 2 3 1 3 1 3 5
Sample Output 2
1 0 -1 -1 -1
G is not necessarily connected. Also, G may have multi-edges or self-loops.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
正整数からなる数列 X = (X_1, \dots, X_n) に対し、その連長圧縮の任意の (長さ・値) ペアにおいて長さと値が等しいとき、かつそのときに限り、X を 221 数列 と呼びます。より厳密には、以下の条件を満たす数列を 221 数列であるとします。
- 1 \leq l \leq r \leq n を満たす整数組 (l, r) が以下の 3 つの条件をすべて満たすならば、必ず (r-l+1) = X_l が成り立つ。
- l = 1 または (l \geq 2 かつ X_{l-1} \neq X_l)
- r = n または (r \leq n-1 かつ X_{r+1} \neq X_r)
- X_l = X_{l+1} = \dots = X_r
たとえば、(2,2,3,3,3,1,2,2) は 221 数列ですが、(1,1) や (4,4,1,4,4) は 221 数列ではありません。
長さ N の正整数列 A = (A_1, \dots, A_N) が与えられます。A の空でない(連続とは限らない)部分列であって、221 数列であるものの個数を 998244353 で割ったあまりを求めてください。 ただし、異なる位置から取り出した部分列であっても、数列として一致するものは区別せずまとめて 1 通りとして数えます。
制約
- 1 \leq N \leq 500\,000
- 1 \leq A_i \leq N
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \cdots A_N
出力
答えを 1 行に出力せよ。
入力例 1
8 2 1 2 1 1 2 7 2
出力例 1
5
A の空でない部分列として現れる 221 数列は以下の 5 通りです。
- (1)
- (1,2,2)
- (2,2)
- (2,2,1)
- (2,2,1,2,2)
入力例 2
5 2 3 4 5 4
出力例 2
0
A の空でない部分列として現れる 221 数列は存在しません。
入力例 3
20 2 2 3 1 1 4 1 4 1 4 2 4 1 2 1 4 4 1 1 4
出力例 3
15
Score : 600 points
Problem Statement
For a sequence X = (X_1, \dots, X_n) of positive integers, we call X a 221 sequence if and only if the length and value are equal for every (length, value) pair in its run-length encoding. More formally, a sequence satisfying the following condition is called a 221 sequence.
- For any integer pair (l, r) satisfying 1 \leq l \leq r \leq n, if all three of the following conditions hold, then (r-l+1) = X_l.
- l = 1 or (l \geq 2 and X_{l-1} \neq X_l)
- r = n or (r \leq n-1 and X_{r+1} \neq X_r)
- X_l = X_{l+1} = \dots = X_r
For example, (2,2,3,3,3,1,2,2) is a 221 sequence, but (1,1) and (4,4,1,4,4) are not 221 sequences.
You are given a sequence A = (A_1, \dots, A_N) of positive integers of length N. Find the number, modulo 998244353, of non-empty (not necessarily contiguous) subsequences of A that are 221 sequences. Even if two subsequences are extracted from different positions, they are counted as one if they are equal as sequences.
Constraints
- 1 \leq N \leq 500\,000
- 1 \leq A_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \cdots A_N
Output
Output the answer on one line.
Sample Input 1
8 2 1 2 1 1 2 7 2
Sample Output 1
5
The 221 sequences that appear as non-empty subsequences of A are the following five:
- (1)
- (1,2,2)
- (2,2)
- (2,2,1)
- (2,2,1,2,2)
Sample Input 2
5 2 3 4 5 4
Sample Output 2
0
No 221 sequences appear as non-empty subsequences of A.
Sample Input 3
20 2 2 3 1 1 4 1 4 1 4 2 4 1 2 1 4 4 1 1 4
Sample Output 3
15