Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
文字列が N 個与えられます。
i 番目 (1\leq i\leq N) に与えられる文字列 S _ i は Takahashi
か Aoki
のどちらかと等しいです。
S _ i が Takahashi
と等しい i がいくつあるか求めてください。
制約
- 1\leq N\leq 100
- N は整数
- S _ i は
Takahashi
かAoki
のいずれか (1\leq i\leq N)
入力
入力は以下の形式で標準入力から与えられる。
N S _ 1 S _ 2 \vdots S _ N
出力
S _ i が Takahashi
と等しい i の個数を整数として一行に出力せよ。
入力例 1
3 Aoki Takahashi Takahashi
出力例 1
2
S _ 2,S _ 3 の 2 つが Takahashi
と等しく、S _ 1 はそうではありません。
よって、2
を出力してください。
入力例 2
2 Aoki Aoki
出力例 2
0
Takahashi
と等しい S _ i が存在しないこともあります。
入力例 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
出力例 3
7
Score : 100 points
Problem Statement
You are given N strings.
The i-th string S_i (1 \leq i \leq N) is either Takahashi
or Aoki
.
How many i are there such that S_i is equal to Takahashi
?
Constraints
- 1 \leq N \leq 100
- N is an integer.
- Each S_i is
Takahashi
orAoki
. (1 \leq i \leq N)
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the count of i such that S_i is equal to Takahashi
as an integer in a single line.
Sample Input 1
3 Aoki Takahashi Takahashi
Sample Output 1
2
S_2 and S_3 are equal to Takahashi
, while S_1 is not.
Therefore, print 2
.
Sample Input 2
2 Aoki Aoki
Sample Output 2
0
It is possible that no S_i is equal to Takahashi
.
Sample Input 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
2N 人の人が横一列に並んでおり、左から i 番目の人は色 A_i の服を着ています。ここで、服の色は 1 から N の N 色であり、それぞれの色についてちょうど 2 人の人がその色の服を着ています。
i=1,2,\ldots,N のうち、以下の条件を満たすものは何通りあるか求めてください。
- 色 i の服を着た二人の人の間にはちょうど一人いる。
制約
- 2\leq N\leq 100
- 1\leq A_i \leq N
- A には 1 以上 N 以下の整数全てがそれぞれ 2 個ずつ含まれる
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_{2N}
出力
答えを出力せよ。
入力例 1
3 1 2 1 3 2 3
出力例 1
2
条件を満たす i は 1 と 3 の 2 個です。
実際、色 1 の服を着ているのは左から 1 番目の人と左から 3 番目の人で、間にちょうど一人います。
入力例 2
2 1 1 2 2
出力例 2
0
条件を満たす i が存在しない場合もあります。
入力例 3
4 4 3 2 3 2 1 4 1
出力例 3
3
Score : 150 points
Problem Statement
There are 2N people standing in a row, and the person at the i-th position from the left is wearing clothes of color A_i. Here, the clothes have N colors from 1 to N, and exactly two people are wearing clothes of each color.
Find how many of the integers i=1,2,\ldots,N satisfy the following condition:
- There is exactly one person between the two people wearing clothes of color i.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq N
- Each integer from 1 through N appears exactly twice in A.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_{2N}
Output
Print the answer.
Sample Input 1
3 1 2 1 3 2 3
Sample Output 1
2
There are two values of i that satisfy the condition: 1 and 3.
In fact, the people wearing clothes of color 1 are at the 1st and 3rd positions from the left, with exactly one person in between.
Sample Input 2
2 1 1 2 2
Sample Output 2
0
There may be no i that satisfies the condition.
Sample Input 3
4 4 3 2 3 2 1 4 1
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
座標平面上に 2\times1 の大きさのタイルが敷き詰められています。 タイルは、次の規則に従って敷き詰められています。
- 整数の組 (i,j) に対し、正方形 A _ {i,j}=\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace は 1 つのタイルに含まれる。
- i+j が偶数のとき、A _ {i,j} と A _ {i + 1,j} は同じタイルに含まれる。
ただし、タイルは境界を含むものとし、共通部分が正の面積をもつような 2 つの異なるタイルは存在しないとします。
原点の近くでは、タイルは以下のように敷き詰められています。
高橋君は、はじめ座標平面上の点 (S _ x+0.5,S _ y+0.5) にいます。
高橋君は、次の移動を好きなだけ繰り返します。
- 上下左右の方向と正の整数 n を選ぶ。その方向に n だけ進む。
高橋君が異なるタイルを通るたび、高橋君は通行料を 1 だけ支払います。
高橋君が点 (T _ x+0.5,T _ y+0.5) にたどり着くために支払わなければならない通行料の最小値を求めてください。
制約
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
S _ x S _ y T _ x T _ y
出力
高橋君が支払わなければならない通行料の最小値を出力せよ。
入力例 1
5 0 2 5
出力例 1
5
例えば、以下のように移動することで支払う通行料を 5 にすることができます。
- 左に 1 進む。通行料を 0 支払う。
- 上に 1 進む。通行料を 1 支払う。
- 左に 1 進む。通行料を 0 支払う。
- 上に 3 進む。通行料を 3 支払う。
- 左に 1 進む。通行料を 0 支払う。
- 上に 1 進む。通行料を 1 支払う。
支払う通行料を 4 以下にすることはできないので、5
を出力してください。
入力例 2
3 1 4 1
出力例 2
0
通行料を支払わなくてよい場合もあります。
入力例 3
2552608206527595 5411232866732612 771856005518028 7206210729152763
出力例 3
1794977862420151
出力すべき値が 32\operatorname{bit} 整数の範囲に収まらない場合があることに注意してください。
Score : 350 points
Problem Statement
The coordinate plane is covered with 2\times1 tiles. The tiles are laid out according to the following rules:
- For an integer pair (i,j), the square A _ {i,j}=\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained in one tile.
- When i+j is even, A _ {i,j} and A _ {i + 1,j} are contained in the same tile.
Tiles include their boundaries, and no two different tiles share a positive area.
Near the origin, the tiles are laid out as follows:
Takahashi starts at the point (S _ x+0.5,S _ y+0.5) on the coordinate plane.
He can repeat the following move as many times as he likes:
- Choose a direction (up, down, left, or right) and a positive integer n. Move n units in that direction.
Each time he enters a tile, he pays a toll of 1.
Find the minimum toll he must pay to reach the point (T _ x+0.5,T _ y+0.5).
Constraints
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
S _ x S _ y T _ x T _ y
Output
Print the minimum toll Takahashi must pay.
Sample Input 1
5 0 2 5
Sample Output 1
5
For example, Takahashi can pay a toll of 5 by moving as follows:
- Move left by 1. Pay a toll of 0.
- Move up by 1. Pay a toll of 1.
- Move left by 1. Pay a toll of 0.
- Move up by 3. Pay a toll of 3.
- Move left by 1. Pay a toll of 0.
- Move up by 1. Pay a toll of 1.
It is impossible to reduce the toll to 4 or less, so print 5
.
Sample Input 2
3 1 4 1
Sample Output 2
0
There are cases where no toll needs to be paid.
Sample Input 3
2552608206527595 5411232866732612 771856005518028 7206210729152763
Sample Output 3
1794977862420151
Note that the value to be output may exceed the range of a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
A
, B
, ?
からなる N 文字の文字列 S が与えられます。
正整数 K が与えられます。
A
, B
からなる文字列 T が次の条件を満たすとき、T は良い文字列であるということにします。
- T の長さ K の連続する部分文字列で、回文であるものが存在しない。
S に含まれる ?
の個数を q 個とします。
q 個の ?
をそれぞれ A
, B
のどちらかに置き換えて得られる文字列は 2 ^ q 通りありますが、その中に良い文字列がいくつあるか求めてください。
ただし、答えは非常に大きくなる場合があるので、998244353 で割った余りを求めてください。
制約
- 2\leq K\leq N\leq 1000
- K\leq 10
- S は
A
,B
,?
からなる文字列 - S の長さは N
- N,K は整数
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを出力せよ。
入力例 1
7 4 AB?A?BA
出力例 1
1
与えられた文字列の中に ?
は 2 個あります。
2 個の ?
をそれぞれ A
, B
のどちらかに置き換えて得られる文字列は次の 4 通りあります。
ABAAABA
ABAABBA
ABBAABA
ABBABBA
このうち、最初の ABAAABA
以外の 3 つの文字列は、長さ 4 の回文 ABBA
を連続する部分文字列として含むため、良い文字列ではありません。
よって、1
を出力してください。
入力例 2
40 7 ????????????????????????????????????????
出力例 2
116295436
良い文字列の個数を 998244353 で割った余りを求めることに注意してください。
入力例 3
15 5 ABABA??????????
出力例 3
0
?
をどのように置き換えても良い文字列にならないこともあります。
入力例 4
40 8 ?A?B??B?B?AA?A?B??B?A???B?BB?B???BA??BAA
出力例 4
259240
Score : 450 points
Problem Statement
You are given a string S of length N consisting of characters A
, B
, and ?
.
You are also given a positive integer K.
A string T consisting of A
and B
is considered a good string if it satisfies the following condition:
- No contiguous substring of length K in T is a palindrome.
Let q be the number of ?
characters in S.
There are 2^q strings that can be obtained by replacing each ?
in S with either A
or B
. Find how many of these strings are good strings.
The count can be very large, so find it modulo 998244353.
Constraints
- 2 \leq K \leq N \leq 1000
- K \leq 10
- S is a string consisting of
A
,B
, and?
. - The length of S is N.
- N and K are integers.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer.
Sample Input 1
7 4 AB?A?BA
Sample Output 1
1
The given string has two ?
s.
There are four strings obtained by replacing each ?
with A
or B
:
ABAAABA
ABAABBA
ABBAABA
ABBABBA
Among these, the last three contain the contiguous substring ABBA
of length 4, which is a palindrome, and thus are not good strings.
Therefore, you should print 1
.
Sample Input 2
40 7 ????????????????????????????????????????
Sample Output 2
116295436
Ensure to find the number of good strings modulo 998244353.
Sample Input 3
15 5 ABABA??????????
Sample Output 3
0
It is possible that there is no way to replace the ?
s to obtain a good string.
Sample Input 4
40 8 ?A?B??B?B?AA?A?B??B?A???B?BB?B???BA??BAA
Sample Output 4
259240
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
ストーリー
長い水槽があり、高さの異なる板が等間隔に配置されています。 高橋くんは、この水槽の端へ水を注いでいったとき、板で区切られたそれぞれの領域に水が到達する時刻が知りたいです。
問題文
長さ N の正整数列 H=(H _ 1,H _ 2,\dotsc,H _ N) が与えられます。
長さ N+1 の非負整数列 A=(A _ 0,A _ 1,\dotsc,A _ N) があります。 はじめ、A _ 0=A _ 1=\dotsb=A _ N=0 です。
A に対して、次の操作を繰り返します。
- A _ 0 の値を 1 増やす。
- i=1,2,\ldots,N に対して、この順に次の操作を行う。
- A _ {i-1}\gt A _ i かつ A _ {i-1}\gt H _ i のとき、A _ {i-1} の値を 1 減らし、A _ i の値を 1 増やす。
i=1,2,\ldots,N のそれぞれに対して、初めて A _ i>0 が成り立つのは何回目の操作の後か求めてください。
制約
- 1\leq N\leq2\times10 ^ 5
- 1\leq H _ i\leq10 ^ 9\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H _ 1 H _ 2 \dotsc H _ N
出力
i=1,2,\ldots,N に対する答えを、空白を区切りとして 1 行に出力せよ。
入力例 1
5 3 1 4 1 5
出力例 1
4 5 13 14 26
はじめ 5 回の操作では以下のようになります。
それぞれの行が一回の操作に対応し、左端が 1 番の操作を、それ以外が 2 番の操作に対応します。
この図から、A _ 1\gt0 が初めて成り立つのは 4 回目の操作の後、A _ 2\gt0 が初めて成り立つのは 5 回目の操作の後です。
同様にして、A _ 3,A _ 4,A _ 5 に対する答えは 13,14,26 です。
よって、4 5 13 14 26
を出力してください。
入力例 2
6 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 2
1000000001 2000000001 3000000001 4000000001 5000000001 6000000001
出力すべき値が 32\operatorname{bit} 整数に収まらない場合があることに注意してください。
入力例 3
15 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632
出力例 3
749 918 1921 2250 4861 5390 5822 6428 6836 7796 7934 8294 10109 10223 11373
Score : 500 points
Story
There is a long water tank with boards of different heights placed at equal intervals. Takahashi wants to know the time at which water reaches each section separated by the boards when water is poured from one end of the tank.
Problem Statement
You are given a sequence of positive integers of length N: H=(H _ 1,H _ 2,\dotsc,H _ N).
There is a sequence of non-negative integers of length N+1: A=(A _ 0,A _ 1,\dotsc,A _ N). Initially, A _ 0=A _ 1=\dotsb=A _ N=0.
Perform the following operations repeatedly on A:
- Increase the value of A _ 0 by 1.
- For i=1,2,\ldots,N in this order, perform the following operation:
- If A _ {i-1}\gt A _ i and A _ {i-1}\gt H _ i, decrease the value of A _ {i-1} by 1 and increase the value of A _ i by 1.
For each i=1,2,\ldots,N, find the number of operations before A _ i>0 holds for the first time.
Constraints
- 1\leq N\leq2\times10 ^ 5
- 1\leq H _ i\leq10 ^ 9\ (1\leq i\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H _ 1 H _ 2 \dotsc H _ N
Output
Print the answers for i=1,2,\ldots,N in a single line, separated by spaces.
Sample Input 1
5 3 1 4 1 5
Sample Output 1
4 5 13 14 26
The first five operations go as follows.
Here, each row corresponds to one operation, with the leftmost column representing step 1 and the others representing step 2.
From this diagram, A _ 1\gt0 holds for the first time after the 4th operation, and A _ 2\gt0 holds for the first time after the 5th operation.
Similarly, the answers for A _ 3, A _ 4, A _ 5 are 13, 14, 26, respectively.
Therefore, you should print 4 5 13 14 26
.
Sample Input 2
6 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 2
1000000001 2000000001 3000000001 4000000001 5000000001 6000000001
Note that the values to be output may not fit within a 32-bit integer.
Sample Input 3
15 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632
Sample Output 3
749 918 1921 2250 4861 5390 5822 6428 6836 7796 7934 8294 10109 10223 11373
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
整数列 A=(A_1,\ldots,A_N) が与えられます。 N 頂点の木 T に対して、 f(T) を以下で定めます。
- T の頂点 i の次数を d_i とする。このとき、f(T)=\sum_{i=1}^N {d_i}^2 A_i とする。
f(T) として考えられる最小値を求めてください。
なお、制約下において答えが 2^{63} 未満となることは保証されています。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 3 2 5 2
出力例 1
24
頂点 1 と頂点 2 を結ぶ辺、頂点 2 と頂点 4 を結ぶ辺、頂点 4 と頂点 3 を結ぶ辺からなるような木 T を考えます。
このとき f(T) = 1^2\times 3 + 2^2\times 2+1^2\times 5 +2^2\times 2 = 24 です。これが f(T) の最小値であることが証明できます。
入力例 2
3 4 3 2
出力例 2
15
入力例 3
7 10 5 10 2 10 13 15
出力例 3
128
Score : 550 points
Problem Statement
You are given a sequence of integers A=(A_1,\ldots,A_N). For a tree T with N vertices, define f(T) as follows:
- Let d_i be the degree of vertex i in T. Then, f(T)=\sum_{i=1}^N {d_i}^2 A_i.
Find the minimum possible value of f(T).
The constraints guarantee the answer to be less than 2^{63}.
Constraints
- 2\leq N\leq 2\times 10^5
- 1\leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 3 2 5 2
Sample Output 1
24
Consider a tree T with an edge connecting vertices 1 and 2, an edge connecting vertices 2 and 4, and an edge connecting vertices 4 and 3.
Then, f(T) = 1^2\times 3 + 2^2\times 2 + 1^2\times 5 + 2^2\times 2 = 24. It can be proven that this is the minimum value of f(T).
Sample Input 2
3 4 3 2
Sample Output 2
15
Sample Input 3
7 10 5 10 2 10 13 15
Sample Output 3
128
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
N 頂点の木が与えられます。 i 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。
また、整数列 A=(A_1,\ldots,A_N) が与えられます。
ここで f(i,j) を以下で定義します。
- A_i=A_j のとき、f(i,j) は頂点 i から頂点 j に移動する場合に通る辺数の最小値とする。A_i\neq A_j のとき f(i,j)=0 とする。
次の式の値を求めてください。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq u_i,v_i \leq N
- 1\leq A_i\leq N
- 入力されるグラフは木
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N u_1 v_1 \vdots u_{N-1} v_{N-1} A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 3 4 4 2 1 2 2 1 1 2
出力例 1
4
f(1,4)=2,f(2,3)=2 となります。また、それ以外の i,j\ (1\leq i < j\leq N) について f(i,j)=0 なので、答えは 2+2=4 です。
入力例 2
8 8 6 3 8 1 4 7 8 4 5 3 4 8 2 1 2 2 2 3 1 1 3
出力例 2
19
Score : 600 points
Problem Statement
You are given a tree with N vertices. The i-th edge connects vertices u_i and v_i bidirectionally.
Additionally, you are given an integer sequence A=(A_1,\ldots,A_N).
Here, define f(i,j) as follows:
- If A_i = A_j, then f(i,j) is the minimum number of edges you need to traverse to move from vertex i to vertex j. If A_i \neq A_j, then f(i,j) = 0.
Calculate the value of the following expression:
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- 1 \leq A_i \leq N
- The input graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N u_1 v_1 \vdots u_{N-1} v_{N-1} A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 3 4 4 2 1 2 2 1 1 2
Sample Output 1
4
f(1,4)=2, f(2,3)=2. For all other i, j\ (1 \leq i < j \leq N), we have f(i,j)=0, so the answer is 2+2=4.
Sample Input 2
8 8 6 3 8 1 4 7 8 4 5 3 4 8 2 1 2 2 2 3 1 1 3
Sample Output 2
19