A - Can you get AC?

Time Limit: 2 sec / Memory Limit: 256 MB

配点: 100

問題文

すぬけ君は、あるプログラミングコンテストのためにジャッジシステムを作りました。

このジャッジシステムにプログラムを提出すると、文字列 S からある連続する 2 文字を取り出した文字列が結果として返ってきます (どの連続する 2 文字も結果として返ってくることがありえます)。

このジャッジシステムにプログラムを提出した結果として AC という文字列が返ってくることがありえるかどうか判定してください。

制約

  • 2 \leq |S| \leq 5
  • S は英大文字からなる。

入力

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

S

出力

ジャッジシステムにプログラムを提出した結果として AC という文字列が返ってくることがありえるならば Yes を、ありえないならば No を出力せよ。


入力例 1

BACD

出力例 1

Yes

BACD という文字列の 2 文字目と 3 文字目を取り出すと AC という文字列になります。


入力例 2

ABCD

出力例 2

No

ABCD という文字列の 1 文字目と 3 文字目を取り出してつなげると AC という文字列になりますが、これらの文字は連続した 2 文字ではないので、 プログラムの提出結果として返ってくることはありません。


入力例 3

CABD

出力例 3

No

入力例 4

ACACA

出力例 4

Yes

入力例 5

XX

出力例 5

No

Score : 100 points

Problem Statement

Snuke built an online judge to hold a programming contest.

When a program is submitted to the judge, the judge returns a verdict, which is a two-character string that appears in the string S as a contiguous substring. (The judge can return any two-character substring of S.)

Determine whether the judge can return the string AC as the verdict to a program.

Constraints

  • 2 \leq |S| \leq 5
  • S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the judge can return the string AC as a verdict to a program, print Yes; if it cannot, print No.


Sample Input 1

BACD

Sample Output 1

Yes

The string AC appears in BACD as a contiguous substring (the second and third characters).


Sample Input 2

ABCD

Sample Output 2

No

Although the string ABCD contains both A and C (the first and third characters), the string AC does not appear in ABCD as a contiguous substring.


Sample Input 3

CABD

Sample Output 3

No

Sample Input 4

ACACA

Sample Output 4

Yes

Sample Input 5

XX

Sample Output 5

No
B - Similar Arrays

Time Limit: 2 sec / Memory Limit: 256 MB

配点: 200

問題文

2 つの長さ N の整数列 x_1, x_2, ..., x_Ny_1, y_2, ..., y_N が「似ている」とは、 任意の i (1 \leq i \leq N) に対して |x_i - y_i| \leq 1 が成り立つことをいうものとします。

とくに、どの整数列もその数列自身と似ていると考えます。

整数 N および長さ N の整数列 A_1, A_2, ..., A_N が与えられます。

A と似ている整数列 b_1, b_2, ..., b_N であって、すべての項の積 b_1 b_2 ... b_N が偶数となるものはいくつあるか求めてください。

制約

  • 1 \leq N \leq 10
  • 1 \leq A_i \leq 100

入力

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

N
A_1 A_2 ... A_N

出力

条件を満たす整数列の個数を出力せよ。


入力例 1

2
2 3

出力例 1

7

条件を満たす整数列は以下の 7 個あります。

  • 1, 2
  • 1, 4
  • 2, 2
  • 2, 3
  • 2, 4
  • 3, 2
  • 3, 4

入力例 2

3
3 3 3

出力例 2

26

入力例 3

1
100

出力例 3

1

入力例 4

10
90 52 56 71 44 8 13 30 57 84

出力例 4

58921

Score : 200 points

Problem Statement

We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).

In particular, any integer sequence is similar to itself.

You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.

How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?

Constraints

  • 1 \leq N \leq 10
  • 1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of integer sequences that satisfy the condition.


Sample Input 1

2
2 3

Sample Output 1

7

There are seven integer sequences that satisfy the condition:

  • 1, 2
  • 1, 4
  • 2, 2
  • 2, 3
  • 2, 4
  • 3, 2
  • 3, 4

Sample Input 2

3
3 3 3

Sample Output 2

26

Sample Input 3

1
100

Sample Output 3

1

Sample Input 4

10
90 52 56 71 44 8 13 30 57 84

Sample Output 4

58921
C - Inserting 'x'

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

英小文字のみからなる文字列 s があります。 すぬけ君は、次の操作を繰り返し行うことができます。

  • s の任意の位置 (先頭および末尾を含む) をひとつ選び、英小文字 x をひとつ挿入する。

すぬけ君の目標は、s を回文にすることです。 すぬけ君の目標が達成可能か判定し、達成可能ならば必要な操作回数の最小値を求めてください。

注釈

回文とは、前後を反転しても変わらない文字列のことです。 例えば、a, aa, abba, abcba は回文ですが、ab, abab, abcda は回文ではありません。

制約

  • 1 \leq |s| \leq 10^5
  • s は英小文字のみからなる。

入力

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

s

出力

すぬけ君の目標が達成可能ならば、必要な操作回数の最小値を出力せよ。 達成不可能ならば、代わりに -1 を出力せよ。


入力例 1

xabxa

出力例 1

2

例えば、次のように操作を行えばよいです (新しく挿入された x は太字で表されています)。

xabxa → xaxbxa → xaxbxax


入力例 2

ab

出力例 2

-1

どのように操作を行っても、s を回文にできません。


入力例 3

a

出力例 3

0

s は最初から回文です。


入力例 4

oxxx

出力例 4

3

例えば、次のように操作を行えばよいです。

oxxx → xoxxx → xxoxxx → xxxoxxx

Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters. Snuke can perform the following operation repeatedly:

  • Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome. Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward. For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

  • 1 \leq |s| \leq 10^5
  • s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required. If it is not, print -1 instead.


Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax


Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.


Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.


Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx

D - Yet Another Palindrome Partitioning

Time Limit: 3 sec / Memory Limit: 512 MB

配点 : 700

問題文

英小文字のみからなる文字列 s があります。 すぬけ君は、s をいくつかの空でない部分文字列へ分割しようとしています。 分割後の部分文字列を、左から順に s_1, s_2, ..., s_N とします (このとき、s = s_1 + s_2 + ... + s_N です)。 すぬけ君は、次の条件が成り立つように s を分割しようとしています。

  • i (1 \leq i \leq N) について、s_i の文字を並べ替えて回文が得られる。

条件が成り立つように s を分割するとき、N の最小値を求めてください。

制約

  • 1 \leq |s| \leq 2 \times 10^5
  • s は英小文字のみからなる。

入力

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

s

出力

条件が成り立つように s を分割するとき、N の最小値を求めよ。


入力例 1

aabxyyzz

出力例 1

2

aabxyyzz = aab + xyyzz と分割すればよいです。 このとき、aab の文字を並べ替えて回文 aba が得られ、xyyzz の文字を並べ替えて回文 zyxyz が得られます。


入力例 2

byebye

出力例 2

1

byebye の文字を並べ替えて回文 byeeyb が得られます。


入力例 3

abcdefghijklmnopqrstuvwxyz

出力例 3

26

入力例 4

abcabcxabcx

出力例 4

3

abcabcxabcx = a + b + cabcxabcx と分割すればよいです。

Score : 700 points

Problem Statement

We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.) Snuke wants to satisfy the following condition:

  • For each i (1 \leq i \leq N), it is possible to permute the characters in s_i and obtain a palindrome.

Find the minimum possible value of N when the partition satisfies the condition.

Constraints

  • 1 \leq |s| \leq 2 \times 10^5
  • s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum possible value of N when the partition satisfies the condition.


Sample Input 1

aabxyyzz

Sample Output 1

2

The solution is to partition s as aabxyyzz = aab + xyyzz. Here, aab can be permuted to form a palindrome aba, and xyyzz can be permuted to form a palindrome zyxyz.


Sample Input 2

byebye

Sample Output 2

1

byebye can be permuted to form a palindrome byeeyb.


Sample Input 3

abcdefghijklmnopqrstuvwxyz

Sample Output 3

26

Sample Input 4

abcabcxabcx

Sample Output 4

3

The solution is to partition s as abcabcxabcx = a + b + cabcxabcx.

E - Cubes

Time Limit: 2 sec / Memory Limit: 256 MB

配点: 1600

問題文

一辺の長さが 1 の立方体の形をしたブロックを ABC 個積み上げて A \times B \times C の直方体を作りました。 さらに、この直方体を以下の条件を満たすように xyz 空間内に配置しました。

  • すべての i, j, k (0 \leq i < A, 0 \leq j < B, 0 \leq k < C) に対して、 点 (i, j, k) と点 (i + 1, j + 1, k + 1) を結ぶ線分を対角線として持ち、すべての辺が座標軸と平行なブロックが存在する。

i, j, k に対して、上のようなブロックをブロック (i, j, k) と呼ぶことにします。

2 つのブロック (i_1, j_1, k_1)(i_2, j_2, k_2) に対して、これらの距離を max(|i_1 - i_2|, |j_1 - j_2|, |k_1 - k_2|) と定義します。

いま、点 (0, 0, 0) と点 (A, B, C) を結ぶ線分に沿って、直方体に十分細い針金を通しました。 このとき、以下の条件を満たすブロック (x, y, z) の個数を 10^9 + 7 で割った余りを求めてください。

  • ある針金が通っている(境界で接する場合は除く)ブロック (x', y', z') が存在して、ブロック (x, y, z) とブロック (x', y', z') の距離は D 以下である。

制約

  • 1 \leq A < B < C \leq 10^{9}
  • A, B, C はどの 2 つも互いに素
  • 0 \leq D \leq 50,000

入力

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

A B C D

出力

条件を満たすブロックの個数を 10^9 + 7 で割った余りを出力せよ。


入力例 1

3 4 5 1

出力例 1

54

以下の図は、直方体を xy 平面に平行な平面で 5 段に分けて描いたものです。 ただし、i - 1 \leq z \leq i の範囲に含まれるブロックを i 段目と呼んでいます。

黒く塗ったブロックは針金が通っているブロックであり、これらはすべて条件を満たします。 また、黄色く塗ったブロックはそれら以外で条件を満たすブロックです。

b3a8ffcd8621b59d4657bfaa9c25a716.png

黒または黄色に塗られたブロックは全部で 54 個存在します。


入力例 2

1 2 3 0

出力例 2

4

針金が通っているブロックは 4 個あり、これらだけが条件を満たします。


入力例 3

3 5 7 100

出力例 3

105

すべてのブロックが条件を満たします。


入力例 4

3 123456781 1000000000 100

出力例 4

444124403

入力例 5

1234 12345 1234567 5

出力例 5

150673016

入力例 6

999999997 999999999 1000000000 50000

出力例 6

8402143

Score : 1600 points

Problem Statement

We constructed a rectangular parallelepiped of dimensions A \times B \times C built of ABC cubic blocks of side 1. Then, we placed the parallelepiped in xyz-space as follows:

  • For every triple i, j, k (0 \leq i < A, 0 \leq j < B, 0 \leq k < C), there exists a block that has a diagonal connecting the points (i, j, k) and (i + 1, j + 1, k + 1). All sides of the block are parallel to a coordinate axis.

For each triple i, j, k, we will call the above block as block (i, j, k).

For two blocks (i_1, j_1, k_1) and (i_2, j_2, k_2), we will define the distance between them as max(|i_1 - i_2|, |j_1 - j_2|, |k_1 - k_2|).

We have passed a wire with a negligible thickness through a segment connecting the points (0, 0, 0) and (A, B, C). How many blocks (x,y,z) satisfy the following condition? Find the count modulo 10^9 + 7.

  • There exists a block (x', y', z') such that the wire passes inside the block (x', y', z') (not just boundary) and the distance between the blocks (x, y, z) and (x', y', z') is at most D.

Constraints

  • 1 \leq A < B < C \leq 10^{9}
  • Any two of the three integers A, B and C are coprime.
  • 0 \leq D \leq 50,000

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of the blocks that satisfy the condition, modulo 10^9 + 7.


Sample Input 1

3 4 5 1

Sample Output 1

54

The figure below shows the parallelepiped, sliced into five layers by planes parallel to xy-plane. Here, the layer in the region i - 1 \leq z \leq i is called layer i.

The blocks painted black are penetrated by the wire, and satisfy the condition. The other blocks that satisfy the condition are painted yellow.

b09f2a541e463456c01d148eabdf36c3.png

There are 54 blocks that are painted black or yellow.


Sample Input 2

1 2 3 0

Sample Output 2

4

There are four blocks that are penetrated by the wire, and only these blocks satisfy the condition.


Sample Input 3

3 5 7 100

Sample Output 3

105

All blocks satisfy the condition.


Sample Input 4

3 123456781 1000000000 100

Sample Output 4

444124403

Sample Input 5

1234 12345 1234567 5

Sample Output 5

150673016

Sample Input 6

999999997 999999999 1000000000 50000

Sample Output 6

8402143
F - Three Gluttons

Time Limit: 2 sec / Memory Limit: 512 MB

配点 : 1800

問題文

3 人の男性 A, B, C が一緒に寿司を食べることになりました。 最初、N 種類の寿司が 1 個ずつあります。 寿司には 1 から N まで番号が振られています。 ただし、N3 の倍数とします。

3 人はそれぞれ寿司に対して好き嫌いの順位付けを持っています。 A の順位付けは、1 から N までの順列 (a_1,\ ...,\ a_N) で表されます。 各 i (1 \leq i \leq N) について、A が i 番目に好きな寿司は寿司 a_i です。 同様に、B および C の順位付けは、1 から N までの順列 (b_1,\ ...,\ b_N) および (c_1,\ ...,\ c_N) で表されます。

寿司がすべて無くなるか、喧嘩が起こる (後述) まで、3 人は次の行動を繰り返します。

  • A, B, C はそれぞれ、残りの寿司のうち最も好きな寿司を見つける。 これらをそれぞれ寿司 x, y, z とする。 x, y, z がすべて相異なるならば、A, B, C はそれぞれ寿司 x, y, z を食べる。 そうでないならば、3 人は殴り合いの喧嘩を始める。

A および B の順位付け (a_1,\ ...,\ a_N) および (b_1,\ ...,\ b_N) が与えられます。 C の順位付け (c_1,\ ...,\ c_N) のうち、3 人が喧嘩を始めることなく寿司をすべて食べ切れるようなものは、何通りあるでしょうか? 10^9+7 で割った余りを求めてください。

制約

  • 3 \leq N \leq 399
  • N3 の倍数である。
  • (a_1,\ ...,\ a_N) および (b_1,\ ...,\ b_N)1 から N までの順列である。

入力

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

N
a_1 ... a_N
b_1 ... b_N

出力

C の順位付け (c_1,\ ...,\ c_N) のうち、3 人が喧嘩を始めることなく寿司をすべて食べ切れるようなものは、何通りあるか? 10^9+7 で割った余りを出力せよ。


入力例 1

3
1 2 3
2 3 1

出力例 1

2

(c_1,\ c_2,\ c_3) = (3,\ 1,\ 2),\ (3,\ 2,\ 1)2 通りです。 どちらの場合も、A, B, C はそれぞれ寿司 1, 2, 3 を食べ、寿司がすべて無くなります。


入力例 2

3
1 2 3
1 2 3

出力例 2

0

(c_1,\ c_2,\ c_3) がどのような順列であっても、A と B はともに寿司 1 を食べようとするので、喧嘩が起こります。


入力例 3

6
1 2 3 4 5 6
2 1 4 3 6 5

出力例 3

80

例えば (c_1,\ c_2,\ c_3,\ c_4,\ c_5,\ c_6) = (5,\ 1,\ 2,\ 6,\ 3,\ 4) の場合、まず A, B, C はそれぞれ寿司 1, 2, 5 を食べ、次に A, B, C はそれぞれ寿司 3, 4, 6 を食べ、寿司がすべて無くなります。


入力例 4

6
1 2 3 4 5 6
6 5 4 3 2 1

出力例 4

160

入力例 5

9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6

出力例 5

33600

Score : 1800 points

Problem Statement

Three men, A, B and C, are eating sushi together. Initially, there are N pieces of sushi, numbered 1 through N. Here, N is a multiple of 3.

Each of the three has likes and dislikes in sushi. A's preference is represented by (a_1,\ ...,\ a_N), a permutation of integers from 1 to N. For each i (1 \leq i \leq N), A's i-th favorite sushi is Sushi a_i. Similarly, B's and C's preferences are represented by (b_1,\ ...,\ b_N) and (c_1,\ ...,\ c_N), permutations of integers from 1 to N.

The three repeats the following action until all pieces of sushi are consumed or a fight brakes out (described later):

  • Each of the three A, B and C finds his most favorite piece of sushi among the remaining pieces. Let these pieces be Sushi x, y and z, respectively. If x, y and z are all different, A, B and C eats Sushi x, y and z, respectively. Otherwise, a fight brakes out.

You are given A's and B's preferences, (a_1,\ ...,\ a_N) and (b_1,\ ...,\ b_N). How many preferences of C, (c_1,\ ...,\ c_N), leads to all the pieces of sushi being consumed without a fight? Find the count modulo 10^9+7.

Constraints

  • 3 \leq N \leq 399
  • N is a multiple of 3.
  • (a_1,\ ...,\ a_N) and (b_1,\ ...,\ b_N) are permutations of integers from 1 to N.

Input

Input is given from Standard Input in the following format:

N
a_1 ... a_N
b_1 ... b_N

Output

Print the number of the preferences of C that leads to all the pieces of sushi being consumed without a fight, modulo 10^9+7.


Sample Input 1

3
1 2 3
2 3 1

Sample Output 1

2

The answer is two, (c_1,\ c_2,\ c_3) = (3,\ 1,\ 2),\ (3,\ 2,\ 1). In both cases, A, B and C will eat Sushi 1, 2 and 3, respectively, and there will be no more sushi.


Sample Input 2

3
1 2 3
1 2 3

Sample Output 2

0

Regardless of what permutation (c_1,\ c_2,\ c_3) is, A and B will try to eat Sushi 1, resulting in a fight.


Sample Input 3

6
1 2 3 4 5 6
2 1 4 3 6 5

Sample Output 3

80

For example, if (c_1,\ c_2,\ c_3,\ c_4,\ c_5,\ c_6) = (5,\ 1,\ 2,\ 6,\ 3,\ 4), A, B and C will first eat Sushi 1, 2 and 5, respectively, then they will eat Sushi 3, 4 and 6, respectively, and there will be no more sushi.


Sample Input 4

6
1 2 3 4 5 6
6 5 4 3 2 1

Sample Output 4

160

Sample Input 5

9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6

Sample Output 5

33600