A - Digit Sum 2

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

N 以下の正の整数の 10 進法での各桁の和の最大値を求めてください。

制約

  • 1\leq N \leq 10^{16}
  • N は整数である

入力

入力は以下の形式で標準入力から与えられる。

N

出力

N 以下の正の整数の 10 進法での各桁の和の最大値を出力せよ。


入力例 1

100

出力例 1

18

例えば 99 の各桁の和は 18 で、これが求める最大値となります。


入力例 2

9995

出力例 2

35

例えば 9989 の各桁の和は 35 で、これが求める最大値となります。


入力例 3

3141592653589793

出力例 3

137

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

  • 1\leq N \leq 10^{16}
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.


Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.


Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.


Sample Input 3

3141592653589793

Sample Output 3

137
B - Holes

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 600

問題文

平面上に N 個の穴があります。i 個目の穴の座標は、(x_i,y_i) です。

R=10^{10^{10^{10}}} とします。りんごさんは、以下の操作を行います。

  • 原点を中心とする半径 R の円内から無作為に 1 点を選び、すぬけ君を置く。すぬけ君は、置かれた点からユークリッド距離が最も近い穴に移動し、落ちる。そのような穴が複数ある場合は、添え字の最も小さいものを選ぶ。

全ての 1\leq i\leq N に対し、すぬけ君が i 番目の穴に落ちる確率を求めてください。

ただし、半径 R の円内から無作為に 1 点を選ぶ操作とは、以下の操作を指します。

  • [-R,R] 上の独立な一様分布にしたがって実数 x,y を選ぶ。
  • もしx^2+y^2\leq R^2 なら、座標 (x,y) を選ぶ。そうでないなら、その条件が満たされるまで実数 x,y を選びなおし続ける。

制約

  • 2 \leq N \leq 100
  • |x_i|,|y_i| \leq 10^6(1\leq i\leq N)
  • 与えられる点は全て相異なる
  • 入力はすべて整数である

入力

入力は以下の形式で標準入力から与えられる。

N
x_1 y_1
:
x_N y_N

出力

実数を N 個出力せよ。i 個目の実数は、すぬけ君が i 番目の穴に落ちる確率を表さなければならない。

出力されたすべての値について絶対誤差あるいは相対誤差が 10^{-5} 以下のとき、正答と判定される。


入力例 1

2
0 0
1 1

出力例 1

0.5
0.5

りんごさんが x+y\leq 1 なる領域にすぬけ君を置いた場合、すぬけ君は 1 番目の穴に落ちます。このような確率は 0.5 に非常に近いです。 また、そうでない場合すぬけ君は 2 番目の穴に落ち、そのような確率も 0.5 に非常に近いです。


入力例 2

5
0 0
2 8
4 5
2 6
3 10

出力例 2

0.43160120892732328768
0.03480224363653196956
0.13880483535586193855
0.00000000000000000000
0.39479171208028279727

Score : 600 points

Problem Statement

There are N holes in a two-dimensional plane. The coordinates of the i-th hole are (x_i,y_i).

Let R=10^{10^{10^{10}}}. Ringo performs the following operation:

  • Randomly choose a point from the interior of a circle of radius R centered at the origin, and put Snuke there. Snuke will move to the hole with the smallest Euclidean distance from the point, and fall into that hole. If there are multiple such holes, the hole with the smallest index will be chosen.

For every i (1 \leq i \leq N), find the probability that Snuke falls into the i-th hole.

Here, the operation of randomly choosing a point from the interior of a circle of radius R is defined as follows:

  • Pick two real numbers x and y independently according to uniform distribution on [-R,R].
  • If x^2+y^2\leq R^2, the point (x,y) is chosen. Otherwise, repeat picking the real numbers x,y until the condition is met.

Constraints

  • 2 \leq N \leq 100
  • |x_i|,|y_i| \leq 10^6(1\leq i\leq N)
  • All given points are pairwise distinct.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print N real numbers. The i-th real number must represent the probability that Snuke falls into the i-th hole.

The output will be judged correct when, for all output values, the absolute or relative error is at most 10^{-5}.


Sample Input 1

2
0 0
1 1

Sample Output 1

0.5
0.5

If Ringo put Snuke in the region x+y\leq 1, Snuke will fall into the first hole. The probability of this happening is very close to 0.5. Otherwise, Snuke will fall into the second hole, the probability of which happening is also very close to 0.5.


Sample Input 2

5
0 0
2 8
4 5
2 6
3 10

Sample Output 2

0.43160120892732328768
0.03480224363653196956
0.13880483535586193855
0.00000000000000000000
0.39479171208028279727
C - Tiling

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 900

問題文

高橋君は、縦 N マス横 M マスのマス目を持っています。 次の条件をすべて満たすように、縦 1 マス横 2 マスのタイル A 枚と、縦 2 マス横 1 マスのタイル B 枚を マス目に置くことができるかどうかを判定し、可能なら置き方をひとつ構成してください。

  • マス目の上に、全てのタイルを置かなければならない。
  • タイルはマス目からはみ出してはならず、また異なるタイル同士が重なってはならない。
  • マス目やタイルを回転させてはならない。
  • 全てのタイルは、マス目のちょうど 2 マスを完全に覆う。

制約

  • 1 \leq N,M \leq 1000
  • 0 \leq A,B \leq 500000
  • N,M,A,B は整数である

入力

入力は以下の形式で標準入力から与えられる。

N M A B

出力

タイルを全て置くことができない場合、NO を出力せよ。 そうでない場合、以下のように出力せよ。

YES
c_{11}...c_{1M}
:
c_{N1}...c_{NM}

ただし、c_{ij} はマス目の ij 列目の状態を表し、文字 .,<.>,^,v のいずれかでなければならない。

c_{ij} が、

  • . のとき、マス目の ij 列目はタイルで覆われていないことを、
  • < のとき、マス目の ij 列目は縦 1 マス、横 2 マスのタイルの左半分で覆われていることを、
  • > のとき、マス目の ij 列目は縦 1 マス、横 2 マスのタイルの右半分で覆われていることを、
  • ^ のとき、マス目の ij 列目は縦 2 マス、横 1 マスのタイルの上半分で覆われていることを、
  • v のとき、マス目の ij 列目は縦 2 マス、横 1 マスのタイルの下半分で覆われていることを、

それぞれ表す。


入力例 1

3 4 4 2

出力例 1

YES
<><>
^<>^
v<>v

3 マス横 4 マスのマス目に、縦 1 マス横 2 マスのタイル 4 枚と、縦 2 マス横 1 マスのタイル 2 枚を置く方法の一例として、 出力例のようなものがあります。


入力例 2

4 5 5 3

出力例 2

YES
<>..^
^.<>v
v<>.^
<><>v

入力例 3

7 9 20 20

出力例 3

NO

Score : 900 points

Problem Statement

Takahashi has an N \times M grid, with N horizontal rows and M vertical columns. Determine if we can place A 1 \times 2 tiles (1 vertical, 2 horizontal) and B 2 \times 1 tiles (2 vertical, 1 horizontal) satisfying the following conditions, and construct one arrangement of the tiles if it is possible:

  • All the tiles must be placed on the grid.
  • Tiles must not stick out of the grid, and no two different tiles may intersect.
  • Neither the grid nor the tiles may be rotated.
  • Every tile completely covers exactly two squares.

Constraints

  • 1 \leq N,M \leq 1000
  • 0 \leq A,B \leq 500000
  • N, M, A and B are integers.

Input

Input is given from Standard Input in the following format:

N M A B

Output

If it is impossible to place all the tiles, print NO. Otherwise, print the following:

YES
c_{11}...c_{1M}
:
c_{N1}...c_{NM}

Here, c_{ij} must be one of the following characters: ., <, >, ^ and v. Represent an arrangement by using each of these characters as follows:

  • When c_{ij} is ., it indicates that the square at the i-th row and j-th column is empty;
  • When c_{ij} is <, it indicates that the square at the i-th row and j-th column is covered by the left half of a 1 \times 2 tile;
  • When c_{ij} is >, it indicates that the square at the i-th row and j-th column is covered by the right half of a 1 \times 2 tile;
  • When c_{ij} is ^, it indicates that the square at the i-th row and j-th column is covered by the top half of a 2 \times 1 tile;
  • When c_{ij} is v, it indicates that the square at the i-th row and j-th column is covered by the bottom half of a 2 \times 1 tile.

Sample Input 1

3 4 4 2

Sample Output 1

YES
<><>
^<>^
v<>v

This is one example of a way to place four 1 \times 2 tiles and three 2 \times 1 tiles on a 3 \times 4 grid.


Sample Input 2

4 5 5 3

Sample Output 2

YES
<>..^
^.<>v
v<>.^
<><>v

Sample Input 3

7 9 20 20

Sample Output 3

NO
D - Reversed LCS

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 900

問題文

高橋君はお母さんに文字列をプレゼントすることにしました。

文字列 T の価値とは、T を逆から読んだものを T' として、TT' の最長共通部分列の長さです。 すなわち、(連続するとは限らない)部分列として TT' の両方に現れるものの最大長です。

高橋君は、文字列 S を持っています。お母さんにできるだけ価値の高い文字列をプレゼントしたい高橋君は、 S の文字を K 箇所まで任意に変更して、できるだけ価値の高い文字列を作りたいです。

達成できる価値の最大値を求めてください。

制約

  • 1 \leq |S| \leq 300
  • 0 \leq K \leq |S|
  • S は英小文字からなる
  • K は整数である

入力

入力は以下の形式で標準入力から与えられる。

S
K

出力

達成できる価値の最大値を出力せよ。


入力例 1

abcabcabc
1

出力例 1

7

1 文字目を c に変更すると、文字列は cbcabcabc になります。 できた文字列を T とおけば、長さ 7 の文字列 cbabcbcTT' の最長共通部分列の一例となります。


入力例 2

atcodergrandcontest
3

出力例 2

15

Score : 900 points

Problem Statement

Takahashi has decided to give a string to his mother.

The value of a string T is the length of the longest common subsequence of T and T', where T' is the string obtained by reversing T. That is, the value is the longest length of the following two strings that are equal: a subsequence of T (possibly non-contiguous), and a subsequence of T' (possibly non-contiguous).

Takahashi has a string S. He wants to give her mother a string of the highest possible value, so he would like to change at most K characters in S to any other characters in order to obtain a string of the highest possible value. Find the highest possible value achievable.

Constraints

  • 1 \leq |S| \leq 300
  • 0 \leq K \leq |S|
  • S consists of lowercase English letters.
  • K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the highest possible value achievable.


Sample Input 1

abcabcabc
1

Sample Output 1

7

Changing the first character to c results in cbcabcabc. Let this tring be T, then one longest common subsequence of T and T' is cbabcbc, whose length is 7.


Sample Input 2

atcodergrandcontest
3

Sample Output 2

15
E - Ball Eat Chameleons

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1200

問題文

AtCoder 共和国では、カメレオン科ボールタベルカメレオン属に属するスヌケカメレオンがペットとして大人気です。 りんごさんは、N 匹のスヌケカメレオンの個体をひとつのカゴに入れて飼っています。

何も食べていない状態のスヌケカメレオンは青色です。スヌケカメレオンは、次の規則で変色します。

  • 青いスヌケカメレオンは、これまでに食べた青いボールの個数よりこれまでに食べた赤いボールの個数の方が真に大きくなった時、赤色に変色する。
  • 赤いスヌケカメレオンは、これまでに食べた赤いボールの個数よりこれまでに食べた青いボールの個数の方が真に大きくなった時、青色に変色する。

最初、スヌケカメレオンたちはどの個体も何も食べていない状態です。りんごさんは、スヌケカメレオンたちに、以下の手順を K 回繰り返すことで餌をやりました。

  • 赤いボールまたは青いボールを握る。
  • 握ったボールを、スヌケカメレオンたちの入ったカゴの中に投げ入れる。このとき、いずれか一匹がそのボールを食べる。

りんごさんが K 個のボールを投げ入れたところ、全ての個体が赤色になっていました。りんごさんの K 個のボールの投げ入れ方としてありうるものは何通りあるでしょうか。 998244353 で割った余りを求めてください。ただし、2 つの投げ入れ方が異なるとは、ある i が存在し、i 個目に投げ入れたボールの色が異なることを指します。

制約

  • 1 \leq N,K \leq 5 \times 10^5
  • N,K は整数である

入力

入力は以下の形式で標準入力から与えられる。

N K

出力

K 個のボールの投げ入れ方としてありうるものの個数を 998244353 で割った余りを出力せよ。


入力例 1

2 4

出力例 1

7

i 個目に投げ入れるボールが赤のとき R を、青のとき B を順に並べた文字列を用いて投げ入れ方を表せば、 BRRR,RBRB,RBRR,RRBB,RRBR,RRRB,RRRR7 個が条件を満たします。


入力例 2

3 7

出力例 2

57

入力例 3

8 3

出力例 3

0

入力例 4

8 10

出力例 4

46

入力例 5

123456 234567

出力例 5

857617983

Score : 1200 points

Problem Statement

In Republic of AtCoder, Snuke Chameleons (Family: Chamaeleonidae, Genus: Bartaberia) are very popular pets. Ringo keeps N Snuke Chameleons in a cage.

A Snuke Chameleon that has not eaten anything is blue. It changes its color according to the following rules:

  • A Snuke Chameleon that is blue will change its color to red when the number of red balls it has eaten becomes strictly larger than the number of blue balls it has eaten.
  • A Snuke Chameleon that is red will change its color to blue when the number of blue balls it has eaten becomes strictly larger than the number of red balls it has eaten.

Initially, every Snuke Chameleon had not eaten anything. Ringo fed them by repeating the following process K times:

  • Grab either a red ball or a blue ball.
  • Throw that ball into the cage. Then, one of the chameleons eats it.

After Ringo threw in K balls, all the chameleons were red. We are interested in the possible ways Ringo could have thrown in K balls. How many such ways are there? Find the count modulo 998244353. Here, two ways to throw in balls are considered different when there exists i such that the color of the ball that are thrown in the i-th throw is different.

Constraints

  • 1 \leq N,K \leq 5 \times 10^5
  • N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the possible ways Ringo could have thrown in K balls, modulo 998244353.


Sample Input 1

2 4

Sample Output 1

7

We will use R to represent a red ball, and B to represent a blue ball. There are seven ways to throw in balls that satisfy the condition: BRRR, RBRB, RBRR, RRBB, RRBR, RRRB and RRRR.


Sample Input 2

3 7

Sample Output 2

57

Sample Input 3

8 3

Sample Output 3

0

Sample Input 4

8 10

Sample Output 4

46

Sample Input 5

123456 234567

Sample Output 5

857617983
F - Trinity

Time Limit: 6 sec / Memory Limit: 256 MB

配点 : 1800

問題文

N マス、横 M マスのマス目があります。i 行目、j 列目にあるマスを (i,j) とあらわすことにします。 特に、一番左上のマスは (1,1) と、一番右下のマスは (N,M) とあらわされます。 高橋君は 0 個以上のいくつかのマスを黒く、他のマスを白く塗りました。

長さ N の数列 A と、長さ M の数列 B,C をそれぞれ以下のように定義するとき、列の組 (A,B,C) としてありうるものの個数を 998244353 で割った余りを求めてください。

  • A_i(1\leq i\leq N) は、マス (i,j) が黒く塗られているような最小の j (存在しない場合、M+1)
  • B_i(1\leq i\leq M) は、マス (k,i) が黒く塗られているような最小の k (存在しない場合、N+1)
  • C_i(1\leq i\leq M) は、マス (k,i) が黒く塗られているような最大の k (存在しない場合、0)

注意

この問題では、提出できるソースコードのサイズは 20000 B 以下に制限される。 制限を超える長さの提出は無効とするので注意すること。


制約

  • 1 \leq N \leq 8000
  • 1 \leq M \leq 200
  • N,M は整数である

部分点

  • N\leq 300 を満たすデータセットに正解すると、1500 点が与えられる。

入力

入力は以下の形式で標準入力から与えられる。

N M

出力

列の組 (A,B,C) の個数を 998244353 で割った余りを出力せよ。


入力例 1

2 3

出力例 1

64

N=2 なので、B_i,C_i の情報から、各列の黒く塗られたマスの配置が一意に定まります。各 i について、(B_i,C_i) の組は (1,1),(1,2),(2,2),(3,0)4 通りなので、答えは 4^M=64 通りです。


入力例 2

4 3

出力例 2

2588

入力例 3

17 13

出力例 3

229876268

入力例 4

5000 100

出力例 4

57613837

Score : 1800 points

Problem Statement

We have an N \times M grid. The square at the i-th row and j-th column will be denoted as (i,j). Particularly, the top-left square will be denoted as (1,1), and the bottom-right square will be denoted as (N,M). Takahashi painted some of the squares (possibly zero) black, and painted the other squares white.

We will define an integer sequence A of length N, and two integer sequences B and C of length M each, as follows:

  • A_i(1\leq i\leq N) is the minimum j such that (i,j) is painted black, or M+1 if it does not exist.
  • B_i(1\leq i\leq M) is the minimum k such that (k,i) is painted black, or N+1 if it does not exist.
  • C_i(1\leq i\leq M) is the maximum k such that (k,i) is painted black, or 0 if it does not exist.

How many triples (A,B,C) can occur? Find the count modulo 998244353.

Notice

In this problem, the length of your source code must be at most 20000 B. Note that we will invalidate submissions that exceed the maximum length.


Constraints

  • 1 \leq N \leq 8000
  • 1 \leq M \leq 200
  • N and M are integers.

Partial Score

  • 1500 points will be awarded for passing the test set satisfying N\leq 300.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of triples (A,B,C), modulo 998244353.


Sample Input 1

2 3

Sample Output 1

64

Since N=2, given B_i and C_i, we can uniquely determine the arrangement of black squares in each column. For each i, there are four possible pairs (B_i,C_i): (1,1), (1,2), (2,2) and (3,0). Thus, the answer is 4^M=64.


Sample Input 2

4 3

Sample Output 2

2588

Sample Input 3

17 13

Sample Output 3

229876268

Sample Input 4

5000 100

Sample Output 4

57613837