A - Ice Tea Store

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

あなたは行きつけの店、インフバリューにアイスティーを買いに来ています。

この店では、様々なサイズのボトル入りアイスティーが様々な価格で売られています。具体的な価格は、0.25 リットルサイズが一つ Q 円、0.5 リットルサイズが一つ H 円、1 リットルサイズが一つ S 円、2 リットルサイズが一つ D 円です。各サイズの在庫は無限にあります。

あなたはちょうど N リットルのアイスティーを買いたいです。これに必要な金額は何円でしょうか?

制約

  • 1 \leq Q, H, S, D \leq 10^8
  • 1 \leq N \leq 10^9
  • 入力値はすべて整数である。

入力

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

Q H S D
N

出力

ちょうど N リットルのアイスティーを買うのに必要な最小の金額を出力せよ。


入力例 1

20 30 70 90
3

出力例 1

150

2 リットルサイズ一つと 0.5 リットルサイズ二つを買うと、90 + 30 + 30 = 150 円で 3 リットルが手に入ります。


入力例 2

10000 1000 100 10
1

出力例 2

100

2 リットルサイズはたったの 10 円ですが、1 リットルしか必要ありません。ですから、1 リットルサイズを 100 円で買う必要があります。


入力例 3

10 100 1000 10000
1

出力例 3

40

今度は 0.25 リットルサイズ四つを 10 + 10 + 10 + 10 = 40 円で買うのがよいです。


入力例 4

12345678 87654321 12345678 87654321
123456789

出力例 4

1524157763907942

Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs. Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen. The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

  • 1 \leq Q, H, S, D \leq 10^8
  • 1 \leq N \leq 10^9
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.


Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.


Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter. Thus, you have to buy a 1-liter bottle for 100 yen.


Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.


Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942
B - Reverse and Compare

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

英小文字からなる文字列 A = A_1 A_2 ... A_n があります。

あなたは 1 \leq i \leq j \leq n であるような任意の二つの添字 i, j を選び、A のうち部分文字列 A_i A_{i+1} ... A_j を反転することができます。

この操作は一回まで行うことができます。

これによって得られる文字列は何通りあるでしょうか?

制約

  • 1 \leq |A| \leq 200,000
  • A は英小文字からなる。

入力

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

A

出力

A のうち任意の部分文字列を一回まで反転することによって、何通りの文字列が得られるか出力せよ。


入力例 1

aatt

出力例 1

5

得られる文字列は aatt(何もしない)、atatA[2..3] を反転)、attaA[2..4] を反転)、ttaaA[1..4] を反転)、taatA[1..3] を反転)です。


入力例 2

xxxxxxxxxx

出力例 2

1

どの部分文字列を反転しても、結果は xxxxxxxxxx です。


入力例 3

abracadabra

出力例 3

44

Score : 500 points

Problem Statement

You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.

You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j.

You can perform this operation at most once.

How many different strings can you obtain?

Constraints

  • 1 \leq |A| \leq 200,000
  • A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.


Sample Input 1

aatt

Sample Output 1

5

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).


Sample Input 2

xxxxxxxxxx

Sample Output 2

1

Whatever substring you reverse, you'll always get xxxxxxxxxx.


Sample Input 3

abracadabra

Sample Output 3

44
C - Fountain Walk

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 900

問題文

都市ネバーモアには、10^8 本の東西方向の通りと 10^8 本の南北方向の通りがあり、どちらにも 0 から 10^8-1 の番号が付けられています。隣接する二本の東西方向の通りの間の距離はちょうど 100 メートルで、隣接する二本の南北方向の通りの間の距離もちょうど 100 メートルです。

すべての東西方向の通りは、すべての南北方向の通りと交わります。すべての交差点は、交差する南北方向の通りの番号を x、東西方向の通りの番号を y として組 (x, y) で表されます。

この都市には N 個の噴水があり、交差点 (X_i, Y_i) に設置されています。 通常の交差点と異なり、これらの交差点には交差点を中心とした半径 10 メートルの円が噴水の外周として描かれており、その内部に道路はありません。

下の図に、都市の一角の道路や噴水の光景の例を示します。

1f931bf0c98ec6f07e612b0282cdb094.png

市長たちは、同じ通りを歩いている間に噴水を二回以上見たくありません。ですから、どの東西方向の通りにも噴水は一つまでしか設置されていませんし、南北方向の通りについても同様です。

市民が通行できるのは東西、南北方向の通りと噴水の外周です。交差点 (x_1, y_1) から (x_2, y_2) に移動するには、最短で何メートル歩く必要があるでしょうか?

制約

  • 0 \leq x_1, y_1, x_2, y_2 < 10^8
  • 1 \leq N \leq 200,000
  • 0 \leq X_i, Y_i < 10^8
  • i \neq j のとき X_i \neq X_j
  • i \neq j のとき Y_i \neq Y_j
  • 交差点 (x_1, y_1), (x_2, y_2) は異なり、これらの位置に噴水は設置されていない。
  • 入力値はすべて整数である。

入力

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

x_1 y_1 x_2 y_2
N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

出力

交差点 (x_1, y_1) から (x_2, y_2) に移動するために歩くべき最短距離をメートル単位で出力せよ。出力は、絶対誤差または相対誤差が 10^{-11} を超えなければ正答とみなされる。


入力例 1

1 1 6 5
3
3 2
5 3
2 4

出力例 1

891.415926535897938

最短経路の一つを下の図に示します。スタート地点は青の点、ゴール地点は紫の点、途中経路は赤の線です。

c49e52b9b79003f61b8a6efa5dad8ba3.png

入力例 2

3 5 6 4
3
3 2
5 3
2 4

出力例 2

400.000000000000000
f9090ab734c89424c413f3b583376990.png

入力例 3

4 2 2 2
3
3 2
5 3
2 4

出力例 3

211.415926535897938
4b76416161f27cad20333a9ac636e00f.png

Score : 900 points

Problem Statement

In the city of Nevermore, there are 10^8 streets and 10^8 avenues, both numbered from 0 to 10^8-1. All streets run straight from west to east, and all avenues run straight from south to north. The distance between neighboring streets and between neighboring avenues is exactly 100 meters.

Every street intersects every avenue. Every intersection can be described by pair (x, y), where x is avenue ID and y is street ID.

There are N fountains in the city, situated at intersections (X_i, Y_i). Unlike normal intersections, there's a circle with radius 10 meters centered at the intersection, and there are no road parts inside this circle.

The picture below shows an example of how a part of the city with roads and fountains may look like.

1f931bf0c98ec6f07e612b0282cdb094.png

City governors don't like encountering more than one fountain while moving along the same road. Therefore, every street contains at most one fountain on it, as well as every avenue.

Citizens can move along streets, avenues and fountain perimeters. What is the shortest distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2)?

Constraints

  • 0 \leq x_1, y_1, x_2, y_2 < 10^8
  • 1 \leq N \leq 200,000
  • 0 \leq X_i, Y_i < 10^8
  • X_i \neq X_j for i \neq j
  • Y_i \neq Y_j for i \neq j
  • Intersections (x_1, y_1) and (x_2, y_2) are different and don't contain fountains.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2
N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

Print the shortest possible distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2), in meters. Your answer will be considered correct if its absolute or relative error doesn't exceed 10^{-11}.


Sample Input 1

1 1 6 5
3
3 2
5 3
2 4

Sample Output 1

891.415926535897938

One possible shortest path is shown on the picture below. The path starts at the blue point, finishes at the purple point and follows along the red line.

c49e52b9b79003f61b8a6efa5dad8ba3.png

Sample Input 2

3 5 6 4
3
3 2
5 3
2 4

Sample Output 2

400.000000000000000
f9090ab734c89424c413f3b583376990.png

Sample Input 3

4 2 2 2
3
3 2
5 3
2 4

Sample Output 3

211.415926535897938
4b76416161f27cad20333a9ac636e00f.png
D - Shift and Flip

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

01 からなる同じ長さの二つの文字列 A = A_1 A_2 ... A_nB = B_1 B_2 ... B_n があります。

あなたは、以下の操作を任意の順序で何度でも行って A を変化させることができます。

  • A を一文字左にシフトする(すなわち、A = A_1 A_2 ... A_n として AA_2 A_3 ... A_n A_1 に置き換える)。
  • A を一文字右にシフトする(すなわち、A = A_1 A_2 ... A_n として AA_n A_1 A_2 ... A_{n-1} に置き換える)。
  • B_i = 1 であるような i をいずれか一つ選び、A_i を反転する(すなわち、A_i = 1 - A_i とする)。

あなたの目標は文字列 A, B を一致させることです。

これに必要な最小の操作回数を出力してください。ただし、これが不可能である場合は -1 と出力してください。

制約

  • 1 \leq |A| = |B| \leq 2,000
  • A, B01 からなる。

入力

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

A
B

出力

文字列 A, B を一致させるために必要な最小の操作回数を出力せよ。ただし、これが不可能である場合は -1 と出力せよ。


入力例 1

1010
1100

出力例 1

3

目標を達成する最短の操作列を一つ示します。

  • A_1 を反転: A = 0010
  • A を左にシフト: A = 0100
  • A_1 を再度反転: A = 1100

入力例 2

1
0

出力例 2

-1

A の唯一のビットを反転させる方法はありません。


入力例 3

11010
10001

出力例 3

4

目標を達成する最短の操作列を一つ示します。

  • A を右にシフト: A = 01101
  • A_5 を反転: A = 01100
  • A を左にシフト: A = 11000
  • A を再度左にシフト: A = 10001

入力例 4

0100100
1111111

出力例 4

5

A_1, A_3, A_4, A_6, A_7 を任意の順序で反転すればよいです。

Score : 1000 points

Problem Statement

You have two strings A = A_1 A_2 ... A_n and B = B_1 B_2 ... B_n of the same length consisting of 0 and 1.

You can transform A using the following operations in any order and as many times as you want:

  • Shift A by one character to the left (i.e., if A = A_1 A_2 ... A_n, replace A with A_2 A_3 ... A_n A_1).
  • Shift A by one character to the right (i.e., if A = A_1 A_2 ... A_n, replace A with A_n A_1 A_2 ... A_{n-1}).
  • Choose any i such that B_i = 1. Flip A_i (i.e., set A_i = 1 - A_i).

You goal is to make strings A and B equal.

Print the smallest number of operations required to achieve this, or -1 if the goal is unreachable.

Constraints

  • 1 \leq |A| = |B| \leq 2,000
  • A and B consist of 0 and 1.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the smallest number of operations required to make strings A and B equal, or -1 if the goal is unreachable.


Sample Input 1

1010
1100

Sample Output 1

3

Here is one fastest way to achieve the goal:

  • Flip A_1: A = 0010
  • Shift A to the left: A = 0100
  • Flip A_1 again: A = 1100

Sample Input 2

1
0

Sample Output 2

-1

There is no way to flip the only bit in A.


Sample Input 3

11010
10001

Sample Output 3

4

Here is one fastest way to achieve the goal:

  • Shift A to the right: A = 01101
  • Flip A_5: A = 01100
  • Shift A to the left: A = 11000
  • Shift A to the left again: A = 10001

Sample Input 4

0100100
1111111

Sample Output 4

5

Flip A_1, A_3, A_4, A_6 and A_7 in any order.

E - Shuffle and Swap

Time Limit: 2 sec / Memory Limit: 512 MB

配点 : 1700

問題文

01 からなる同じ長さの二つの文字列 A = A_1 A_2 ... A_nB = B_1 B_2 ... B_n があります。 A, B に含まれる 1 の個数は等しいです。

あなたは次のアルゴリズムによって A を変化させることにしました。

  • a_1, a_2, ..., a_k を、A1 が出現する位置の添字とする。
  • b_1, b_2, ..., b_k を、B1 が出現する位置の添字とする。
  • a, b の要素をそれぞれ無作為に並び替える。これらの無作為な並び替えは一様かつ独立である。
  • 1 から k までの各 i に対して、順に A_{a_i}A_{b_i} の値を入れ替える。

この手順のあと、文字列 A, B が一致する確率を P とします。

さらに、Z = P \times (k!)^2 とします。明らかに、Z は整数です。

Z998244353 で割った余りを求めてください。

制約

  • 1 \leq |A| = |B| \leq 10,000
  • A, B01 からなる。
  • A, B に含まれる 1 の個数は等しい。
  • A, B には 1 が少なくとも一つ含まれる。

部分点

  • 1 \leq |A| = |B| \leq 500 を満たすデータセットに正解すると、1200 点が与えられる。

入力

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

A
B

出力

Z998244353 で割った余りを出力せよ。


入力例 1

1010
1100

出力例 1

3

最初の二つのステップで、a = [1, 3], b = [1, 2] となります。a, b の要素を無作為に並び替えたあとの結果としてありうるものは次の 4 つです。

  • a = [1, 3], b = [1, 2]。はじめ A = 1010A_1A_1 の入れ替え後 A = 1010A_3A_2 の入れ替え後 A = 1100
  • a = [1, 3], b = [2, 1]。はじめ A = 1010A_1A_2 の入れ替え後 A = 0110A_3A_1 の入れ替え後 A = 1100
  • a = [3, 1], b = [1, 2]。はじめ A = 1010A_3A_1 の入れ替え後 A = 1010A_1A_2 の入れ替え後 A = 0110
  • a = [3, 1], b = [2, 1]。はじめ A = 1010A_3A_2 の入れ替え後 A = 1100A_1A_1 の入れ替え後 A = 1100

この 4 つの結果のうち、3 つで A = B となっています。よって、P = 3 / 4 であり、Z = 3 となります。


入力例 2

01001
01001

出力例 2

4

A の要素の入れ替えによって A が変化することはなく、したがって必ず A = B となります。


入力例 3

101010
010101

出力例 3

36

三回の A の要素の入れ替えがどのように起こっても、A に含まれる 1 は適切な位置に移動します。


入力例 4

1101011011110
0111101011101

出力例 4

932171449

Score : 1700 points

Problem Statement

You have two strings A = A_1 A_2 ... A_n and B = B_1 B_2 ... B_n of the same length consisting of 0 and 1. The number of 1's in A and B is equal.

You've decided to transform A using the following algorithm:

  • Let a_1, a_2, ..., a_k be the indices of 1's in A.
  • Let b_1, b_2, ..., b_k be the indices of 1's in B.
  • Replace a and b with their random permutations, chosen independently and uniformly.
  • For each i from 1 to k, in order, swap A_{a_i} and A_{b_i}.

Let P be the probability that strings A and B become equal after the procedure above.

Let Z = P \times (k!)^2. Clearly, Z is an integer.

Find Z modulo 998244353.

Constraints

  • 1 \leq |A| = |B| \leq 10,000
  • A and B consist of 0 and 1.
  • A and B contain the same number of 1's.
  • A and B contain at least one 1.

Partial Score

  • 1200 points will be awarded for passing the testset satisfying 1 \leq |A| = |B| \leq 500.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the value of Z modulo 998244353.


Sample Input 1

1010
1100

Sample Output 1

3

After the first two steps, a = [1, 3] and b = [1, 2]. There are 4 possible scenarios after shuffling a and b:

  • a = [1, 3], b = [1, 2]. Initially, A = 1010. After swap(A_1, A_1), A = 1010. After swap(A_3, A_2), A = 1100.
  • a = [1, 3], b = [2, 1]. Initially, A = 1010. After swap(A_1, A_2), A = 0110. After swap(A_3, A_1), A = 1100.
  • a = [3, 1], b = [1, 2]. Initially, A = 1010. After swap(A_3, A_1), A = 1010. After swap(A_1, A_2), A = 0110.
  • a = [3, 1], b = [2, 1]. Initially, A = 1010. After swap(A_3, A_2), A = 1100. After swap(A_1, A_1), A = 1100.

Out of 4 scenarios, 3 of them result in A = B. Therefore, P = 3 / 4, and Z = 3.


Sample Input 2

01001
01001

Sample Output 2

4

No swap ever changes A, so we'll always have A = B.


Sample Input 3

101010
010101

Sample Output 3

36

Every possible sequence of three swaps puts the 1's in A into the right places.


Sample Input 4

1101011011110
0111101011101

Sample Output 4

932171449
F - Yes or No

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 2000

問題文

あなたは N + M 問のマルバツクイズが出題されるクイズゲームに参加します。

出題される問題のうち、N 問の正解がマル、M 問の正解がバツであることは事前に知らされていますが、問題は無作為な順序で出題されます。

あなたにはどの問題の正解も見当がつきません。 問題には一問ずつ解答していき、解答するごとにその問題の正解をすぐに知ることができます。

ここで、あなたが問題に正解する回数の期待値を最大化する戦略をとったと仮定します。

この期待値を P/Q(既約分数)とします。また、M = 998244353 とします。このとき、0 以上 M - 1 以下の整数 R がただ一つ存在して P = Q \times R mod M となることが証明でき、その値は P \times Q^{-1} mod M と等しくなります。ここで、Q^{-1}Q のモジュラ逆数です。R を求めてください。

制約

  • 1 \leq N, M \leq 500,000
  • N, M はともに整数である。

部分点

  • N = M および 1 \leq N, M \leq 10^5 を満たすデータセットに正解すると、1500 点が与えられる。

入力

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

N M

出力

P/Q を最適な戦略に従った場合の問題に正解する回数の期待値を表す既約分数とする。P \times Q^{-1} mod 998244353 を出力せよ。


入力例 1

1 1

出力例 1

499122178

問題が二問あります。 一問目には無作為に答えてよく、正解する確率は 50% です。 そして、二問目の答えは一問目と異なることが分かっているため、二問目に正解する確率は 100% です。

以上から、正解数の期待値は 3 / 2 です。 したがって、P = 3, Q = 2, Q^{-1} = 499122177 (mod 998244353), P \times Q^{-1} = 499122178 (mod 998244353) となります。


入力例 2

2 2

出力例 2

831870297

正解数の期待値は 17 / 6 です。


入力例 3

3 4

出力例 3

770074220

正解数の期待値は 169 / 35 です。


入力例 4

10 10

出力例 4

208827570

入力例 5

42 23

出力例 5

362936761

Score : 2000 points

Problem Statement

You are participating in a quiz with N + M questions and Yes/No answers.

It's known in advance that there are N questions with answer Yes and M questions with answer No, but the questions are given to you in random order.

You have no idea about correct answers to any of the questions. You answer questions one by one, and for each question you answer, you get to know the correct answer immediately after answering.

Suppose you follow a strategy maximizing the expected number of correct answers you give.

Let this expected number be P/Q, an irreducible fraction. Let M = 998244353. It can be proven that a unique integer R between 0 and M - 1 exists such that P = Q \times R modulo M, and it is equal to P \times Q^{-1} modulo M, where Q^{-1} is the modular inverse of Q. Find R.

Constraints

  • 1 \leq N, M \leq 500,000
  • Both N and M are integers.

Partial Score

  • 1500 points will be awarded for passing the testset satisfying N = M and 1 \leq N, M \leq 10^5.

Input

Input is given from Standard Input in the following format:

N M

Output

Let P/Q be the expected number of correct answers you give if you follow an optimal strategy, represented as an irreducible fraction. Print P \times Q^{-1} modulo 998244353.


Sample Input 1

1 1

Sample Output 1

499122178

There are two questions. You may answer randomly to the first question, and you'll succeed with 50% probability. Then, since you know the second answer is different from the first one, you'll succeed with 100% probability.

The expected number of your correct answers is 3 / 2. Thus, P = 3, Q = 2, Q^{-1} = 499122177 (modulo 998244353), and P \times Q^{-1} = 499122178 (again, modulo 998244353).


Sample Input 2

2 2

Sample Output 2

831870297

The expected number of your correct answers is 17 / 6.


Sample Input 3

3 4

Sample Output 3

770074220

The expected number of your correct answers is 169 / 35.


Sample Input 4

10 10

Sample Output 4

208827570

Sample Input 5

42 23

Sample Output 5

362936761