E - digitnum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

整数 N が与えられるので、以下の問題を解いてください。

f(x)= ( x 以下の正整数で、 x と桁数が同じものの数) とします。
f(1)+f(2)+\dots+f(N)998244353 で割った余りを求めてください。

制約

  • N は整数
  • 1 \le N < 10^{18}

入力

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

N

出力

答えを整数として出力せよ。


入力例 1

16

出力例 1

73
  • 1 以上 9 以下の正整数 x について、 x 以下の整数で、 x と桁数が同じものは 1,2,\dots,x です。
    • これより、 f(1)=1,f(2)=2,...,f(9)=9 となります。
  • 10 以上 16 以下の正整数 x について、 x 以下の整数で、 x と桁数が同じものは 10,11,\dots,x です。
    • これより、 f(10)=1,f(11)=2,...,f(16)=7 となります。

結局、求める答えは 73 です。


入力例 2

238

出力例 2

13870

入力例 3

999999999999999999

出力例 3

762062362

998244353 で割った余りを求めることに注意してください。

Score : 300 points

Problem Statement

Given an integer N, solve the following problem.

Let f(x)= (The number of positive integers at most x with the same number of digits as x).
Find f(1)+f(2)+\dots+f(N) modulo 998244353.

Constraints

  • N is an integer.
  • 1 \le N < 10^{18}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

16

Sample Output 1

73
  • For a positive integer x between 1 and 9, the positive integers at most x with the same number of digits as x are 1,2,\dots,x.
    • Thus, we have f(1)=1,f(2)=2,...,f(9)=9.
  • For a positive integer x between 10 and 16, the positive integers at most x with the same number of digits as x are 10,11,\dots,x.
    • Thus, we have f(10)=1,f(11)=2,...,f(16)=7.

The final answer is 73.


Sample Input 2

238

Sample Output 2

13870

Sample Input 3

999999999999999999

Sample Output 3

762062362

Be sure to find the sum modulo 998244353.

F - kasaka

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字からなる文字列 S が与えられます。 S の先頭に a をいくつか( 0 個でも良い)つけ加えて回文にすることができるか判定してください。

ただし、長さ N の文字列 A=A_1A_2\ldots A_N が回文であるとは、すべての 1\leq i\leq N について A_i=A_{N+1-i} が成り立っていることをいいます。

制約

  • 1 \leq \lvert S \rvert \leq 10^6
  • S は英小文字のみからなる。

入力

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

S

出力

S の先頭に a をいくつかつけ加えて回文にすることができるならば Yes を、そうでないならば No を出力せよ。


入力例 1

kasaka

出力例 1

Yes

kasaka の先頭に a1 つ付け加えることによって、akasaka となり回文となるため Yes を出力します。


入力例 2

atcoder

出力例 2

No

atcoder の先頭に a をいくつ付け加えても回文となる事はありません。


入力例 3

php

出力例 3

Yes

php はそれ自体回文です。S の先頭に付け加える a0 個でも許されるため、Yes を出力します。

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Determine whether adding some number of a's (possibly zero) at the beginning of S can make it a palindrome.

Here, a string of length N, A=A_1A_2\ldots A_N, is said to be a palindrome when A_i=A_{N+1-i} for every 1\leq i\leq N.

Constraints

  • 1 \leq \lvert S \rvert \leq 10^6
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If adding some number of a's (possibly zero) at the beginning of S can make it a palindrome, print Yes; otherwise, print No.


Sample Input 1

kasaka

Sample Output 1

Yes

By adding one a at the beginning of kasaka, we have akasaka, which is a palindrome, so Yes should be printed.


Sample Input 2

atcoder

Sample Output 2

No

Adding any number of a's at the beginning of atcoder does not make it a palindrome.


Sample Input 3

php

Sample Output 3

Yes

php itself is a palindrome. Adding zero a's at the beginning of S is allowed, so Yes should be printed.

G - Set Menu

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

AtCoder 食堂では N 種類の主菜と M 種類の副菜が提供されており、i 種類目の主菜の価格は A_ij 種類目の副菜の価格は B_j です。 AtCoder 食堂では新たに定食のメニューを設けることを検討しています。 定食は 1 種類の主菜と 1 種類の副菜から構成され、主菜と副菜の価格の和を s としたとき、定食の価格は \min(s,P) です。 ここで、P は入力で与えられる定数です。

定食を構成する主菜と副菜の選び方は NM 通りありますが、それらすべてに対する定食の価格の総和を求めてください。

制約

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_j \leq 10^8
  • 1\leq P \leq 2\times 10^8
  • 入力は全て整数

入力

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

N M P
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

答えを整数として出力せよ。 なお、本問題の制約下では、答えは 64 bit 符号付き整数型の範囲に収まることが証明できる。


入力例 1

2 2 7
3 5
6 1

出力例 1

24
  • 1 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(3+6,7)=7 です。
  • 1 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(3+1,7)=4 です。
  • 2 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(5+6,7)=7 です。
  • 2 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(5+1,7)=6 です。

よって、答えは 7+4+7+6=24 です。


入力例 2

1 3 2
1
1 1 1

出力例 2

6

入力例 3

7 12 25514963
2436426 24979445 61648772 23690081 33933447 76190629 62703497
11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857

出力例 3

2115597124

Score : 400 points

Problem Statement

AtCoder cafeteria offers N main dishes and M side dishes. The price of the i-th main dish is A_i, and that of the j-th side dish is B_j. The cafeteria is considering introducing a new set meal menu. A set meal consists of one main dish and one side dish. Let s be the sum of the prices of the main dish and the side dish, then the price of the set meal is \min(s,P). Here, P is a constant given in the input.

There are NM ways to choose a main dish and a side dish for a set meal. Find the total price of all these set meals.

Constraints

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_j \leq 10^8
  • 1\leq P \leq 2\times 10^8
  • All input values are integers.

Input

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

N M P
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print the answer as an integer. Under the constraints of this problem, it can be proved that the answer fits into a 64-bit signed integer.


Sample Input 1

2 2 7
3 5
6 1

Sample Output 1

24
  • If you choose the first main dish and the first side dish, the price of the set meal is \min(3+6,7)=7.
  • If you choose the first main dish and the second side dish, the price of the set meal is \min(3+1,7)=4.
  • If you choose the second main dish and the first side dish, the price of the set meal is \min(5+6,7)=7.
  • If you choose the second main dish and the second side dish, the price of the set meal is \min(5+1,7)=6.

Thus, the answer is 7+4+7+6=24.


Sample Input 2

1 3 2
1
1 1 1

Sample Output 2

6

Sample Input 3

7 12 25514963
2436426 24979445 61648772 23690081 33933447 76190629 62703497
11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857

Sample Output 3

2115597124
H - Toward 0

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

整数 N が与えられます。あなたは次の 2 種類の操作を行うことができます。

  • X 円払う。N\displaystyle\left\lfloor\frac{N}{A}\right\rfloor に置き換える。
  • Y 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N\displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。

ここで \lfloor s \rfloors 以下の最大の整数を表します。例えば \lfloor 3 \rfloor=3\lfloor 2.5 \rfloor=2 です。

適切に操作を選択したとき、N0 にするまでに払う必要がある金額の期待値の最小値を求めてください。
なお、サイコロの出目は操作ごとに独立であり、操作の選択はそれまでの操作の結果を確認してから行うことができます。

制約

  • 1 \leq N \leq 10^{18}
  • 2 \leq A \leq 6
  • 1 \leq X,Y \leq 10^9
  • 入力は全て整数である

入力

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

N A X Y

出力

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


入力例 1

3 2 10 20

出力例 1

20.000000000000000

行える操作は 次の 2 種類です。

  • 10 円払う。N\displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
  • 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N\displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。

前者の操作を 2 回行うのが最適です。


入力例 2

3 2 20 20

出力例 2

32.000000000000000

行える操作は 次の 2 種類です。

  • 20 円払う。N\displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
  • 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N\displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。

最適な操作は以下のようになります。

  • まず後者の操作でサイコロを振ります。
    • 4 以上が出た場合 N=0 となります。
    • 2 または 3 が出た場合 N=1 となります。続けて前者の操作を行うことで N=0 となります。
    • 1 が出た場合最初からやり直します。

入力例 3

314159265358979323 4 223606797 173205080

出力例 3

6418410657.7408381

Score: 450 points

Problem Statement

You are given an integer N. You can perform the following two types of operations:

  • Pay X yen to replace N with \displaystyle\left\lfloor\frac{N}{A}\right\rfloor.
  • Pay Y yen to roll a die (dice) that shows an integer between 1 and 6, inclusive, with equal probability. Let b be the outcome of the die, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.

Here, \lfloor s \rfloor denotes the greatest integer less than or equal to s. For example, \lfloor 3 \rfloor=3 and \lfloor 2.5 \rfloor=2.

Determine the minimum expected cost paid before N becomes 0 when optimally choosing operations.
The outcome of the die in each operation is independent of other rolls, and the choice of operation can be made after observing the results of the previous operations.

Constraints

  • 1 \leq N \leq 10^{18}
  • 2 \leq A \leq 6
  • 1 \leq X, Y \leq 10^9
  • All input values are integers.

Input

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

N A X Y

Output

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


Sample Input 1

3 2 10 20

Sample Output 1

20.000000000000000

The available operations are as follows:

  • Pay 10 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
  • Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.

The optimal strategy is to perform the first operation twice.


Sample Input 2

3 2 20 20

Sample Output 2

32.000000000000000

The available operations are as follows:

  • Pay 20 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
  • Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.

The optimal strategy is as follows:

  • First, perform the second operation to roll the die.
    • If the outcome is 4 or greater, then N becomes 0.
    • If the outcome is 2 or 3, then N becomes 1. Now, perform the first operation to make N = 0.
    • If the outcome is 1, restart from the beginning.

Sample Input 3

314159265358979323 4 223606797 173205080

Sample Output 3

6418410657.7408381
I - SSttrriinngg in StringString

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

長さ n の文字列 X に対して、Xk 回繰り返して得られる文字列を f(X,k) と表記し、X1 文字目、2 文字目、\dotsn 文字目を k 回ずつこの順に繰り返して得られる文字列を g(X,k) と表記します。 例えば、X= abc のとき、f(X,2)= abcabcg(X,3)= aaabbbccc です。 また、任意の文字列 X に対して、f(X,0)g(X,0) は共に空文字列です。

正整数 N および文字列 S,T が与えられます。 g(T,k)f(S,N) の(連続とは限らない)部分列であるような最大の非負整数 k を求めてください。 なお、定義より、g(T,0) は常に f(S,N) の部分列であることに注意してください。

部分列とは 文字列 X の(連続とは限らない)部分列とは、X から 0 個以上の文字を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことをいいます。 例えば、acatcoder (空文字列)などはどれも atcoder の部分列ですが、taatcoder の部分列ではありません。

制約

  • N は整数
  • 1\leq N\leq 10^{12}
  • S, T は英小文字からなる長さ 1 以上 10^5 以下の文字列

入力

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

N
S
T

出力

g(T,k)f(S,N) の(連続とは限らない)部分列であるような最大の非負整数 k を出力せよ。


入力例 1

3
abc
ab

出力例 1

2

f(S,3)= abcabcabc です。 g(T,2)= aabbf(S,3) の部分列ですが、g(T,3)= aaabbbf(S,3) の部分列ではないので、2 を出力します。


入力例 2

3
abc
arc

出力例 2

0

入力例 3

1000000000000
kzazkakxkk
azakxk

出力例 3

344827586207

Score: 525 points

Problem Statement

For a string X of length n, let f(X,k) denote the string obtained by repeating k times the string X, and g(X,k) denote the string obtained by repeating k times the first character, the second character, \dots, the n-th character of X, in this order. For example, if X= abc, then f(X,2)= abcabc, and g(X,3)= aaabbbccc. Also, for any string X, both f(X,0) and g(X,0) are empty strings.

You are given a positive integer N and strings S and T. Find the largest non-negative integer k such that g(T,k) is a (not necessarily contiguous) subsequence of f(S,N). Note that g(T,0) is always a subsequence of f(S,N) by definition.

What is a subsequence? A (not necessarily contiguous) subsequence of a string X is a string obtained by removing zero or more characters from X and then concatenating the remaining elements without changing the order. For example, ac, atcoder, and (empty string) are all subsequences of atcoder, but ta is not.

Constraints

  • N is an integer.
  • 1\leq N\leq 10^{12}
  • S and T are strings consisting of lowercase English letters with lengths between 1 and 10^5, inclusive.

Input

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

N
S
T

Output

Print the largest non-negative integer k such that g(T,k) is a (not necessarily contiguous) subsequence of f(S,N).


Sample Input 1

3
abc
ab

Sample Output 1

2

We have f(S,3)= abcabcabc. g(T,2)= aabb is a subsequence of f(S,3), but g(T,3)= aaabbb is not, so print 2.


Sample Input 2

3
abc
arc

Sample Output 2

0

Sample Input 3

1000000000000
kzazkakxkk
azakxk

Sample Output 3

344827586207