A - Pay to Win

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

あなたは 0 という数を持っており、これを N に変えようとしています。

あなたが持っている数は、以下の操作により、定まった枚数のコインを支払うことで変化させることができます。

  • A 枚のコインを支払い、持っている数を 2 倍する。
  • B 枚のコインを支払い、持っている数を 3 倍する。
  • C 枚のコインを支払い、持っている数を 5 倍する。
  • D 枚のコインを支払い、持っている数を 1 増やす、または 1 減らす。

これらの操作は、任意の順に何度でも行うことができます。

持っている数を N とするには、最小で何枚のコインが必要でしょうか。

T 個のテストケースについて答えてください。

制約

  • 1 \le T \le 10
  • 1 \le N \le 10^{18}
  • 1 \le A, B, C, D \le 10^9
  • N, A, B, C, D はすべて整数である。

入力

入力は標準入力から与えられる。入力の 1 行目は以下の通りである。

T

そして、続く T 行が T 個のテストケースを表す。 これらはそれぞれ以下の形式の行である。

N A B C D

出力

各テストケースに対し、答えを標準出力に出力せよ。テストケースごとに改行すること。


入力例 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

出力例 1

20
19
26
3821859835
23441258666

1 個目のテストケースで、必要な最小枚数である 20 枚のコインで目標を達成する方法は以下の通りです。

  • はじめ、持っている数 (以下、これを x とする) は 0 である。
  • 8 枚のコインを支払い、x1 増やす (x = 1)。
  • 1 枚のコインを支払い、x2 倍する (x = 2)。
  • 1 枚のコインを支払い、x2 倍する (x = 4)。
  • 2 枚のコインを支払い、x3 倍する (x = 12)。
  • 8 枚のコインを支払い、x1 減らす (x = 11)。

2 個目のテストケースで、必要な最小枚数である 19 枚のコインで目標を達成する方法は以下の通りです。

  • はじめ、x = 0 である。
  • 8 枚のコインを支払い、x1 増やす (x = 1)。
  • 1 枚のコインを支払い、x2 倍する (x = 2)。
  • 2 枚のコインを支払い、x5 倍する (x = 10)。
  • 8 枚のコインを支払い、x1 増やす (x = 11)。

Score : 400 points

Problem Statement

You start with the number 0 and you want to reach the number N.

You can change the number, paying a certain amount of coins, with the following operations:

  • Multiply the number by 2, paying A coins.
  • Multiply the number by 3, paying B coins.
  • Multiply the number by 5, paying C coins.
  • Increase or decrease the number by 1, paying D coins.

You can perform these operations in arbitrary order and an arbitrary number of times.

What is the minimum number of coins you need to reach N?

You have to solve T testcases.

Constraints

  • 1 \le T \le 10
  • 1 \le N \le 10^{18}
  • 1 \le A, B, C, D \le 10^9
  • All numbers N, A, B, C, D are integers.

Input

The input is given from Standard Input. The first line of the input is

T

Then, T lines follow describing the T testcases. Each of the T lines has the format

N A B C D

Output

For each testcase, print the answer on Standard Output followed by a newline.


Sample Input 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

Sample Output 1

20
19
26
3821859835
23441258666

For the first testcase, a sequence of moves that achieves the minimum cost of 20 is:

  • Initially x = 0.
  • Pay 8 to increase by 1 (x = 1).
  • Pay 1 to multiply by 2 (x = 2).
  • Pay 1 to multiply by 2 (x = 4).
  • Pay 2 to multiply by 3 (x = 12).
  • Pay 8 to decrease by 1 (x = 11).

For the second testcase, a sequence of moves that achieves the minimum cost of 19 is:

  • Initially x = 0.
  • Pay 8 to increase by 1 (x = 1).
  • Pay 1 to multiply by 2 (x = 2).
  • Pay 2 to multiply by 5 (x = 10).
  • Pay 8 to increase by 1 (x = 11).
B - Joker

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

映画「ジョーカー」が今夜放映されるとあり、あなたの行きつけの劇場はすでに満席です。この劇場には N 席の座席からなる列が N 列あり、これらの席が N\times N の正方形型に並んでいます。最前列の観客に左から 1, 2,\dots, N の番号を、前から 2 列目の観客に左から N+1, \dots, 2N の番号を付け、以降の観客にも同様に番号を付けます。最後列の観客の番号は、左から N^2-N+1,\dots, N^2 となります。

上映が終わると、観客は決まった順に劇場を出ます。i 番目に劇場を出るのは、番号 P_i の観客です。番号 P_{i+1} の観客は、番号 P_{i} の観客が劇場を出るまで待ってから移動します。劇場を出るには、席から席への移動を繰り返し、席からなる正方形型のエリアの外に出なければなりません (四辺のどこからでも出ることができます)。席から席への移動では、前後左右の 4 方向への移動が可能です。

番号 x の観客が、劇場を出る際に番号 y の別の観客が まだ座っている 席を通り抜けてしまうと、番号 x の観客は番号 y の観客に永遠に嫌われます。各観客は、自分を永遠に嫌う観客の数が最小となるように移動方法を選びます。

番号 x の観客が番号 y の観客に永遠に嫌われるような組 (x, y) の個数を求めてください。

制約

  • 2 \le N \le 500
  • P_1, P_2, \dots, P_{N^2}\{1, 2, \dots, N^2\} の順列である。

入力

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

N
P_1 P_2 \cdots P_{N^2}

出力

問題文で述べたような観客の組 (x, y)ans 組存在するとして、標準出力に以下の形式で出力せよ。

ans

入力例 1

3
1 3 7 9 5 4 8 6 2

出力例 1

1

上映が終わる前の劇場内の観客の配置は以下の通りです。

1 2 3
4 5 6
7 8 9

劇場を出る最初の 4 人 (番号 1, 3, 7, 9 の観客) は席を通り抜けることなく劇場を出られるので、誰にも嫌われません。

その後、番号 5 の観客は、劇場を出る際に番号 2, 4, 6, 8 の観客が座る席のうちいずれかを通り抜けなければなりません。よって、番号 5 の観客は上記の観客のうち少なくとも一人に嫌われます。

最後に残った 4 人 (順に番号 4, 8, 6, 2 の観客) は、人が座っている席を通り抜けずに劇場を出られます (そもそも、席を通り抜ける必要がありません)。


入力例 2

4
6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8

出力例 2

3

入力例 3

6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30

出力例 3

11

Score : 700 points

Problem Statement

Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2.

At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat. To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column). While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever.

Compute the number of pairs of viewers (x, y) such that y will hate x forever.

Constraints

  • 2 \le N \le 500
  • The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \{1, 2, \dots, N^2\}.

Input

The input is given from Standard Input in the format

N
P_1 P_2 \cdots P_{N^2}

Output

If ans is the number of pairs of viewers described in the statement, you should print on Standard Output

ans

Sample Input 1

3
1 3 7 9 5 4 8 6 2

Sample Output 1

1

Before the end of the movie, the viewers are arranged in the cinema as follows:

1 2 3
4 5 6
7 8 9

The first four viewers leaving the cinema (1, 3, 7, 9) can leave the cinema without going through any seat, so they will not be hated by anybody.

Then, viewer 5 must go through one of the seats where viewers 2, 4, 6, 8 are currently seated while leaving the cinema; hence he will be hated by at least one of those viewers.

Finally the remaining viewers can leave the cinema (in the order 4, 8, 6, 2) without going through any occupied seat (actually, they can leave the cinema without going through any seat at all).


Sample Input 2

4
6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8

Sample Output 2

3

Sample Input 3

6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30

Sample Output 3

11
C - Strange Dance

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

3^N 人の人が輪になって踊っています。 輪の中の人がいる位置に、0,1,\dots, 3^{N}-1 の番号を、適当な場所から始めて時計回りに付けます。はじめ、これらの位置それぞれに一人の人が立っています。

これから二種類の曲、サルサとルンバが流れ、人々はそれに合わせて踊ります。

  • サルサが流れたら、位置 i にいる人は以下で述べるような位置 j に移動します。j は、i3 進法で表記し、1 という桁をそれぞれ 2 に、2 という桁をそれぞれ 1 に置き換えて得られる数です (例えば、位置 46 の人は位置 65 に移動します)。
  • ルンバが流れたら、位置 i にいる人は位置 i+1 に移動します。ここで、位置 3^N は位置 0 とみなします。

文字列 T=T_1T_2\cdots T_{|T|} が与えられます。これは、T_i=S なら i 番目に流れる曲がサルサであり、T_i=R ならルンバであることを表します。 はじめ位置 i に立っていた人が、すべての曲が流れたあとに位置 P_i に立っているとします。 列 P_0,P_1,\dots, P_{3^N-1} を求めてください。

制約

  • 1 \le N \le 12
  • 1 \le |T| \le 200,000
  • TSR からなる。

入力

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

N
T

出力

以下の形式で標準出力に出力せよ。

P_0 P_1 \cdots P_{3^N-1}

入力例 1

1
SRS

出力例 1

2 0 1 

最初の曲が流れる前に位置 i に立っていた人を人 i とします。

  1. サルサが一度目に流れたあと、人 0, 1, 2 はそれぞれ位置 0, 2, 1 に立っています。
  2. ルンバが流れたあと、人 0, 1, 2 はそれぞれ位置 1, 0, 2 に立っています。
  3. サルサが二度目に流れたあと、人 0, 1, 2 はそれぞれ位置 2, 0, 1 に立っています。

入力例 2

2
RRSRSSSSR

出力例 2

3 8 1 0 5 7 6 2 4 

入力例 3

3
SRSRRSRRRSRRRR

出力例 3

23 9 22 8 3 7 20 24 19 5 18 4 17 12 16 2 6 1 14 0 13 26 21 25 11 15 10 

Score : 1000 points

Problem Statement

There are 3^N people dancing in circle. We denote with 0,1,\dots, 3^{N}-1 the positions in the circle, starting from an arbitrary position and going around clockwise. Initially each position in the circle is occupied by one person.

The people are going to dance on two kinds of songs: salsa and rumba.

  • When a salsa is played, the person in position i goes to position j, where j is the number obtained replacing all digits 1 with 2 and all digits 2 with 1 when reading i in base 3 (e.g., the person in position 46 goes to position 65).
  • When a rumba is played, the person in position i moves to position i+1 (with the identification 3^N = 0).

You are given a string T=T_1T_2\cdots T_{|T|} such that T_i=S if the i-th song is a salsa and T_i=R if it is a rumba. After all the songs have been played, the person that initially was in position i is in position P_i. Compute the array P_0,P_1,\dots, P_{3^N-1}.

Constraints

  • 1 \le N \le 12
  • 1 \le |T| \le 200,000
  • T contains only the characters S and R.

Input

Input is given from Standard Input in the following format:

N
T

Output

You should print on Standard Output:

P_0 P_1 \cdots P_{3^N-1}

Sample Input 1

1
SRS

Sample Output 1

2 0 1 

Before any song is played, the positions are: 0, 1, 2.

When we say "person i", we mean "the person that was initially in position i".

  1. After the first salsa, the positions are: 0, 2, 1.
  2. After the rumba, the positions are: 1, 0, 2 (so, person 0 is in position 1, person 1 is in position 0 and person 2 is in position 2).
  3. After the second salsa, the positions are 2, 0, 1 (so, person 0 is in position 2, person 1 is in position 0 and person 2 is in position 1).

Sample Input 2

2
RRSRSSSSR

Sample Output 2

3 8 1 0 5 7 6 2 4 

Sample Input 3

3
SRSRRSRRRSRRRR

Sample Output 3

23 9 22 8 3 7 20 24 19 5 18 4 17 12 16 2 6 1 14 0 13 26 21 25 11 15 10 
D - Guess the Password

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

これは対話式の問題です。

あなたの課題は、秘密のパスワード S を当てることです。 パスワードは長さ L 以下の空でない文字列であり、パスワードの各文字は英小文字 (a, b, ..., z)、英大文字 (A, B, ..., Z)、数字 (0, 1, ..., 9) のいずれかです。

秘密のパスワード S を当てるために、クエリを送ることができます。 クエリは、上述のパスワードの要件を満たす文字列 T からなり、送られたクエリに対しては ST の編集距離 (考慮する操作は文字の削除、挿入、置換) が回答されます。 送ることができるクエリの数は Q 個までです。

注記: 編集距離 (考慮する操作は文字の削除、挿入、置換) の定義については、Wikipedia 内のこちらのページ を参照してください。

制約

  • L = 128
  • Q = 850
  • 秘密のパスワード S は、プログラムとジャッジの対話の開始前に選ばれる。

入出力

クエリを送るには、標準出力に以下のように出力せよ (末尾に改行を入れること)。

? T

ここで、文字列 T はパスワードの要件を満たさなければならない。

クエリへの回答 ans は、標準入力から以下の形式で与えられる。

ans

秘密のパスワード S を特定したら、標準出力に以下のように出力せよ (末尾に改行を入れること)。

! S

そして、すぐにプログラムを終了させよ。

判定

  • 出力のたびに標準出力を flush せよ。 これが守られない場合、ジャッジ結果が TLE となる可能性がある。
  • 秘密のパスワード (とあなたが考えるもの) を出力したら、直ちにプログラムを終了させよ。これが守られない場合のジャッジ結果は未定義である。
  • 不正な形式のクエリが送られた場合 (例: 送られた文字列がパスワードの要件を満たさない、出力の先頭に ? がない)、プログラムが異常終了した場合、または Q 回を超えてクエリが送られた場合のジャッジ結果は未定義である (WA とは限らない)。

入出力例

以下の例において、秘密のパスワードは Atcod3rIsGreat です。

Input Output
\texttt{? AtcoderIsBad}
5
\texttt{? AtcoderIsGreat}
1
\texttt{! Atcod3rIsGreat}

Score : 1100 points

Problem Statement

This is an interactive task.

You have to guess a secret password S. A password is a nonempty string whose length is at most L. The characters of a password can be lowercase and uppercase english letters (a, b, ... , z, A, B, ... , Z) and digits (0, 1, ... , 9).

In order to guess the secret password S you can ask queries. A query is made of a valid password T, the answer to such query is the edit distance between S and T (the operations considered for the edit distance are: removal, insertion, substitution). You can ask at most Q queries.

Note: For a definition of edit distance (with operations: removal, insertion, substitution), see this wikipedia page.

Constraints

  • L = 128
  • Q = 850
  • The secret password S is chosen before the beginning of the interaction.

Interaction

To ask a query, print on Standard Output (with a newline at the end)

? T

The string T must be a valid password.

The answer ans to each query shall be read from Standard Input in the form:

ans

As soon as you have determined the hidden password S, you should print on Standard Output (with a newline at the end)

! S

and terminate your program.

Judgement

  • After each output, you must flush Standard Output. Otherwise you may get TLE.
  • After you print (your guess of) the hidden password, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.
  • If the password you have determined is wrong, you will get WA.
  • If you ask a malformed query (for example because the string is not a valid password, or you forget to put ?) or if your program crashes or if you ask more than Q queries, the behavior of the judge is undefined (it does not necessarily give WA).

Samples

In the only sample testcase of this problem the hidden password is Atcod3rIsGreat.

The following one is an example of interaction if S=Atcod3rIsGreat.

Input Output
\texttt{? AtcoderIsBad}
5
\texttt{? AtcoderIsGreat}
1
\texttt{! Atcod3rIsGreat}
E - Random Pawn

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1300

問題文

あなたの目的は、これからプレイするゲームでの利得の期待値を最大化することです。 ゲームを開始すると、ポーン (チェスの駒) が \{1,2,\dots, N\} から等確率に選ばれた位置 p に置かれます。これらの N 個の位置 1, 2, \dots, N は円周上に時計回りに並び、位置 1 の両隣は位置 N, 2 です。

ゲームはターン制で進行します。各ターンでは、ゲームを終えて A_p ドル (p はその時点でのポーンの位置) を得るか、B_p ドルを支払ってゲームを続けるかを選べます。 ゲームを続ける場合、ポーンは自身と隣接する 2 つの位置 p-1, p+1 のいずれかに等確率で移されます (位置 0 は位置 N と、位置 N+1 は位置 1 とみなします)。

最適な戦略の期待利得は何ドルでしょうか。

注記: 「最適な戦略の期待利得」は、ゲームが 有限の ターン数で終わるような戦略における期待利得の上限と定義されます。

制約

  • 2 \le N \le 200,000
  • 0 \le A_p \le 10^{12} (p = 1,\ldots, N)
  • 0 \le B_p \le 100 (p = 1, \ldots, N)
  • 入力中のすべての値は整数である。

入力

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

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

出力

最適な戦略の期待利得を 1 個の実数として出力せよ。

出力された値は、絶対誤差または相対誤差が 10^{-10} を超えなければ正解とみなされる。


入力例 1

5
4 2 6 3 5
1 1 1 1 1

出力例 1

4.700000000000

入力例 2

4
100 0 100 0
0 100 0 100

出力例 2

50.000000000000

入力例 3

14
4839 5400 6231 5800 6001 5200 6350 7133 7986 8012 7537 7013 6477 5912
34 54 61 32 52 61 21 43 65 12 45 21 1 4

出力例 3

7047.142857142857

入力例 4

10
470606482521 533212137322 116718867454 746976621474 457112271419 815899162072 641324977314 88281100571 9231169966 455007126951
26 83 30 59 100 88 84 91 54 61

出力例 4

815899161079.400024414062

Score : 1300 points

Problem Statement

You are playing a game and your goal is to maximize your expected gain. At the beginning of the game, a pawn is put, uniformly at random, at a position p\in\{1,2,\dots, N\}. The N positions are arranged on a circle (so that 1 is between N and 2).

The game consists of turns. At each turn you can either end the game, and get A_p dollars (where p is the current position of the pawn), or pay B_p dollar to keep playing. If you decide to keep playing, the pawn is randomly moved to one of the two adjacent positions p-1, p+1 (with the identifications 0 = N and N+1=1).

What is the expected gain of an optimal strategy?

Note: The "expected gain of an optimal strategy" shall be defined as the supremum of the expected gain among all strategies such that the game ends in a finite number of turns.

Constraints

  • 2 \le N \le 200,000
  • 0 \le A_p \le 10^{12} for any p = 1,\ldots, N
  • 0 \le B_p \le 100 for any p = 1, \ldots, N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N

Output

Print a single real number, the expected gain of an optimal strategy. Your answer will be considered correct if its relative or absolute error does not exceed 10^{-10}.


Sample Input 1

5
4 2 6 3 5
1 1 1 1 1

Sample Output 1

4.700000000000

Sample Input 2

4
100 0 100 0
0 100 0 100

Sample Output 2

50.000000000000

Sample Input 3

14
4839 5400 6231 5800 6001 5200 6350 7133 7986 8012 7537 7013 6477 5912
34 54 61 32 52 61 21 43 65 12 45 21 1 4

Sample Output 3

7047.142857142857

Sample Input 4

10
470606482521 533212137322 116718867454 746976621474 457112271419 815899162072 641324977314 88281100571 9231169966 455007126951
26 83 30 59 100 88 84 91 54 61

Sample Output 4

815899161079.400024414062
F - Name-Preserving Clubs

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 2400

問題文

N 人の (名前の異なる) 人がおり、K 個のクラブがあります。あなたは、各クラブの会員をすべて知っています (形式的に言えば、K 個の無順序リストを持っています)。それぞれの人は複数のクラブの会員である可能性も、どのクラブの会員でもない可能性もあります。複数のクラブの会員の集合が全く同じである可能性もあります。 ここで、K の値は、次の条件を満たす会員構成が少なくとも一通り存在するような最小の値となっています。

  • N 人全員が (全員の名前が異なるという条件を守りながら) 改名して、K 個のクラブの改名後の会員リストがあなたに与えられた (ただし、どの会員リストがどのクラブのものかは知らされない) とする。このとき、あなたは確実に、改名後の N 個の名前すべてについてその名前の人の元の名前を当てることができる。

このような K 個のクラブの会員構成が何種類存在するか求めてください。ただし、1000 種類を超える場合はその旨を報告してください。 ここで、2 種類の会員構成は、一方において N 人の人が適切に改名することでもう一方が得られるなら同一とみなします。

厳密な問題文: クラブとは、\{1,\dots, N\} の部分集合 (空である可能性もある) である。(N 人の人に番号 1, 2, \ldots, N が付けられているとすると、この集合の要素がクラブの会員に対応する。) 会員構成とは、K 個のクラブ (異なるとは限らない) からなる多重集合である。 \{1,\dots, N\} の順列 \sigma(1), \dots, \sigma(N) と会員構成 L=\{C_1, C_2, \dots, C_K\} に対し、\sigma(L) で会員構成 \{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\} を表す (C がクラブであるとき、\sigma(C)=\{\sigma(x):\, x\in C\} である)。 会員構成 L は、任意の異なる順列 \sigma\not=\tau に対して \sigma(L)\not=\tau(L) を満たすとき、name-preserving であるという。

name-preserving であってクラブ数 K が最小であるような会員構成の個数を求めよ。ある順列 \sigma が存在して L_2=\sigma(L_1) が成立するような 2 つの会員構成 L_1, L_2 は同一とみなす。このような会員構成の個数が 1000 を超える場合は -1 を出力せよ。

制約

  • 2 \le N \le 2\cdot10^{18}

入力

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

N

出力

問題文で述べたような会員構成が ans 種類存在するとして、以下の形式で標準出力に出力せよ。

ans

ただし、このような会員構成の数が 1000 種類より多い場合は -1 を出力せよ。


入力例 1

2

出力例 1

1

K の値は 1 で、name-preserving であるような唯一の会員構成は \{\{1\}\} です (\{\{2\}\} はこれと同一とみなされます)。


入力例 2

3

出力例 2

1

K の値は 2 で、name-preserving であるような唯一の会員構成は \{\{1\}, \{1, 2\}\} です (置換により一致する会員構成を同一視すると、これのみです)。


入力例 3

4

出力例 3

7

K の値は 3 で、name-preserving であるような 3 クラブの会員構成は以下の通りです。

  • \{\{1\}, \{2\}, \{1, 3\}\}
  • \{\{1\}, \{1, 2\}, \{2, 3\}\}
  • \{\{1, 2\}, \{1, 2\}, \{1, 3\}\}
  • \{\{1\}, \{1, 2\}, \{1, 2, 3\}\}
  • \{\{1\}, \{2, 3\}, \{1, 2, 4\}\}
  • \{\{1, 2\}, \{1, 3\}, \{1, 2, 4\}
  • \{\{1, 2\}, \{1, 2, 3\}, \{2, 3, 4\}\}

入力例 4

13

出力例 4

6

入力例 5

26

出力例 5

-1

入力例 6

123456789123456789

出力例 6

-1

Score : 2400 points

Problem Statement

There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones.

Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal.

Formal statement: A club is a (possibly empty) subset of \{1,\dots, N\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \{1,\dots, N\} and a configuration of clubs L=\{C_1, C_2, \dots, C_K\}, we denote with \sigma(L) the configuration of clubs \{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\} (if C is a club, \sigma(C)=\{\sigma(x):\, x\in C\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L).

You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1.

Constraints

  • 2 \le N \le 2\cdot10^{18}

Input

The input is given from Standard Input in the format

N

Output

If ans is the number of configurations with the properties described in the statement, you should print on Standard Output

ans

If there are more than 1000 such configurations, print -1.


Sample Input 1

2

Sample Output 1

1

The value of K is 1 and the unique name-preserving configuration of clubs is \{\{1\}\} (the configuration \{\{2\}\} is considered the same).


Sample Input 2

3

Sample Output 2

1

The value of K is 2 and the unique (up to permutation) name-preserving configuration of clubs is \{\{1\}, \{1, 2\}\}.


Sample Input 3

4

Sample Output 3

7

The value of K is 3 and the name-preserving configurations of clubs with 3 clubs are:

  • \{\{1\}, \{2\}, \{1, 3\}\}
  • \{\{1\}, \{1, 2\}, \{2, 3\}\}
  • \{\{1, 2\}, \{1, 2\}, \{1, 3\}\}
  • \{\{1\}, \{1, 2\}, \{1, 2, 3\}\}
  • \{\{1\}, \{2, 3\}, \{1, 2, 4\}\}
  • \{\{1, 2\}, \{1, 3\}, \{1, 2, 4\}
  • \{\{1, 2\}, \{1, 2, 3\}, \{2, 3, 4\}\}

Sample Input 4

13

Sample Output 4

6

Sample Input 5

26

Sample Output 5

-1

Sample Input 6

123456789123456789

Sample Output 6

-1