Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
以下の 5 つの日を五節句と呼びます。
- 1 月 \color{red}{\mathbf{7}} 日
- 3 月 3 日
- 5 月 5 日
- 7 月 7 日
- 9 月 9 日
M 月 D 日が五節句に含まれるならば Yes を、含まれないならば No を出力してください。
制約
- M, D は整数
- M 月 D 日はグレゴリオ暦における閏年の日付として正しい。
入力
入力は以下の形式で標準入力から与えられる。
M D
出力
M 月 D 日が五節句に含まれるならば Yes を、含まれないならば No を出力せよ。
入力例 1
3 3
出力例 1
Yes
3 月 3 日は五節句に含まれるので、Yes を出力します。
入力例 2
1 1
出力例 2
No
1 月 1 日は五節句に含まれないので、No を出力します。
入力例 3
4 4
出力例 3
No
入力例 4
11 7
出力例 4
No
Score : 100 points
Problem Statement
The following five days are called the five seasonal festivals (gosekku).
- January \color{red}{\mathbf{7}}
- March 3
- May 5
- July 7
- September 9
If month M, day D is one of the five seasonal festivals, output Yes; otherwise, output No.
Constraints
- M and D are integers.
- Month M, day D is a valid date in a leap year in the Gregorian calendar.
Input
The input is given from Standard Input in the following format:
M D
Output
If month M, day D is one of the five seasonal festivals, output Yes; otherwise, output No.
Sample Input 1
3 3
Sample Output 1
Yes
March 3 is one of the five seasonal festivals, so output Yes.
Sample Input 2
1 1
Sample Output 2
No
January 1 is not one of the five seasonal festivals, so output No.
Sample Input 3
4 4
Sample Output 3
No
Sample Input 4
11 7
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 H 行、横 W 列 のマス目があります。 高橋くんは、このマス目のそれぞれのマスを白か黒で塗ろうとしています。
高橋くんは、マス目に含まれるマスのうち端にあるマスをすべて黒く塗り、それ以外のマスを白く塗ります。 高橋くんが色を塗ったあとのマス目を出力してください。
より厳密には
H\times W のマス目があります。
上から i 行目 (1\le i\le H) 、左から j 列目 (1\le j\le W) のマスをマス (i,j) と呼ぶことにします。
マス (i,j)\ (1\le i\le H,1\le j\le W) とマス (k,l)\ (1\le k\le H,1\le l\le W) が |i-k|+|j-l|=1 を満たすとき、かつそのときに限りこれらのマスは辺で隣接していると言います。
マス (i,j) と辺で隣接しているマスが 3 個以下のとき、かつそのときに限りマス (i,j) は端にあると言います。
次の条件を満たす H 個の文字列 S _ 1,S _ 2,\ldots,S _ H を求めてください。
- S _ i は長さ W の文字列であり、S _ i の j 文字目は、マス (i,j) が端にあるとき
#、そうでないとき.である。
制約
- 3\le H\le10
- 3\le W\le10
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
出力
H 行にわたって、長さ W の文字列を出力せよ。
i 行目 (1\le i\le H) の j 文字目 (1\le j\le W) には、マス (i,j) が黒く塗られているなら # 、白く塗られているなら . を出力せよ。
入力例 1
4 5
出力例 1
##### #...# #...# #####
例えば、マス (1,1) は端にあります。
マス (1,1) と辺で隣接しているマスはマス (1,2) とマス (2,1) の 2 個のみだからです。
よって、マス (1,1) は黒く塗られるので、1 行目の 1 文字目には # を出力してください。
逆に、例えばマス (3,4) は端にありません。
マス (3,4) と辺で隣接しているマスはマス (2,4), マス (3,3), マス (3,5), マス (4,4) の 4 個だからです。
よって、マス (3,4) は白く塗られるので、3 行目の 4 文字目には . を出力してください。
入力例 2
5 6
出力例 2
###### #....# #....# #....# ######
Score : 200 points
Problem Statement
There is a grid with H rows and W columns. Takahashi is going to paint each cell of this grid black or white.
He paints all cells on the border of the grid black, and all other cells white. Output the grid after he has painted it.
More formally
There is an H\times W grid.
We call the cell at the i-th row from the top (1\le i\le H) and the j-th column from the left (1\le j\le W) cell (i,j).
Cell (i,j)\ (1\le i\le H,1\le j\le W) and cell (k,l)\ (1\le k\le H,1\le l\le W) are said to be edge-adjacent if and only if |i-k|+|j-l|=1.
Cell (i,j) is said to be on the border if and only if the number of cells edge-adjacent to cell (i,j) is at most 3.
Find H strings S _ 1,S _ 2,\ldots,S _ H satisfying the following condition.
- S _ i is a string of length W, and the j-th character of S _ i is
#if cell (i,j) is on the border, and.otherwise.
Constraints
- 3\le H\le10
- 3\le W\le10
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W
Output
Output H lines, each a string of length W.
The j-th character (1\le j\le W) of the i-th line (1\le i\le H) should be # if cell (i,j) is painted black, and . if it is painted white.
Sample Input 1
4 5
Sample Output 1
##### #...# #...# #####
For example, cell (1,1) is on the border.
This is because there are only two cells edge-adjacent to cell (1,1): cells (1,2) and (2,1).
Thus, cell (1,1) is painted black, so the first character of the first line should be #.
Conversely, for example, cell (3,4) is not on the border.
This is because there are four cells edge-adjacent to cell (3,4): cells (2,4), (3,3), (3,5), and (4,4).
Thus, cell (3,4) is painted white, so the fourth character of the third line should be ..
Sample Input 2
5 6
Sample Output 2
###### #....# #....# #....# ######
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
アーティストの高砂君は、魚の骨をかたどったオブジェを作りました。
オブジェは N 本の肋骨と 1 本の脊椎からなります。 肋骨には 1 から N までの番号が付けられています。
高砂君は、以下の条件をすべて満たすように N+1 本の骨に 1 つずつ文字列を書こうと考えています。
- 脊椎に書く文字列の長さは N である。
- 肋骨 i = 1, \dots, N に対して、以下が成り立つ。
- 肋骨 i に書く文字列の長さは A_i である。
- 肋骨 i に書く文字列の B_i 文字目は、脊椎に書く文字列の i 文字目に一致する。
- N+1 本の骨に書く文字列はいずれも、S_1, \cdots, S_M のいずれかである(重複してもよい)。
S_1, \cdots, S_M は英小文字からなる文字列であり、互いに異なります。
j = 1, \cdots, M に対して、以下の質問に答えてください。
- 条件を満たす書き方のうち、脊椎に書く文字列が S_j であるものは存在しますか?
制約
- N は整数
- 1 \leq N \leq 10
- A_i, B_i は整数 (1 \leq i \leq N)
- 1 \leq B_i \leq A_i \leq 10 (1 \leq i \leq N)
- M は整数
- 1 \leq M \leq 200\,000
- S_j は英小文字からなる文字列 (1 \leq j \leq M)
- 1 \leq |S_j| \leq 10 (1 \leq j \leq M)
- S_1, \cdots, S_M は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 \vdots A_N B_N M S_1 \vdots S_M
出力
M 行出力せよ。
j 行目 (1 \leq j \leq M) には、条件を満たす書き方のうち脊椎に書く文字列が S_j のものが存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
5 5 3 5 2 4 1 5 1 3 2 8 retro chris itchy tuna crab rock cod ash
出力例 1
Yes Yes No No No No No No
肋骨 1,2,3,4,5 にそれぞれ chris, retro, tuna, retro, cod と書くことで、脊椎に retro を書いたときに条件を満たします。

retroの長さは 5 文字。- 各肋骨について、以下が成り立つ。
- 肋骨 1 に書かれる文字列
chrisの長さは 5 文字である。その 3 文字目はrであり、retroの 1 文字目に一致する。 - 肋骨 2 に書かれる文字列
retroの長さは 5 文字である。その 2 文字目はeであり、retroの 2 文字目に一致する。 - 肋骨 3 に書かれる文字列
tunaの長さは 4 文字である。その 1 文字目はtであり、retroの 3 文字目に一致する。 - 肋骨 4 に書かれる文字列
retroの長さは 5 文字である。その 1 文字目はrであり、retroの 4 文字目に一致する。 - 肋骨 5 に書かれる文字列
codの長さは 3 文字である。その 2 文字目はoであり、retroの 5 文字目に一致する。
- 肋骨 1 に書かれる文字列
肋骨 1,2,3,4,5 にそれぞれ itchy, chris, rock, itchy, ash と書くことで、脊椎に chris を書いたときに条件を満たします。

入力例 2
5 5 1 5 2 5 3 5 4 5 5 8 retro chris itchy tuna crab rock cod ash
出力例 2
Yes Yes Yes No No No No No
Score : 300 points
Problem Statement
Artist Takasago has created an object in the shape of a fish skeleton.
The object consists of N ribs and one spine. The ribs are numbered 1 through N.
He wants to write one string on each of the N+1 bones, satisfying all of the following conditions.
- The length of the string written on the spine is N.
- For each rib i = 1, \dots, N, the following hold.
- The length of the string written on rib i is A_i.
- The B_i-th character of the string written on rib i equals the i-th character of the string written on the spine.
- Each of the strings written on the N+1 bones is one of S_1, \cdots, S_M (duplicates allowed).
S_1, \cdots, S_M are strings consisting of lowercase English letters, and they are all distinct.
For each j = 1, \cdots, M, answer the following question.
- Among the ways to write strings satisfying the conditions, is there one where the string written on the spine is S_j?
Constraints
- N is an integer.
- 1 \leq N \leq 10
- A_i and B_i are integers. (1 \leq i \leq N)
- 1 \leq B_i \leq A_i \leq 10 (1 \leq i \leq N)
- M is an integer.
- 1 \leq M \leq 200\,000
- S_j is a string consisting of lowercase English letters. (1 \leq j \leq M)
- 1 \leq |S_j| \leq 10 (1 \leq j \leq M)
- S_1, \cdots, S_M are pairwise distinct.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 \vdots A_N B_N M S_1 \vdots S_M
Output
Output M lines.
The j-th line (1 \leq j \leq M) should contain Yes if there exists a way to write strings satisfying the conditions with S_j written on the spine, and No otherwise.
Sample Input 1
5 5 3 5 2 4 1 5 1 3 2 8 retro chris itchy tuna crab rock cod ash
Sample Output 1
Yes Yes No No No No No No
By writing chris, retro, tuna, retro, cod on ribs 1,2,3,4,5 respectively, the conditions are satisfied with retro written on the spine.

- The length of
retrois 5. - For each rib, the following hold.
- The string written on rib 1 is
chris, which has length 5. Its third character isr, which equals the first character ofretro. - The string written on rib 2 is
retro, which has length 5. Its second character ise, which equals the second character ofretro. - The string written on rib 3 is
tuna, which has length 4. Its first character ist, which equals the third character ofretro. - The string written on rib 4 is
retro, which has length 5. Its first character isr, which equals the fourth character ofretro. - The string written on rib 5 is
cod, which has length 3. Its second character iso, which equals the fifth character ofretro.
- The string written on rib 1 is
By writing itchy, chris, rock, itchy, ash on ribs 1,2,3,4,5 respectively, the conditions are satisfied with chris written on the spine.

Sample Input 2
5 5 1 5 2 5 3 5 4 5 5 8 retro chris itchy tuna crab rock cod ash
Sample Output 2
Yes Yes Yes No No No No No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
英小文字からなる文字列 S,T が与えられます。
S の空でない部分文字列 s のうち、T を(連続するとは限らない)部分列として含まないものの個数を求めてください。
ここで、S の 2 つの部分文字列は、取り出した箇所が異なれば文字列として等しくても区別するものとします。
部分文字列とは
文字列 X の部分文字列とは、X の先頭から 0 文字以上、末尾から 0 文字以上を削除して得られる文字列のことを指します。部分列とは
文字列 X の部分列とは,X の要素を 0 個以上選んで削除し,残った要素を元の順序を保って並べた文字列のことを指します.制約
- S は英小文字からなる文字列
- |S| を S の長さとして、1\le |S|\le2\times10 ^ 5
- T は英小文字からなる文字列
- |T| を T の長さとして、1\le |T|\le50
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
答えを出力せよ。
入力例 1
abrakadabra aba
出力例 1
51
例えば、S の 1 文字目から 3 文字目までからなる部分文字列 abr は T を部分列として含みません。
他にも、k(S の 5 文字目のみ)や akada(S の 4 文字目から 8 文字目)などの 51 個の部分文字列が条件を満たします。
文字列 abr は S の 1 文字目から 3 文字目までからなる部分文字列としても S の 8 文字目から 10 文字目までからなる部分文字列としても得ることができますが、文字列から取り出す箇所が異なるため区別して数えることに注意してください。
入力例 2
aaaaa a
出力例 2
0
S の空でない部分文字列は、すべて T を部分列として含みます。
よって、条件を満たす部分文字列は存在しないため、0 を出力してください。
入力例 3
rdddrdtdcdrrdcredctdordoeecrotet dcre
出力例 3
263
Score : 400 points
Problem Statement
You are given strings S and T consisting of lowercase English letters.
Among the non-empty substrings s of S, count those that do not contain T as a (not necessarily contiguous) subsequence.
Here, two substrings of S are distinguished if they are taken from different positions, even if they are equal as strings.
What is a substring?
A substring of a string X is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of X.What is a subsequence?
A subsequence of a string X is a string obtained by deleting zero or more elements from X and arranging the remaining elements in their original order.Constraints
- S is a string consisting of lowercase English letters.
- 1\le |S|\le2\times10 ^ 5, where |S| is the length of S.
- T is a string consisting of lowercase English letters.
- 1\le |T|\le50, where |T| is the length of T.
Input
The input is given from Standard Input in the following format:
S T
Output
Output the answer.
Sample Input 1
abrakadabra aba
Sample Output 1
51
For example, the substring abr consisting of the first through third characters of S does not contain T as a subsequence.
Including this, there are 51 substrings satisfying the condition, such as k (only the fifth character of S) and akada (the fourth through eighth characters of S).
Note that the string abr can be obtained both as the substring from the first to third characters of S and as the substring from the eighth to tenth characters of S, but they are taken from different positions, so they are counted separately.
Sample Input 2
aaaaa a
Sample Output 2
0
All non-empty substrings of S contain T as a subsequence.
Thus, there are no substrings satisfying the condition, so output 0.
Sample Input 3
rdddrdtdcdrrdcredctdordoeecrotet dcre
Sample Output 3
263
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
あなたの今日のラッキーギリシャ文字はシグマです。シグマを 2 つも使ったこの問題を解けば、きっと幸運が舞い込むことでしょう。
長さ N の正整数列 A = (A_1, \cdots, A_N) および長さ M の正整数列 B = (B_1, \cdots, B_M) が与えられます。
\displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} A_i \cdot B_j \cdot (i \bmod j) の値を 998244353 で割ったあまりを求めてください。
制約
- 1 \leq N,M \leq 5 \times 10^5
- 1 \leq A_i, B_j \leq 5 \times 10^5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \cdots A_N B_1 B_2 \cdots B_M
出力
答えを 1 行に出力せよ。
入力例 1
6 4 1 6 9 2 3 1 1 10 3 7
出力例 1
508
以下の 24 個の値の合計は 508 です。
- A_1 \cdot B_1 \cdot (1 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_1 \cdot B_2 \cdot (1 \bmod 2) = 1 \cdot 10 \cdot 1 = 10
- A_1 \cdot B_3 \cdot (1 \bmod 3) = 1 \cdot 3 \cdot 1 = 3
- A_1 \cdot B_4 \cdot (1 \bmod 4) = 1 \cdot 7 \cdot 1 = 7
- A_2 \cdot B_1 \cdot (2 \bmod 1) = 6 \cdot 1 \cdot 0 = 0
- A_2 \cdot B_2 \cdot (2 \bmod 2) = 6 \cdot 10 \cdot 0 = 0
- A_2 \cdot B_3 \cdot (2 \bmod 3) = 6 \cdot 3 \cdot 2 = 36
- A_2 \cdot B_4 \cdot (2 \bmod 4) = 6 \cdot 7 \cdot 2 = 84
- A_3 \cdot B_1 \cdot (3 \bmod 1) = 9 \cdot 1 \cdot 0 = 0
- A_3 \cdot B_2 \cdot (3 \bmod 2) = 9 \cdot 10 \cdot 1 = 90
- A_3 \cdot B_3 \cdot (3 \bmod 3) = 9 \cdot 3 \cdot 0 = 0
- A_3 \cdot B_4 \cdot (3 \bmod 4) = 9 \cdot 7 \cdot 3 = 189
- A_4 \cdot B_1 \cdot (4 \bmod 1) = 2 \cdot 1 \cdot 0 = 0
- A_4 \cdot B_2 \cdot (4 \bmod 2) = 2 \cdot 10 \cdot 0 = 0
- A_4 \cdot B_3 \cdot (4 \bmod 3) = 2 \cdot 3 \cdot 1 = 6
- A_4 \cdot B_4 \cdot (4 \bmod 4) = 2 \cdot 7 \cdot 0 = 0
- A_5 \cdot B_1 \cdot (5 \bmod 1) = 3 \cdot 1 \cdot 0 = 0
- A_5 \cdot B_2 \cdot (5 \bmod 2) = 3 \cdot 10 \cdot 1 = 30
- A_5 \cdot B_3 \cdot (5 \bmod 3) = 3 \cdot 3 \cdot 2 = 18
- A_5 \cdot B_4 \cdot (5 \bmod 4) = 3 \cdot 7 \cdot 1 = 21
- A_6 \cdot B_1 \cdot (6 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_6 \cdot B_2 \cdot (6 \bmod 2) = 1 \cdot 10 \cdot 0 = 0
- A_6 \cdot B_3 \cdot (6 \bmod 3) = 1 \cdot 3 \cdot 0 = 0
- A_6 \cdot B_4 \cdot (6 \bmod 4) = 1 \cdot 7 \cdot 2 = 14
入力例 2
20 20 36625 195265 98908 111868 111868 47382 147644 472464 472464 416653 111868 195265 327972 327972 262769 75439 381156 451275 36625 195265 327972 111868 416653 177330 340019 262769 47382 262769 47382 340019 47382 262769 327972 327972 359676 381156 327972 36625 451275 381156
出力例 2
58141644
Score : 450 points
Problem Statement
Your lucky Greek letter for today is sigma. Solve this problem that uses sigma twice, and good fortune will surely come your way.
You are given a sequence of positive integers A = (A_1, \cdots, A_N) of length N and a sequence of positive integers B = (B_1, \cdots, B_M) of length M.
Find the value, modulo 998244353, of \displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} A_i \cdot B_j \cdot (i \bmod j).
Constraints
- 1 \leq N,M \leq 5 \times 10^5
- 1 \leq A_i, B_j \leq 5 \times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \cdots A_N B_1 B_2 \cdots B_M
Output
Output the answer on a single line.
Sample Input 1
6 4 1 6 9 2 3 1 1 10 3 7
Sample Output 1
508
The sum of the following 24 values is 508.
- A_1 \cdot B_1 \cdot (1 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_1 \cdot B_2 \cdot (1 \bmod 2) = 1 \cdot 10 \cdot 1 = 10
- A_1 \cdot B_3 \cdot (1 \bmod 3) = 1 \cdot 3 \cdot 1 = 3
- A_1 \cdot B_4 \cdot (1 \bmod 4) = 1 \cdot 7 \cdot 1 = 7
- A_2 \cdot B_1 \cdot (2 \bmod 1) = 6 \cdot 1 \cdot 0 = 0
- A_2 \cdot B_2 \cdot (2 \bmod 2) = 6 \cdot 10 \cdot 0 = 0
- A_2 \cdot B_3 \cdot (2 \bmod 3) = 6 \cdot 3 \cdot 2 = 36
- A_2 \cdot B_4 \cdot (2 \bmod 4) = 6 \cdot 7 \cdot 2 = 84
- A_3 \cdot B_1 \cdot (3 \bmod 1) = 9 \cdot 1 \cdot 0 = 0
- A_3 \cdot B_2 \cdot (3 \bmod 2) = 9 \cdot 10 \cdot 1 = 90
- A_3 \cdot B_3 \cdot (3 \bmod 3) = 9 \cdot 3 \cdot 0 = 0
- A_3 \cdot B_4 \cdot (3 \bmod 4) = 9 \cdot 7 \cdot 3 = 189
- A_4 \cdot B_1 \cdot (4 \bmod 1) = 2 \cdot 1 \cdot 0 = 0
- A_4 \cdot B_2 \cdot (4 \bmod 2) = 2 \cdot 10 \cdot 0 = 0
- A_4 \cdot B_3 \cdot (4 \bmod 3) = 2 \cdot 3 \cdot 1 = 6
- A_4 \cdot B_4 \cdot (4 \bmod 4) = 2 \cdot 7 \cdot 0 = 0
- A_5 \cdot B_1 \cdot (5 \bmod 1) = 3 \cdot 1 \cdot 0 = 0
- A_5 \cdot B_2 \cdot (5 \bmod 2) = 3 \cdot 10 \cdot 1 = 30
- A_5 \cdot B_3 \cdot (5 \bmod 3) = 3 \cdot 3 \cdot 2 = 18
- A_5 \cdot B_4 \cdot (5 \bmod 4) = 3 \cdot 7 \cdot 1 = 21
- A_6 \cdot B_1 \cdot (6 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_6 \cdot B_2 \cdot (6 \bmod 2) = 1 \cdot 10 \cdot 0 = 0
- A_6 \cdot B_3 \cdot (6 \bmod 3) = 1 \cdot 3 \cdot 0 = 0
- A_6 \cdot B_4 \cdot (6 \bmod 4) = 1 \cdot 7 \cdot 2 = 14
Sample Input 2
20 20 36625 195265 98908 111868 111868 47382 147644 472464 472464 416653 111868 195265 327972 327972 262769 75439 381156 451275 36625 195265 327972 111868 416653 177330 340019 262769 47382 262769 47382 340019 47382 262769 327972 327972 359676 381156 327972 36625 451275 381156
Sample Output 2
58141644
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
正整数 N と、(1,2,\ldots,N) の順列 P=(P _ 1,P _ 2,\ldots,P _ N) が与えられます。
整数 K が与えられます。 次の 2 つの条件を満たす整数の組 (l,r) がいくつあるか求めてください。
- 1\le l\le r\le N
- 列 (P _ l,P _ {l+1},\ldots,P _ r) の転倒数が K と等しい。
制約
- 1\le N\le5\times10 ^ 5
- 0\le K\le\dfrac{N(N-1)}2
- 1\le P _ i\le N\ (1\le i\le N)
- P _ i\ne P _ j\ (1\le i\lt j\le N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K P _ 1 P _ 2 \ldots P _ N
出力
条件を満たす整数の組 (l,r) の個数を出力せよ。
入力例 1
7 3 6 3 2 1 7 5 4
出力例 1
5
例えば、列 (P _ 1,P _ 2,P _ 3)=(6,3,2) の転倒数が 3 なので、(l,r)=(1,3) は条件を満たします。
他には (l,r)=(2,4),(2,5),(4,7),(5,7) が条件を満たすので、5 を出力してください。
入力例 2
4 1 1 2 3 4
出力例 2
0
P の連続する部分列の転倒数はすべて 0 になるので、条件を満たす (l,r) は存在しません。
よって、0 を出力してください。
入力例 3
25 18 14 19 24 8 12 11 6 5 3 13 22 15 17 2 9 4 7 18 10 25 23 16 1 20 21
出力例 3
3
Score : 500 points
Problem Statement
You are given a positive integer N and a permutation P=(P _ 1,P _ 2,\ldots,P _ N) of (1,2,\ldots,N).
You are given an integer K. Find the number of pairs of integers (l,r) satisfying the following two conditions.
- 1\le l\le r\le N
- The inversion number of the sequence (P _ l,P _ {l+1},\ldots,P _ r) equals K.
Constraints
- 1\le N\le5\times10 ^ 5
- 0\le K\le\dfrac{N(N-1)}2
- 1\le P _ i\le N\ (1\le i\le N)
- P _ i\ne P _ j\ (1\le i\lt j\le N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K P _ 1 P _ 2 \ldots P _ N
Output
Output the number of pairs of integers (l,r) satisfying the conditions.
Sample Input 1
7 3 6 3 2 1 7 5 4
Sample Output 1
5
For example, the inversion number of the sequence (P _ 1,P _ 2,P _ 3)=(6,3,2) is 3, so (l,r)=(1,3) satisfies the conditions.
The other pairs satisfying the conditions are (l,r)=(2,4),(2,5),(4,7),(5,7), so output 5.
Sample Input 2
4 1 1 2 3 4
Sample Output 2
0
The inversion number of every contiguous subsequence of P is 0, so there are no pairs (l,r) satisfying the conditions.
Thus, output 0.
Sample Input 3
25 18 14 19 24 8 12 11 6 5 3 13 22 15 17 2 9 4 7 18 10 25 23 16 1 20 21
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 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 数列としてあり得るものの個数を求めてください。
ただし、異なる位置から取り出した連続部分列であっても、数列として一致するものは区別せずまとめて 1 通りとして数えます。
制約
- 1 \leq N \leq 500\,000
- 1 \leq A_i \leq 9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \cdots A_N
出力
答えを 1 行に出力せよ。
入力例 1
23 2 2 3 3 3 1 1 1 3 3 3 1 2 2 2 1 9 1 4 4 4 4 4
出力例 1
14
A の空でない連続部分列として現れる 221 数列は、以下の 14 通りです。
- (1)
- (1,2,2)
- (1,3,3,3)
- (1,3,3,3,1)
- (1,3,3,3,1,2,2)
- (1,4,4,4,4)
- (2,2)
- (2,2,1)
- (2,2,3,3,3)
- (2,2,3,3,3,1)
- (3,3,3)
- (3,3,3,1)
- (3,3,3,1,2,2)
- (4,4,4,4)
入力例 2
2 6 7
出力例 2
0
A の空でない連続部分列として現れる 221 数列は存在しません。
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 of distinct 221 sequences that appear as a non-empty contiguous subsequence of A.
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 9
- 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 a single line.
Sample Input 1
23 2 2 3 3 3 1 1 1 3 3 3 1 2 2 2 1 9 1 4 4 4 4 4
Sample Output 1
14
The 221 sequences that appear as a non-empty contiguous subsequence of A are the following 14:
- (1)
- (1,2,2)
- (1,3,3,3)
- (1,3,3,3,1)
- (1,3,3,3,1,2,2)
- (1,4,4,4,4)
- (2,2)
- (2,2,1)
- (2,2,3,3,3)
- (2,2,3,3,3,1)
- (3,3,3)
- (3,3,3,1)
- (3,3,3,1,2,2)
- (4,4,4,4)
Sample Input 2
2 6 7
Sample Output 2
0
No 221 sequences appear as a non-empty contiguous subsequence of A.