実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
別世界の AtCoder で開催されている AtCoder Big Contest では、 10^{16} 問の問題が一度に出題されます。
問題の ID は 1 問目から順に A
, B
, ..., Z
, AA
, AB
, ..., ZZ
, AAA
, ... と付けられています。
つまり、 ID は以下の順番で付けられています。
- 長さ 1 の英大文字からなる文字列を辞書順に並べたもの
- 長さ 2 の英大文字からなる文字列を辞書順に並べたもの
- 長さ 3 の英大文字からなる文字列を辞書順に並べたもの
- ...
このコンテストに含まれる問題の ID である文字列 S が与えられるので、それが何問目か答えてください。
制約
- S は AtCoder Big Contest に含まれる問題の ID として正しい
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
AB
出力例 1
28
ID が AB
である問題は、 AtCoder Big Contest の 28 問目です。
入力例 2
C
出力例 2
3
ID が C
である問題は、 AtCoder Big Contest の 3 問目です。
入力例 3
BRUTMHYHIIZP
出力例 3
10000000000000000
ID が BRUTMHYHIIZP
である問題は、 AtCoder Big Contest の 10^{16} 問目、すなわち最終問題です。
Score : 300 points
Problem Statement
In a parallel universe, AtCoder holds AtCoder Big Contest, where 10^{16} problems are given at once.
The IDs of the problems are as follows, from the 1-st problem in order: A
, B
, ..., Z
, AA
, AB
, ..., ZZ
, AAA
, ...
In other words, the IDs are given in the following order:
- the strings of length 1 consisting of uppercase English letters, in lexicographical order;
- the strings of length 2 consisting of uppercase English letters, in lexicographical order;
- the strings of length 3 consisting of uppercase English letters, in lexicographical order;
- ...
Given a string S that is an ID of a problem given in this contest, find the index of the problem. (See also Samples.)
Constraints
- S is a valid ID of a problem given in AtCoder Big Contest.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
AB
Sample Output 1
28
The problem whose ID is AB
is the 28-th problem of AtCoder Big Contest, so 28 should be printed.
Sample Input 2
C
Sample Output 2
3
The problem whose ID is C
is the 3-rd problem of AtCoder Big Contest, so 3 should be printed.
Sample Input 3
BRUTMHYHIIZP
Sample Output 3
10000000000000000
The problem whose ID is BRUTMHYHIIZP
is the
10^{16}-th (last) problem of AtCoder Big Contest, so 10^{16} should be printed as an integer.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。
ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。
単純パスとは?
グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_i と v_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。制約
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- 入力はすべて整数
- 与えられるグラフは木
入力
入力は以下の形式で標準入力から与えられる。
N X Y U_1 V_1 U_2 V_2 \vdots U_{N-1} V_{N-1}
出力
頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。
入力例 1
5 2 5 1 2 1 3 3 4 3 5
出力例 1
2 1 3 5
木 T は以下のような形であり、頂点 2 から頂点 5への単純パスは
頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。
入力例 2
6 1 2 3 1 2 5 1 2 4 1 2 6
出力例 2
1 2
木 T は以下のような形です。
Score : 300 points
Problem Statement
There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.
You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.
It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.
What is a simple path?
For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- All values in the input are integers.
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N X Y U_1 V_1 U_2 V_2 \vdots U_{N-1} V_{N-1}
Output
Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.
Sample Input 1
5 2 5 1 2 1 3 3 4 3 5
Sample Output 1
2 1 3 5
The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.
Sample Input 2
6 1 2 3 1 2 5 1 2 4 1 2 6
Sample Output 2
1 2
The tree T is shown below.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N \times N のマス目があります。上から i 行目、左から j 列目のマスを (i,j) と表します。
始め、(1,1) に駒が 1 個置かれています。あなたは以下の操作を何度でも行うことができます。
- 今駒が置かれているマスと距離がちょうど \sqrt{M} であるマスに駒を移動させる。
ここで、マス (i,j) とマス (k,l) の距離は \sqrt{(i-k)^2+(j-l)^2} とします。
全てのマス (i,j) に対して、以下の問題を解いてください。
- 駒を (1,1) から (i,j) に移動させることができるかを判定し、できる場合は操作回数の最小値を求めてください。
制約
- 1 \le N \le 400
- 1 \le M \le 10^6
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
N 行出力せよ。i 行目には N 個の整数を出力せよ。i 行目の j 個目の出力は、マス (i,j) に駒を移動させることができるのであれば操作回数の最小値を、そうでないのであれば -1 を出力せよ。
入力例 1
3 1
出力例 1
0 1 2 1 2 3 2 3 4
駒は上下左右の 4 個のマスに移動することができます。
例えば (2,2) に移動するには、以下のように 2 回の操作を行えばよいです。
- 今駒は (1,1) に置かれている。(1,1) と (1,2) の距離はちょうど \sqrt{1} であるため、駒を (1,2) に移動させる。
- 今駒は (1,2) に置かれている。(1,2) と (2,2) の距離はちょうど \sqrt{1} であるため、駒を (2,2) に移動させる。
入力例 2
10 5
出力例 2
0 3 2 3 2 3 4 5 4 5 3 4 1 2 3 4 3 4 5 6 2 1 4 3 2 3 4 5 4 5 3 2 3 2 3 4 3 4 5 6 2 3 2 3 4 3 4 5 4 5 3 4 3 4 3 4 5 4 5 6 4 3 4 3 4 5 4 5 6 5 5 4 5 4 5 4 5 6 5 6 4 5 4 5 4 5 6 5 6 7 5 6 5 6 5 6 5 6 7 6
Score : 400 points
Problem Statement
There is a grid with N \times N squares. We denote by (i, j) the square at the i-th row from the top and j-th column from the left.
Initially, a piece is placed on (1, 1). You may repeat the following operation any number of times:
- Let (i, j) be the square the piece is currently on. Move the piece to the square whose distance from (i, j) is exactly \sqrt{M}.
Here, we define the distance between square (i, j) and square (k, l) as \sqrt{(i-k)^2+(j-l)^2}.
For all squares (i, j), determine if the piece can reach (i, j). If it can, find the minimum number of operations required to do so.
Constraints
- 1 \le N \le 400
- 1 \le M \le 10^6
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print N lines. The i-th line should contain N integers. If the piece can reach (i, j), the j-th integer in the i-th line should be the minimum number of operations required to do so; otherwise, it should be -1.
Sample Input 1
3 1
Sample Output 1
0 1 2 1 2 3 2 3 4
You can move the piece to four adjacent squares.
For example, we can move the piece to (2,2) with two operations as follows.
- The piece is now on (1,1). The distance between (1,1) and (1,2) is exactly \sqrt{1}, so move the piece to (1,2).
- The piece is now on (1,2). The distance between (1,2) and (2,2) is exactly \sqrt{1}, so move the piece to (2,2).
Sample Input 2
10 5
Sample Output 2
0 3 2 3 2 3 4 5 4 5 3 4 1 2 3 4 3 4 5 6 2 1 4 3 2 3 4 5 4 5 3 2 3 2 3 4 3 4 5 6 2 3 2 3 4 3 4 5 4 5 3 4 3 4 3 4 5 4 5 6 4 3 4 3 4 5 4 5 6 5 5 4 5 4 5 4 5 6 5 6 4 5 4 5 4 5 6 5 6 7 5 6 5 6 5 6 5 6 7 6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
ある国には都市が N 個あります。
あなたは、都市 1 にある営業所から 0 個以上の都市を経由して都市 N にある訪問先へ移動しようとしています。
移動手段は社用車と電車の 2 種類があります。都市 i から都市 j へ移動するときの所要時間は以下の通りです。
- 社用車を使った場合 : D_{i,j} \times A 分
- 電車を使った場合 : D_{i,j} \times B + C 分
ただし、社用車から電車に乗り換えることはできますが、電車から社用車に乗り換えることはできません。
また、乗り換えは各都市のみで行え、乗り換えに時間はかかりません。
都市 1 から都市 N に移動するのにかかる時間は最短で何分ですか?
制約
- 2 \leq N \leq 1000
- 1 \leq A, B, C \leq 10^6
- D_{i,j} \leq 10^6
- D_{i,i} = 0
- D_{i,j} = D_{j,i} > 0 (i \neq j)
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A B C D_{1,1} D_{1,2} \ldots D_{1,N} D_{2,1} D_{2,2} \ldots D_{2,N} \vdots D_{N,1} D_{N,2} \ldots D_{N,N}
出力
答えを整数として出力せよ。
入力例 1
4 8 5 13 0 6 2 15 6 0 3 5 2 3 0 13 15 5 13 0
出力例 1
78
以下のように移動することで合計 78 分で都市 1 から都市 4 に移動することができます。
- 都市 1 から都市 3 まで社用車で移動する。この移動には 2 \times 8 = 16 分かかる。
- 都市 3 から都市 2 まで社用車で移動する。この移動には 3 \times 8 = 24 分かかる。
- 都市 2 から都市 4 まで電車で移動する。この移動には 5 \times 5 + 13 = 38 分かかる。
78 分未満の時間で都市 1 から都市 4 に移動することはできません。
入力例 2
3 1 1000000 1000000 0 10 1 10 0 10 1 10 0
出力例 2
1
入力例 3
5 954257 954213 814214 0 84251 214529 10017 373342 84251 0 91926 32336 164457 214529 91926 0 108914 57762 10017 32336 108914 0 234705 373342 164457 57762 234705 0
出力例 3
168604826785
Score : 450 points
Problem Statement
There are N cities in a certain country.
You will travel from your office in city 1 to a destination in city N, via zero or more cities.
Two types of transportation are available: company car and train. The time required to travel from city i to city j is as follows:
- D_{i,j} \times A minutes by company car, and
- D_{i,j} \times B + C minutes by train.
You can switch from company car to train, but not vice versa.
You can do so without spending time, but only in a city.
What is the minimum time in minutes to travel from city 1 to city N?
Constraints
- 2 \leq N \leq 1000
- 1 \leq A, B, C \leq 10^6
- D_{i,j} \leq 10^6
- D_{i,i} = 0
- D_{i,j} = D_{j,i} > 0 (i \neq j)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A B C D_{1,1} D_{1,2} \ldots D_{1,N} D_{2,1} D_{2,2} \ldots D_{2,N} \vdots D_{N,1} D_{N,2} \ldots D_{N,N}
Output
Print the answer as an integer.
Sample Input 1
4 8 5 13 0 6 2 15 6 0 3 5 2 3 0 13 15 5 13 0
Sample Output 1
78
You can travel from city 1 to city 4 in a total of 78 minutes by moving as follows.
- Travel by company car from city 1 to city 3. This takes 2 \times 8 = 16 minutes.
- Travel by company car from city 3 to city 2. This takes 3 \times 8 = 24 minutes.
- Travel by train from city 2 to city 4. This takes 5 \times 5 + 13 = 38 minutes.
It is impossible to travel from city 1 to city 4 in less than 78 minutes.
Sample Input 2
3 1 1000000 1000000 0 10 1 10 0 10 1 10 0
Sample Output 2
1
Sample Input 3
5 954257 954213 814214 0 84251 214529 10017 373342 84251 0 91926 32336 164457 214529 91926 0 108914 57762 10017 32336 108914 0 234705 373342 164457 57762 234705 0
Sample Output 3
168604826785
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
(1,2, \dots, N) を並び替えた長さ N の順列 P = (p_1, p_2, \dots, p_N) に対して、 P のスコア S(P) を次のように定めます。
- N 人の人とすぬけ君がいて、N 人の人には 1,2,\dots,N の番号がついています。はじめ、人 i (1 \leq i \leq N) はボール i を持っています。
すぬけ君が叫ぶたびに、i \neq p_i であるようなすべての人 i は人 p_i に持っているボールを同時に渡します。
すぬけ君は、1 回以上叫んだ後にすべての人 i がボール i を持っている状態になると叫ぶのをやめます。
すぬけ君が叫ぶのをやめるまでに叫んだ回数が順列のスコアとなります。ここでスコアは有限の値を取ることが保証されます。
P としてあり得るものは N! 通りありますが、それらのスコアを K 乗した値の総和を 998244353 で割ったあまりを計算してください。
-
厳密に言い換えると、(1,2, \dots, N) を並び替えた長さ N の順列全体の集合を S_N として
\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}
を計算してください。
制約
- 2 \leq N \leq 50
- 1 \leq K \leq 10^4
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353} を出力せよ。
入力例 1
2 2
出力例 1
5
N = 2 のとき P としてあり得る順列は (1,2),(2,1) の 2 つです。
順列 (1,2) のスコアは次のように決まります。
- はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
すぬけ君が 1 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
よってスコアは 1 となります。
順列 (2,1) のスコアは次のように決まります。
- はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
すぬけ君が 1 回目に叫んだ後に、人 1 はボール 2 を、人 2 はボール 1 を持っています。
すぬけ君が 2 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
よってスコアは 2 となります。
よって 1^2 + 2^2 = 5 がこの問題の答えになります。
入力例 2
3 3
出力例 2
79
すべての順列とスコアの組を列挙すると以下のようになります。
- 順列 : (1,2,3), スコア : 1
- 順列 : (1,3,2), スコア : 2
- 順列 : (2,1,3), スコア : 2
- 順列 : (2,3,1), スコア : 3
- 順列 : (3,1,2), スコア : 3
- 順列 : (3,2,1), スコア : 2
よって 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79 を出力します。
入力例 3
50 10000
出力例 3
77436607
Score : 500 points
Problem Statement
For a permutation P = (p_1, p_2, \dots, p_N) of (1,2, \dots, N), let us define the score S(P) of P as follows.
- There are N people, numbered 1,2,\dots,N. Additionally, Snuke is there. Initially, Person i (1 \leq i \leq N) has Ball i.
Each time Snuke screams, every Person i such that i \neq p_i gives their ball to Person p_i simultaneously.
If, after screaming at least once, every Person i has Ball i, Snuke stops screaming.
The score is the number of times Snuke screams until he stops. Here, it is guaranteed that the score will be a finite value.
There are N! permutations P of (1,2, \dots, N). Find the sum, modulo 998244353, of the scores of those permutations, each raised to the K-th power.
- Formally, let S_N be the set of the permutations of (1,2, \dots, N). Compute the following: \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.
Constraints
- 2 \leq N \leq 50
- 1 \leq K \leq 10^4
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.
Sample Input 1
2 2
Sample Output 1
5
When N = 2, there are two possible permutations P: (1,2),(2,1).
The score of the permutation (1,2) is found as follows.
- Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
After Snuke's first scream, Person 1 has Ball 1, and Person 2 has Ball 2.
Here, every Person i has Ball i, so he stops screaming.
Thus, the score is 1.
The score of the permutation (2,1) is found as follows.
- Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
After Snuke's first scream, Person 1 has Ball 2, and Person 2 has Ball 1.
After Snuke's second scream, Person 1 has Ball 1, and Person 2 has Ball 2.
Here, every Person i has Ball i, so he stops screaming.
Thus, the score is 2.
Therefore, the answer in this case is 1^2 + 2^2 = 5.
Sample Input 2
3 3
Sample Output 2
79
All permutations and their scores are listed below.
- (1,2,3): The score is 1.
- (1,3,2): The score is 2.
- (2,1,3): The score is 2.
- (2,3,1): The score is 3.
- (3,1,2): The score is 3.
- (3,2,1): The score is 2.
Thus, we should print 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79.
Sample Input 3
50 10000
Sample Output 3
77436607