実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
AtCoder 市では市長選挙が行われています。候補者は高橋氏と青木氏です。
2 人のどちらかに投じられた有効票は N 票あり、現在開票が行われています。なお、 N は奇数です。
現在の開票作業の途中経過は高橋氏に T 票、青木氏に A 票です。
現時点で勝敗が確定しているかを判定してください。
制約
- 1 \leq N \leq 99
- N は奇数
- 0 \leq T,A \leq N
- T+A \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N T A
出力
現時点で勝敗が確定しているならば Yes 、そうでなければ No と出力せよ。
入力例 1
7 4 2
出力例 1
Yes
残りの 1 票が青木氏に入っても、高橋氏は勝利します。高橋氏の勝利が確定しているため、Yes と出力します。
入力例 2
99 12 48
出力例 2
No
現時点では青木氏が多く票を獲得していますが、高橋氏が残りの 39 票を獲得すると高橋氏が勝利します。よって、No と出力します。
入力例 3
1 0 0
出力例 3
No
Score : 100 points
Problem Statement
A mayoral election is being held in AtCoder City. The candidates are Takahashi and Aoki.
There are N valid votes cast for either of the two candidates, and the counting is currently underway. Here, N is an odd number.
The current vote count is T votes for Takahashi and A votes for Aoki.
Determine if the outcome of the election is already decided at this point.
Constraints
- 1 \leq N \leq 99
- N is an odd number.
- 0 \leq T, A \leq N
- T + A \leq N
- All input values are integers.
Input
The input is given from standard input in the following format:
N T A
Output
Print Yes if the outcome of the election is already decided, and No otherwise.
Sample Input 1
7 4 2
Sample Output 1
Yes
Even if the remaining one vote goes to Aoki, Takahashi will still win. That is, his victory is decided, so print Yes.
Sample Input 2
99 12 48
Sample Output 2
No
Although Aoki currently has more votes, Takahashi would win if he receives the remaining 39 votes. Therefore, print No.
Sample Input 3
1 0 0
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
AtCoder Land は、 10 時ちょうど、 15 時ちょうど、 17 時ちょうどの 3 つのタイミングでしか入場できません。
高橋君は X 時ちょうどに AtCoder Land に到着しました。最短で何時ちょうどに入園できますか?
但し、高橋君の到着と同じタイミングに AtCoder Land に入場できる場合も、高橋君は AtCoder Land に入場できるものとします。
制約
- X は 6 \le X \le 17 を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを整数として出力せよ。
入力例 1
14
出力例 1
15
高橋君は 14 時ちょうどに AtCoder Land に到着しました。高橋君は 15 時ちょうどに入園できます。
入力例 2
17
出力例 2
17
高橋君は 17 時ちょうどに AtCoder Land に到着しました。高橋君は 17 時ちょうどに入園できます。
この入力は、高橋君の到着と同じタイミングに AtCoder Land に入場できる場合に対応します。
入力例 3
6
出力例 3
10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S が良い文字列であるとは、すべての 1 以上の整数 i について次の性質が成り立つことであるとします。
- S にちょうど i 回現れる文字はちょうど 0 種類またはちょうど 2 種類ある
文字列 S が与えられるので、 S が良い文字列か判定してください。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が良い文字列ならば Yes を、そうでないならば No を出力せよ。
入力例 1
commencement
出力例 1
Yes
文字列 commencement にちょうど i 回現れる文字の種類数は以下のとおりです。
- i=1:
o,tの 2 種類 - i=2:
c,nの 2 種類 - i=3:
e,mの 2 種類 - i\geq 4: 0 種類
よって、commencement は良い文字列の条件を満たします。
入力例 2
banana
出力例 2
No
文字列 banana にちょうど 1 回現れる文字は b の 1 種類だけであり、良い文字列の条件を満たしません。
入力例 3
ab
出力例 3
Yes
Score: 200 points
Problem Statement
A string S consisting of lowercase English letters is a good string if and only if it satisfies the following property for all integers i not less than 1:
- There are exactly zero or exactly two different letters that appear exactly i times in S.
Given a string S, determine if it is a good string.
Constraints
- S is a string of lowercase English letters with a length between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if S is a good string, and No otherwise.
Sample Input 1
commencement
Sample Output 1
Yes
For the string commencement, the number of different letters that appear exactly i times is as follows:
- i=1: two letters (
oandt) - i=2: two letters (
candn) - i=3: two letters (
eandm) - i\geq 4: zero letters
Therefore, commencement satisfies the condition of a good string.
Sample Input 2
banana
Sample Output 2
No
For the string banana, there is only one letter that appears exactly one time, which is b, so it does not satisfy the condition of a good string.
Sample Input 3
ab
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は野球をモチーフにしたゲームを作ろうとしましたが、うまくコードが書けなくて困っています。
高橋君の代わりに次の問題を解くプログラムを作ってください。
マス 0, マス 1, マス 2, マス 3 の 4 つのマス目があります。はじめマスの上には何もありません。
また、整数 P があり、はじめ P = 0 です。
正の整数からなる数列 A = (A_1, A_2, \dots, A_N) が与えられるので、i = 1, 2, \dots, N について順番に次の操作を行います。
- マス 0 に駒を 1 個置く。
- マス上のすべての駒を番号が A_i 大きいマスに進める。言い換えると、駒がマス x にあればその駒をマス x + A_i に移動する。
ただし移動先のマスが存在しない (すなわち x + A_i が 4 以上になる) 駒たちに関しては、それらを取り除いて P に取り除いた個数を加算する。
すべての操作を行った後の P の値を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
操作終了時点での P の値を出力せよ。
入力例 1
4 1 1 3 2
出力例 1
3
操作を説明すると次のようになり、操作終了時点での P の値は 3 になります。
- i=1 での操作
- マス 0 に駒を置く。この時点でマス 0 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1 に駒が乗っている。
- i=2 での操作
- マス 0 に駒を置く。この時点でマス 0, 1 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1, 2 に駒が乗っている。
- i=3 での操作
- マス 0 に駒を置く。この時点でマス 0, 1, 2 にコマが乗っている。
- すべての駒を 3 大きいマスに進める。
この時、マス 1,2 にある駒は移動先のマスが存在しないため (それぞれ 1+3=4,2+3=5 なので) 、盤上から取り除いて P に 2 を加算する。P の値は 2 になる。
移動を終えた時点でマス 3 に駒が乗っている。
- i=4 での操作
- マス 0 に駒を置く。この時点でマス 0, 3 にコマが乗っている。
- すべての駒を 2 大きいマスに進める。
この時、マス 3 にある駒は移動先のマスが存在しないため (3+2=5 なので) 、盤上から取り除いて P に 1 を加算する。P の値は 3 になる。
移動を終えた時点でマス 2 に駒が乗っている。
入力例 2
3 1 1 1
出力例 2
0
P の値が操作中に変化しない場合もあります。
入力例 3
10 2 2 4 1 1 1 4 2 2 1
出力例 3
8
Score : 200 points
Problem Statement
Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.
There are 4 squares called Square 0, Square 1, Square 2, and Square 3. Initially, all squares are empty.
There is also an integer P; initially, P = 0.
Given a sequence of positive integers A = (A_1, A_2, \dots, A_N), perform the following operations for i = 1, 2, \dots, N in this order:
- Put a piece on Square 0.
- Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i).
If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4) for a piece, remove it. Add to P the number of pieces that have been removed.
Print the value of P after all the operations have been performed.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the value of P after all the operations have been performed.
Sample Input 1
4 1 1 3 2
Sample Output 1
3
The operations are described below. After all the operations have been performed, P equals 3.
- The operations for i=1:
- Put a piece on Square 0. Now, Square 0 has a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Square 1 has a piece.
- The operations for i=2:
- Put a piece on Square 0. Now, Squares 0 and 1 have a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Squares 1 and 2 have a piece.
- The operations for i=3:
- Put a piece on Square 0. Now, Squares 0, 1, and 2 have a piece.
- Advance every piece on the squares 3 squares ahead.
Here, for the pieces on Squares 1 and 2, the destination squares do not exist (since 1+3=4 and 2+3=5), so remove these pieces and add 2 to P. P now equals 2. After these moves, Square 3 has a piece.
- The operations for i=4:
- Put a piece on Square 0. Now, Squares 0 and 3 have a piece.
- Advance every piece on the squares 2 squares ahead.
Here, for the piece on Square 3, the destination square does not exist (since 3+2=5), so remove this piece and add 1 to P. P now equals 3.
After these moves, Square 2 has a piece.
Sample Input 2
3 1 1 1
Sample Output 2
0
The value of P may not be updated by the operations.
Sample Input 3
10 2 2 4 1 1 1 4 2 2 1
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる長さ N の文字列 S = S_1S_2\ldots S_N が与えられます。
また、S に関する Q 個の質問が与えられます。 i = 1, 2, \ldots, Q について、i 番目の質問は 2 つの整数 l_i, r_i で表される下記の質問です。
S の l_i 文字目から r_i 文字目までからなる部分文字列 S_{l_i}S_{l_i+1}\ldots S_{r_i} において、 同じ英小文字が 2 つ隣りあう箇所は何個ありますか? すなわち、l_i \leq p \leq r_i-1 かつ S_p = S_{p+1}を満たす整数 p は何個ありますか?
Q 個の質問それぞれの答えを出力してください。
制約
- N, Q は整数
- 1 \leq N, Q \leq 3 \times 10^5
- S は英小文字のみからなる長さ N の文字列
- l_i, r_i は整数
- 1 \leq l_i \leq r_i \leq N
入力
入力は以下の形式で標準入力から与えられる。
N Q S l_1 r_1 l_2 r_2 \vdots l_Q r_Q
出力
Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には i 番目の質問に対する答えを出力せよ。
入力例 1
11 4 mississippi 3 9 4 10 4 6 7 7
出力例 1
2 2 0 0
4 個の質問それぞれに対する答えは下記の通りです。
- 1 個目の質問に関して、S_3S_4\ldots S_9 =
ssissipで同じ英小文字が隣り合う箇所は、S_3S_4 =ssと S_6S_7 =ssの 2 個です。 - 2 個目の質問に関して、S_4S_5\ldots S_{10} =
sissippで同じ英小文字が隣り合う箇所は、S_6S_7 =ssと S_9S_{10} =ppの 2 個です。 - 3 個目の質問に関して、S_4S_5S_6 =
sisで同じ英小文字が隣り合う箇所は 0 個です。 - 4 個目の質問に関して、S_7 =
sで同じ英小文字が隣り合う箇所は 0 個です。
入力例 2
5 1 aaaaa 1 5
出力例 2
4
S_1S_2\ldots S_5 = aaaaa で同じ英小文字が隣り合う箇所は、
S_1S_2 = aa 、S_2S_3 = aa 、S_3S_4 = aa 、S_4S_5 = aa の 4 個です。
Score : 300 points
Problem Statement
You are given a string S = S_1S_2\ldots S_N of length N consisting of lowercase English letters.
Additionally, you are given Q queries about the string S. For i = 1, 2, \ldots, Q, the i-th query is represented by two integers l_i, r_i and asks the following.
In the substring S_{l_i}S_{l_i+1}\ldots S_{r_i} of S, which ranges from the l_i-th to the r_i-th character, how many places are there where the same lowercase English letter occurs twice in a row? In other words, how many integers p satisfy l_i \leq p \leq r_i-1 and S_p = S_{p+1}?
Print the answer for each of the Q queries.
Constraints
- N and Q are integers.
- 1 \leq N, Q \leq 3 \times 10^5
- S is a string of length N consisting of lowercase English letters.
- l_i and r_i are integers.
- 1 \leq l_i \leq r_i \leq N
Input
The input is given from Standard Input in the following format:
N Q S l_1 r_1 l_2 r_2 \vdots l_Q r_Q
Output
Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th query.
Sample Input 1
11 4 mississippi 3 9 4 10 4 6 7 7
Sample Output 1
2 2 0 0
The answers to the four queries are as follows.
- For the first query, S_3S_4\ldots S_9 =
ssissiphas two places where the same lowercase English letter occurs twice in a row: S_3S_4 =ssand S_6S_7 =ss. - For the second query, S_4S_5\ldots S_{10} =
sissipphas two places where the same lowercase English letter occurs twice in a row: S_6S_7 =ssand S_9S_{10} =pp. - For the third query, S_4S_5S_6 =
sishas zero places where the same lowercase English letter occurs twice in a row. - For the fourth query, S_7 =
shas zero places where the same lowercase English letter occurs twice in a row.
Sample Input 2
5 1 aaaaa 1 5
Sample Output 2
4
S_1S_2\ldots S_5 = aaaaa has four places where the same lowercase English letter occurs twice in a row:
S_1S_2 = aa, S_2S_3 = aa, S_3S_4 = aa, and S_4S_5 = aa.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
上下左右に広がる N\times N のマス目があり、最初全てのマスは白く塗られています。このマス目の上から i 行目、左から j 列目のマスを (i,j) で表します。
高橋君は 1 以上 N 以下の整数 A, B を持っており、次のような操作を行います。
- \max(1-A,1-B)\leq k\leq \min(N-A,N-B) をみたす全ての整数 k について、(A+k,B+k) を黒く塗る。
- \max(1-A,B-N)\leq k\leq \min(N-A,B-1) をみたす全ての整数 k について、(A+k,B-k) を黒く塗る。
この操作を行った後のマス目について、P\leq i\leq Q かつ R\leq j\leq S をみたす各マス (i,j) がそれぞれ何色で塗られているか求めてください。
制約
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A B P Q R S
出力
Q-P+1 行出力せよ。
各行は # と . のみからなる長さ S-R+1 の文字列であり、
i 行目の文字列の j 番目の文字が
# であることは (P+i-1,R+j-1) が黒く塗られていることを、
. であることは (P+i-1,R+j-1) が白く塗られていることをさす。
入力例 1
5 3 2 1 5 1 5
出力例 1
...#. #.#.. .#... #.#.. ...#.
1 つめの操作で (2,1), (3,2), (4,3), (5,4) の 4 マスが、
2 つめの操作で (4,1), (3,2), (2,3), (1,4) の 4 マスが黒く塗られます。
よって、P=1, Q=5, R=1, S=5 より、上のように出力します。
入力例 2
5 3 3 4 5 2 5
出力例 2
#.#. ...#
操作によって、
(1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5) の 9 マスが
黒く塗られます。
P=4, Q=5, R=2, S=5 より、上のように出力します。
入力例 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
出力例 3
#.# .#. #.#
入力が 32 bit 整数型に収まらないことがあることに注意してください。
Score : 300 points
Problem Statement
There is an N\times N grid with horizontal rows and vertical columns, where all squares are initially painted white. Let (i,j) denote the square at the i-th row and j-th column.
Takahashi has integers A and B, which are between 1 and N (inclusive). He will do the following operations.
- For every integer k such that \max(1-A,1-B)\leq k\leq \min(N-A,N-B), paint (A+k,B+k) black.
- For every integer k such that \max(1-A,B-N)\leq k\leq \min(N-A,B-1), paint (A+k,B-k) black.
In the grid after these operations, find the color of each square (i,j) such that P\leq i\leq Q and R\leq j\leq S.
Constraints
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B P Q R S
Output
Print Q-P+1 lines.
Each line should contain a string of length S-R+1 consisting of # and ..
The j-th character of the string in the i-th line should be # to represent that (P+i-1, R+j-1) is painted black, and . to represent that (P+i-1, R+j-1) is white.
Sample Input 1
5 3 2 1 5 1 5
Sample Output 1
...#. #.#.. .#... #.#.. ...#.
The first operation paints the four squares (2,1), (3,2), (4,3), (5,4) black, and the second paints the four squares (4,1), (3,2), (2,3), (1,4) black.
Thus, the above output should be printed, since P=1, Q=5, R=1, S=5.
Sample Input 2
5 3 3 4 5 2 5
Sample Output 2
#.#. ...#
The operations paint the nine squares (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5).
Thus, the above output should be printed, since P=4, Q=5, R=2, S=5.
Sample Input 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
Sample Output 3
#.# .#. #.#
Note that the input may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
整数 N が与えられます。以下の条件を満たす N 以下の正整数の組 (i,j) の個数を求めてください。
- i \times j は平方数である。
制約
- 1 \le N \le 2 \times 10^5
- N は整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
4
出力例 1
6
(1,1),(1,4),(2,2),(3,3),(4,1),(4,4) の 6 個が条件を満たします。
(2,3) は 2 \times 3 =6 が平方数でないため条件を満たしません。
入力例 2
254
出力例 2
896
Score : 400 points
Problem Statement
You are given an integer N. Find the number of pairs (i,j) of positive integers at most N that satisfy the following condition:
- i \times j is a square number.
Constraints
- 1 \le N \le 2 \times 10^5
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
4
Sample Output 1
6
The six pairs (1,1),(1,4),(2,2),(3,3),(4,1),(4,4) satisfy the condition.
On the other hand, (2,3) does not, since 2 \times 3 =6 is not a square number.
Sample Input 2
254
Sample Output 2
896
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
1, \dots, N と番号づけられた N 個の都市と、1, \dots, M と番号づけられた M 本の道があります。
全ての道は一方通行であり、道 i \, (1 \leq i \leq M) を通ると、都市 A_i から都市 B_i へ移動することができます。また、道 i の長さは C_i です。
1 以上 M 以下の整数からなる長さ K の数列 E = (E_1, \dots, E_K) が与えられます。都市 1 から都市 N までいくつかの道を使って移動する方法であって、以下の条件を満たすものを良い経路と呼びます。
- 通る道の番号を通った順番に並べた列は、E の部分列である。
なお、部分列とは、数列から 0 個以上の要素を削除し、残った要素を元の順序で並べて得られる数列のことを指します。
全ての良い経路における、通る道の長さの合計の最小値を求めてください。
ただし、良い経路が存在しない場合は、そのことを報告してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
出力
全ての良い経路における、通る道の長さの合計の最小値を出力せよ。ただし、良い経路が存在しないならば、-1 を出力せよ。
入力例 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
出力例 1
4
良い経路として考えられるのは次の二つです。
- 道 4 を使う。通る道の長さの合計は 5 である。
- 道 1, 2 をこの順で使う。通る道の長さの合計は 2 + 2 = 4 である。
よって、求める最小値は 4 です。
入力例 2
3 2 3 1 2 1 2 3 1 2 1 1
出力例 2
-1
良い経路は存在しません。
入力例 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
出力例 3
14
Score : 500 points
Problem Statement
There are N towns numbered 1, \dots, N, and M roads numbered 1, \dots, M.
Every road is directed; road i (1 \leq i \leq M) leads you from Town A_i to Town B_i. The length of road i is C_i.
You are given a sequence E = (E_1, \dots, E_K) of length K consisting of integers between 1 and M. A way of traveling from town 1 to town N using roads is called a good path if:
- the sequence of the roads' numbers arranged in the order used in the path is a subsequence of E.
Note that a subsequence of a sequence is a sequence obtained by removing 0 or more elements from the original sequence and concatenating the remaining elements without changing the order.
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, report that fact.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
Output
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, print -1.
Sample Input 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
Sample Output 1
4
There are two possible good paths as follows:
- Using road 4. In this case, the sum of the length of the used road is 5.
- Using road 1 and 2 in this order. In this case, the sum of the lengths of the used roads is 2 + 2 = 4.
Therefore, the desired minimum value is 4.
Sample Input 2
3 2 3 1 2 1 2 3 1 2 1 1
Sample Output 2
-1
There is no good path.
Sample Input 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
Sample Output 3
14
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
1 から 9 までの数字のみで構成された文字列 S が与えられます。
この文字列 S から、以下の操作によって式 T を作ります。
- はじめ、 T=S であるとする。
- 各要素が 1 以上 |S|-1 以下の整数である、値に重複のない集合 A を選ぶ。なお、 A が空集合であってもよい。
- A の全ての要素 x について、 x の降順に以下の操作を行う。
- T の x 文字目と x+1 文字目の間に、
+を挿入する。
- T の x 文字目と x+1 文字目の間に、
例えば、 S= 1234、 A= \lbrace 2,3 \rbrace であるとき、 T= 12+3+4 となります。
この操作によって得られる T としてあり得る全ての式に対して、式を計算したときの値の総和を 998244353 で割った余りを求めてください。
制約
- 1 \le |S| \le 2 \times 10^5
- S は
1,2,3,4,5,6,7,8,9のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
1234
出力例 1
1736
式 T としてあり得るものは 1234, 123+4, 12+34, 12+3+4, 1+234, 1+23+4, 1+2+34, 1+2+3+4 の 8 つです。
これらを計算した値の総和は 1736 です。
入力例 2
1
出力例 2
1
S の長さが 1 であることもあります。この場合、 A として指定可能なのは空集合のみです。
入力例 3
31415926535897932384626433832795
出力例 3
85607943
答えを 998244353 で割った余りを求めてください。
Score : 500 points
Problem Statement
Given is a string S consisting of digits from 1 through 9.
From this string S, let us make a formula T by the following operations.
- Initially, let T=S.
- Choose a (possibly empty) set A of different integers where each element is between 1 and |S|-1 (inclusive).
- For each element x in descending order, do the following.
- Insert a
+between the x-th and (x+1)-th characters of T.
- Insert a
For example, when S= 1234 and A= \lbrace 2,3 \rbrace, we will have T= 12+3+4.
Consider evaluating all possible formulae T obtained by these operations. Find the sum, modulo 998244353, of the evaluations.
Constraints
- 1 \le |S| \le 2 \times 10^5
- S consists of
1,2,3,4,5,6,7,8, and9.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
1234
Sample Output 1
1736
There are eight formulae that can be obtained as T: 1234, 123+4, 12+34, 12+3+4, 1+234, 1+23+4, 1+2+34, and 1+2+3+4.
The sum of the evaluations of these formulae is 1736.
Sample Input 2
1
Sample Output 2
1
S may have a length of 1, in which case the only possible choice for A is the empty set.
Sample Input 3
31415926535897932384626433832795
Sample Output 3
85607943
Be sure to find the sum modulo 998244353.