A - Triangular Number

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

正整数 N が与えられます。
1 以上 N 以下の整数をすべて足し合わせた値 1+2+\cdots+N を出力してください。

制約

  • 1 \leq N \leq 100
  • N は整数

入力

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

N

出力

1 以上 N 以下の整数をすべて足し合わせた値を出力せよ。


入力例 1

5

出力例 1

15

1+2+3+4+5=15 のため、15 を出力します。


入力例 2

1

出力例 2

1

入力例 3

29

出力例 3

435

Score : 100 points

Problem Statement

You are given a positive integer N.
Output the sum of all integers from 1 to N, that is, 1+2+\cdots+N.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.

Input

The input is given from Standard Input in the following format:

N

Output

Output the sum of all integers from 1 to N.


Sample Input 1

5

Sample Output 1

15

Since 1+2+3+4+5=15, output 15.


Sample Input 2

1

Sample Output 2

1

Sample Input 3

29

Sample Output 3

435
B - Daily Cookie

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

N 個の箱が横一列に並んでおり、そのうちのいくつかの箱にはクッキーが入っています。

各箱の状態は長さ N の文字列 S によって表されます。 具体的には、左から i\ (1\leq i\leq N) 番目の箱は、Si 文字目が @ のときクッキーが 1 枚入っており、. のとき空き箱です。

高橋君は今からの D 日間、一日一回ずつ、これらの箱のいずれかに入ったクッキーを 1 枚選んで食べます。

N 個の箱のうち、D 日間が経過した後に空き箱であるものはいくつあるか求めてください。 (この値は高橋君が各日でどのクッキーを選ぶかによらないことが証明できます。)

なお、S には @D 個以上含まれることが保証されます。

制約

  • 1\leq D \leq N \leq 100
  • N,D は整数
  • S@. からなる長さ N の文字列
  • S には @D 個以上含まれる

入力

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

N D
S

出力

N 個の箱のうち、D 日間が経過した後に空き箱であるものの個数を出力せよ。


入力例 1

5 2
.@@.@

出力例 1

4

例えば、高橋君は以下のように行動する可能性があります。

  • 1 日目:左から 2,3,5 番目の箱にクッキーが入っている。左から 2 番目の箱に入っているクッキーを選んで食べる。
  • 2 日目:左から 3,5 番目の箱にクッキーが入っている。左から 5 番目の箱に入っているクッキーを選んで食べる。
  • 2 日間が経過した後、左から 3 番目の箱にのみクッキーが入っている。よって、5 個の箱のうち 4 箱が空き箱である。

高橋君が各日で選ぶクッキーはこの例とは異なる可能性もありますが、いずれにせよ 2 日間が経過した後には 4 箱が空き箱です。 よって答えは 4 です。


入力例 2

3 3
@@@

出力例 2

3

入力例 3

10 4
@@@.@@.@@.

出力例 3

7

Score: 100 points

Problem Statement

There are N boxes arranged in a row, and some of these boxes contain cookies.

The state of these boxes is represented by a string S of length N. Specifically, the i-th box (1\leq i \leq N) from the left contains one cookie if the i-th character of S is @, and is empty if it is ..

Over the next D days, Takahashi will choose and eat one cookie per day from among the cookies in these boxes.

Determine how many of the N boxes will be empty after D days have passed. (It can be proved that this value does not depend on which cookies Takahashi chooses each day.)

It is guaranteed that S contains at least D occurrences of @.

Constraints

  • 1 \leq D \leq N \leq 100
  • N and D are integers.
  • S is a string of length N consisting of @ and ..
  • S contains at least D occurrences of @.

Input

The input is given from Standard Input in the following format:

N D
S

Output

Print the number of boxes that will be empty after D days have passed among the N boxes.


Sample Input 1

5 2
.@@.@

Sample Output 1

4

For example, Takahashi might act as follows:

  • Day 1: There are cookies in the 2nd, 3rd, and 5th boxes from the left. He chooses the cookie in the 2nd box to eat.
  • Day 2: There are cookies in the 3rd and 5th boxes. He chooses the cookie in the 5th box to eat.
  • After two days have passed, only the 3rd box from the left contains a cookie. Therefore, four out of the five boxes are empty.

Even though Takahashi might choose differently on each day than in this example, there will still be four empty boxes after two days. Therefore, the answer is 4.


Sample Input 2

3 3
@@@

Sample Output 2

3

Sample Input 3

10 4
@@@.@@.@@.

Sample Output 3

7
C - Let's Get a Perfect Score

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1 から N までの番号がついた N 人の参加者が、1 から M までの番号がついた M 問からなるコンテストに参加します。

1 以上 N 以下の整数 i1 以上 M 以下の整数 j について、S_ij 番目の文字が o のとき参加者 i は問題 j を解くことが可能で、S_ij 番目の文字が x のとき参加者 i は問題 j を解くことが不可能です。

このコンテストは、二人の参加者でペアを組んで参加します。二人が協力することで M 問全てを解くことが可能であるようなペアの個数を答えてください。

より厳密には、1\leq x < y\leq N を満たす整数の組 (x,y) であって、 1 以上 M 以下の任意の整数 j について、参加者 x か参加者 y の少なくとも一方は問題 j を解くことが可能であるという条件を満たすものの個数を答えてください。

制約

  • N2 以上 30 以下の整数
  • M1 以上 30 以下の整数
  • S_io, x からなる長さ M の文字列

入力

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

N M
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5 5
ooooo
oooxx
xxooo
oxoxo
xxxxx

出力例 1

5

参加者 12 のペア、参加者 13 のペア、参加者 14 のペア、参加者 15 のペア、参加者 23 のペアの 5 個のペアが条件を満たします。

例えば参加者 24 のペアは、問題 4 が解けないので条件を満たしません。


入力例 2

3 2
ox
xo
xx

出力例 2

1

入力例 3

2 4
xxxx
oxox

出力例 3

0

Score : 200 points

Problem Statement

N participants, numbered 1 to N, will participate in a contest with M problems, numbered 1 to M.

For an integer i between 1 and N and an integer j between 1 and M, participant i can solve problem j if the j-th character of S_i is o, and cannot solve it if that character is x.

The participants must be in pairs. Print the number of ways to form a pair of participants who can collectively solve all the M problems.

More formally, print the number of pairs (x,y) of integers satisfying 1\leq x < y\leq N such that for any integer j between 1 and M, at least one of participant x and participant y can solve problem j.

Constraints

  • N is an integer between 2 and 30, inclusive.
  • M is an integer between 1 and 30, inclusive.
  • S_i is a string of length M consisting of o and x.

Input

The input is given from Standard Input in the following format:

N M
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5 5
ooooo
oooxx
xxooo
oxoxo
xxxxx

Sample Output 1

5

The following five pairs satisfy the condition: participants 1 and 2, participants 1 and 3, participants 1 and 4, participants 1 and 5, and participants 2 and 3.

On the other hand, the pair of participants 2 and 4, for instance, does not satisfy the condition because they cannot solve problem 4.


Sample Input 2

3 2
ox
xo
xx

Sample Output 2

1

Sample Input 3

2 4
xxxx
oxox

Sample Output 3

0
D - Unvarnished Report

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

キーエンスでは良いことも悪いこともありのままに報告するという文化があります。
そこで、報告内容が元の文章のありのままであるかを確認したいです。

英小文字のみからなる文字列 S, T が与えられます。
ST が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力してください。
ただし、S,T の一方にのみ i 文字目が存在するときも、i 文字目は異なっているとみなすものとします。

より厳密には、ST が等しくないならば次の条件のうちいずれかをみたす最小の整数 i を出力してください。

  • 1\leq i\leq |S| かつ 1\leq i\leq |T| かつ S_i\neq T_i
  • |S|< i\leq |T|
  • |T|< i\leq |S|

ただし、|S|,|T| でそれぞれ S,T の長さを、S_i,T_i でそれぞれ S,Ti 文字目を表します。

制約

  • S,T は英小文字のみからなる長さ 1 以上 100 以下の文字列

入力

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

S
T

出力

ST が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力せよ。


入力例 1

abcde
abedc

出力例 1

3

S=abcde, T=abedc です。
ST1,2 文字目は等しく、3 文字目が異なるため、 3 を出力します。


入力例 2

abcde
abcdefg

出力例 2

6

S=abcde, T=abcdefg です。
ST5 文字目まで等しく、T にのみ 6 文字目が存在するため、6 を出力します。


入力例 3

keyence
keyence

出力例 3

0

ST は等しいため、 0 を出力します。

Score : 200 points

Problem Statement

KEYENCE has a culture of reporting things as they are, whether good or bad.
So we want to check whether the reported content is exactly the same as the original text.

You are given two strings S and T, consisting of lowercase English letters.
If S and T are equal, print 0; otherwise, print the position of the first character where they differ.
Here, if the i-th character exists in only one of S and T, consider that the i-th characters are different.

More precisely, if S and T are not equal, print the smallest integer i satisfying one of the following conditions:

  • 1\leq i\leq |S|, 1\leq i\leq |T|, and S_i\neq T_i.
  • |S| < i \leq |T|.
  • |T| < i \leq |S|.

Here, |S| and |T| denote the lengths of S and T, respectively, and S_i and T_i denote the i-th characters of S and T, respectively.

Constraints

  • S and T are strings of length between 1 and 100, inclusive, consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S
T

Output

If S and T are equal, print 0; otherwise, print the position of the first character where they differ.


Sample Input 1

abcde
abedc

Sample Output 1

3

We have S= abcde and T= abedc.
S and T have the same first and second characters, but differ at the third character, so print 3.


Sample Input 2

abcde
abcdefg

Sample Output 2

6

We have S= abcde and T= abcdefg.
S and T are equal up to the fifth character, but only T has a sixth character, so print 6.


Sample Input 3

keyence
keyence

Sample Output 3

0

S and T are equal, so print 0.

E - Palindromic in Both Bases

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

414 の十進法での表記は 414 であり、これは回文です。 また、414 の八進法での表記は 636 であり、これも回文です。 これを踏まえて、以下の問題を解いてください。

正の整数 A, N が与えられます。 1 以上 N 以下の整数のうち、十進法での表記も A 進法での表記も回文であるようなものの総和を求めてください。

なお、この問題の制約下で答えは 2^{63} 未満であることが証明できます。

制約

  • 2 \leq A \leq 9
  • 1 \leq N \le 10^{12}
  • 入力される数値は全て整数である。

入力

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

A
N

出力

答えを 1 行で出力せよ。


入力例 1

8
1000

出力例 1

2155

条件を満たす整数は 1,2,3,4,5,6,7,9,121,292,333,373,414,58514 個であり、その総和は 2155 です。


入力例 2

8
999999999999

出力例 2

914703021014

入力例 3

6
999999999999

出力例 3

283958331810

Score : 350 points

Problem Statement

The decimal representation of 414 is 414, which is a palindrome. Also, the octal representation of 414 is 636, which is also a palindrome. Based on this, solve the following problem.

You are given positive integers A and N. Find the sum of all integers between 1 and N, inclusive, whose decimal representation and base-A representation are both palindromes.

Under the constraints of this problem, it can be proved that the answer is less than 2^{63}.

Constraints

  • 2 \leq A \leq 9
  • 1 \leq N \le 10^{12}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

A
N

Output

Output the answer in one line.


Sample Input 1

8
1000

Sample Output 1

2155

The integers satisfying the condition are 1,2,3,4,5,6,7,9,121,292,333,373,414,585 (14 integers), and their sum is 2155.


Sample Input 2

8
999999999999

Sample Output 2

914703021014

Sample Input 3

6
999999999999

Sample Output 3

283958331810
F - Almost Equal

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

英小文字からなる長さ M の文字列 NS_1,S_2,\dots,S_N が与えられます。ここで、S_i は互いに異なります。

これらを並び替えた文字列の列 T_1,T_2,\dots,T_N であって、以下の条件を満たすものが存在するか判定してください。

  • 1 \le i \le N-1 を満たす全ての整数 i に対して、T_i1 文字だけ別の英小文字に変えて T_{i+1} にすることが出来る。

制約

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i は英小文字からなる長さ M の文字列である。(1 \le i \le N)
  • S_i は互いに異なる。

入力

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

N M
S_1
S_2
\vdots
S_N

出力

問題文の条件を満たす列が存在するならば Yes を、そうでないならば No を出力せよ。


入力例 1

4 4
bbed
abcd
abed
fbed

出力例 1

Yes

abcd , abed , bbed , fbed の順に並び替えると条件を満たします。


入力例 2

2 5
abcde
abced

出力例 2

No

どのように並び替えても条件を満たすことは出来ません。


入力例 3

8 4
fast
face
cast
race
fact
rice
nice
case

出力例 3

Yes

Score : 250 points

Problem Statement

You are given N strings S_1,S_2,\dots,S_N, each of length M, consisting of lowercase English letter. Here, S_i are pairwise distinct.

Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that:

  • for all integers i such that 1 \le i \le N-1, one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1}.

Constraints

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i is a string of length M consisting of lowercase English letters. (1 \le i \le N)
  • S_i are pairwise distinct.

Input

The input is given from Standard Input in the following format:

N M
S_1
S_2
\vdots
S_N

Output

Print Yes if one can obtain a conforming sequence; print No otherwise.


Sample Input 1

4 4
bbed
abcd
abed
fbed

Sample Output 1

Yes

One can rearrange them in this order: abcd, abed, bbed, fbed. This sequence satisfies the condition.


Sample Input 2

2 5
abcde
abced

Sample Output 2

No

No matter how the strings are rearranged, the condition is never satisfied.


Sample Input 3

8 4
fast
face
cast
race
fact
rice
nice
case

Sample Output 3

Yes
G - Reindeer and Sleigh

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N 台のソリがあり、各ソリには 1,2,\ldots, N の番号がつけられています。

ソリ i を引くために必要なトナカイは R_i 匹です。

また、各トナカイが引けるソリは高々 1 台です。より厳密には、m 台のソリ i_1, i_2, \ldots, i_m を引くために必要なトナカイは \sum_{k=1}^{m} R_{i_k} 匹です。

以下の形式のクエリが Q 個与えられるので、答えを求めてください。

  • 整数 X が与えられる。トナカイが X 匹いるときに最大で何台のソリを引けるか求めよ。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq R_i \leq 10^9
  • 1 \leq X \leq 2 \times 10^{14}
  • 入力される数値はすべて整数

入力

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

N Q
R_1 R_2 \ldots R_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは次の形式で与えられる。

X

出力

Q 行出力せよ。

i 行目には i 個目のクエリに対する答えを出力せよ。


入力例 1

4 3
5 3 11 8
16
7
1000

出力例 1

3
1
4

トナカイが 16 匹いるとき、ソリ 1,2,4 を引くことができます。

16 匹のトナカイで 4 台のソリを引くことはできないので、クエリ 1 の答えは 3 となります。


入力例 2

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

出力例 2

1
1
2
2
2
3

入力例 3

2 2
1000000000 1000000000
200000000000000
1

出力例 3

2
0

Score : 400 points

Problem Statement

There are N sleighs numbered 1,2,\ldots, N.

R_i reindeer are required to pull sleigh i.

Additionally, each reindeer can pull at most one sleigh. More precisely, \sum_{k=1}^{m} R_{i_k} reindeer are required to pull m sleighs i_1, i_2, \ldots, i_m.

Find the answer to Q queries of the following form:

  • You are given an integer X. Determine the maximum number of sleighs that can be pulled when there are X reindeer.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq R_i \leq 10^9
  • 1 \leq X \leq 2 \times 10^{14}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
R_1 R_2 \ldots R_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

X

Output

Print Q lines.

The i-th line should contain the answer to the i-th query.


Sample Input 1

4 3
5 3 11 8
16
7
1000

Sample Output 1

3
1
4

When there are 16 reindeer, sleighs 1,2,4 can be pulled.

It is impossible to pull four sleighs with 16 reindeer, so the answer to query 1 is 3.


Sample Input 2

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

Sample Output 2

1
1
2
2
2
3

Sample Input 3

2 2
1000000000 1000000000
200000000000000
1

Sample Output 3

2
0
H - Duplicate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

1 から 9 までの数字からなる文字列 S に対して、 f(S) を次の手順によって得られる文字列 T とします。(S_iSi 番目の文字を意味します)

  • 文字列 T がある。はじめ、T は空文字列である。
  • i=1, 2, \dots, |S| - 1 の順に次の操作を行う。
    • S_{i+1} を整数として解釈したときの値を n とする。T の末尾に S_in 個追加する。

例えば S = 313 のとき、以下の手順によって f(S) = 3111 に決まります。

  • はじめ T は空文字列である。
  • i=1 のとき n = 1 である。T31 個追加する。T3 になる。
  • i=2 のとき n = 3 である。T13 個追加する。T3111 になる。
  • 操作を終了する。T として 3111 を得る。

1 から 9 までの数字からなる長さ N の文字列 S が与えられます。
あなたは「Sf(S) に置き換える」という操作を S の長さが 1 になるまで繰り返します。
操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを求めてください。ただし、操作が無限に続く場合は -1 を出力してください。

制約

  • 2 \leq N \leq 10^6
  • S1, 2, 3, 4, 5, 6, 7, 8, 9 からなる長さ N の文字列

入力

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

N
S

出力

操作が終了するまでに行う操作を行う回数を 998244353 で割った余りを出力せよ。ただし、操作が無限に続く場合は -1 を出力せよ。


入力例 1

3
313

出力例 1

4

S = 313 の場合、操作を 4 回行うと S の長さが 1 になります。

  • f(S) = 3111 である。S3111 に置き換える。
  • f(S) = 311 である。S311 に置き換える。
  • f(S) = 31 である。S31 に置き換える。
  • f(S) = 3 である。S3 に置き換える。
  • S の長さが 1 になったので操作を終了する。

入力例 2

9
123456789

出力例 2

-1

S = 123456789 の場合、操作が無限に続きます。この場合は -1 を出力してください。


入力例 3

2
11

出力例 3

1

Score : 600 points

Problem Statement

For a string S consisting of digits from 1 through 9, let f(S) be the string T obtained by the following procedure. (S_i denotes the i-th character of S.)

  • Let T be an initially empty string.
  • For i=1, 2, \dots, |S| - 1, perform the following operation:
    • Append n copies of S_i to the tail of T, where n is the value when S_{i+1} is interpreted as an integer.

For example, S = 313 yields f(S) = 3111 by the following steps.

  • T is initially empty.
  • For i=1, we have n = 1. Append one copy of 3 to T, which becomes 3.
  • For i=2, we have n = 3. Append three copies of 1 to T, which becomes 3111.
  • Terminate the procedure. We obtain T = 3111.

You are given a length-N string S consisting of digits from 1 through 9.
You repeat the following operation until the length of S becomes 1: replace S with f(S).
Find how many times, modulo 998244353, you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.

Constraints

  • 2 \leq N \leq 10^6
  • S is a length-N string consisting of 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the number of times, modulo 998244353, that you perform the operation until you complete it. If you will repeat the operation indefinitely, print -1 instead.


Sample Input 1

3
313

Sample Output 1

4

If S = 313, the length of S be comes 1 after four operations.

  • We have f(S) = 3111. Replace S with 3111.
  • We have f(S) = 311. Replace S with 311.
  • We have f(S) = 31. Replace S with 31.
  • We have f(S) = 3. Replace S with 3.
  • Now that the length of S is 1, terminate the repetition.

Sample Input 2

9
123456789

Sample Output 2

-1

If S = 123456789, you indefinitely repeat the operation. In this case, -1 should be printed.


Sample Input 3

2
11

Sample Output 3

1
I - Black Jack

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

あなたとディーラーでゲームをします。 ゲームは 1 以上 D 以下の整数が等確率で出る D 面サイコロ、0 で初期化された変数 x,y を用いて以下のように行われます。

  • あなたはサイコロを振り、出た目を x に加算する操作を好きな回数行える。ここで、あなたは操作を行うたびに次の操作を行うかを選択できる。

  • その後、ディーラーは y < L を満たす限り、サイコロを振り、出た目を y に加算する操作を繰り返す。

  • x > N の場合あなたの負けである。そうでない場合、y > N または x>y のいずれかを満たす場合あなたの勝ちで、どちらも満たさない場合あなたの負けである。

あなたが勝率を最大化するように適切に行動する際、勝率を求めてください。

制約

  • 入力は全て整数
  • 1\leq L\leq N\leq 2\times 10^5
  • 1\leq D \leq N

入力

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

N L D

出力

答えを出力せよ。出力した値の真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

3 2 2

出力例 1

0.468750000000000

x2 以下の場合操作を続けるという戦略が最適であることが証明できます。


入力例 2

200000 200000 200000

出力例 2

0.999986408692793

Score: 550 points

Problem Statement

You will play a game against a dealer. The game uses a D-sided die (dice) that shows an integer from 1 to D with equal probability, and two variables x and y initialized to 0. The game proceeds as follows:

  • You may perform the following operation any number of times: roll the die and add the result to x. You can choose whether to continue rolling or not after each roll.

  • Then, the dealer will repeat the following operation as long as y < L: roll the die and add the result to y.

  • If x > N, you lose. Otherwise, you win if y > N or x > y and lose if neither is satisfied.

Determine the probability of your winning when you act in a way that maximizes the probability of winning.

Constraints

  • All inputs are integers.
  • 1 \leq L \leq N \leq 2 \times 10^5
  • 1 \leq D \leq N

Input

The input is given from Standard Input in the following format:

N L D

Output

Print the answer. Your output will be considered correct if its absolute or relative error from the true value is at most 10^{-6}.


Sample Input 1

3 2 2

Sample Output 1

0.468750000000000

It can be proved that the optimal strategy is to continue rolling as long as x is not greater than 2.


Sample Input 2

200000 200000 200000

Sample Output 2

0.999986408692793