A - Swap Digit

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

先頭の桁が 0 でない N 桁の正整数 A,B が与えられます。

あなたは、以下の操作を好きな回数(0 回でもよい)繰り返すことができます。

  • 0 \le i \le N-1 を満たす整数 i を選び、A,B10^{i} の位の数字を交換する。

操作を終えたときの A \times B の最小値を 998244353 で割ったあまりを求めてください。

A \times B998244353 で割ったあまりの最小値を求めるのではないことに注意してください。

制約

  • 1 \le N \le 200000
  • A,B は先頭の桁が 0 でない N 桁の正整数

入力

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

N
A
B

出力

答えを 1 行に出力せよ。


入力例 1

2
13
22

出力例 1

276

以下のように 1 回操作を行うと A \times B276 にすることが出来ます。

  • i=0 を選び、A,B1 の位の数字を交換する。A=12,B=23 となる。

A \times B275 以下にすることは出来ないので、答えは 276 です。


入力例 2

8
20220122
21002300

出力例 2

54558365

998244353 で割ったあまりを求めてください。

Score : 300 points

Problem Statement

You are given N-digit positive integers A and B whose topmost digits are not 0.

You can repeat the following operation any number of times (possibly zero).

  • Choose an integer i such that 1 \le i \le N and swap the i-th lowest digits of A and B.

Find the smallest possible value of A \times B after your operations, modulo 998244353.

Note that you are not asked to minimize the remainder when A \times B is divided by 998244353.

Constraints

  • 1 \le N \le 200000
  • A and B are N-digit positive integers whose topmost digits are not 0.

Input

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

N
A
B

Output

Print a single line containing the answer.


Sample Input 1

2
13
22

Sample Output 1

276

You can make A \times B = 276 by performing the operation once, as follows.

  • Choose i=1 to swap the bottommost digits of A and B, making A=12, B=23.

You cannot make A \times B = 275 or less, so the answer is 276.


Sample Input 2

8
20220122
21002300

Sample Output 2

54558365

Find the value modulo 998244353.

B - New Place

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ N の英小文字からなる文字列 S,T が与えられます。

あなたは以下の操作を好きな回数(0 回でもよい)繰り返すことができます。

  • S先頭の文字を削除し、同じ文字を S の任意の位置に挿入する。

ST に一致させることができるか判定し、できるのであれば必要な最小の操作回数を求めてください。

制約

  • 1 \le N \le 2 \times 10^5
  • S,T は英小文字からなる長さ N の文字列

入力

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

N
S
T

出力

ST に一致させることが出来ない場合 -1 を出力せよ。一致させることができる場合必要な最小の操作回数を出力せよ。


入力例 1

4
abab
abba

出力例 1

2

以下のように操作を行うことで 2 回で ST に一致させることができます。

  • S の先頭の文字を削除する。そして、同じ文字 aS の末尾に挿入する。Sbaba となる。
  • S の先頭の文字を削除する。そして、同じ文字 bS2 文字目と 3 文字目の間に挿入する。Sabba となる。

1 回以下の操作で ST に一致させることはできないため、答えは 2 です。


入力例 2

3
arc
cra

出力例 2

2

Score : 400 points

Problem Statement

You are given strings S and T of length N consisting of lowercase English letters.

You can repeat the following operation any number of times (possibly zero).

  • Erase the first character of S and insert the same character at any position of S.

Determine whether it is possible to make S equal T, and if it is possible, find the minimum number of operations needed.

Constraints

  • 1 \le N \le 2 \times 10^5
  • S and T are strings of length N consisting of lowercase English letters.

Input

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

N
S
T

Output

If it is impossible to make S equal T, print -1. If it is possible, print the minimum number of operations needed.


Sample Input 1

4
abab
abba

Sample Output 1

2

You can make S equal T in two operations, as follows.

  • Erase the first character of S, and insert that character a at the end of S, making S baba.
  • Erase the first character of S, and insert that character b between the 2-nd and 3-rd characters of S, making S abba.

It is impossible to make S equal T in one or fewer operations, so the answer is 2.


Sample Input 2

3
arc
cra

Sample Output 2

2
C - Roller

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N) が与えられます。

あなたは以下の操作を好きな回数(0 回でもよい)繰り返すことができます。

  • 1 \le i \le N を満たす整数 i を選び、A_iA_{i+1} で置き換える。

ただし、A_{N+1} とは A_1 のこととします。

AB に一致させることが出来るか判定してください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 5000
  • 1 \le N \le 5000
  • 1 \le A_i,B_i \le N
  • 1 個の入力に含まれるテストケースについて、それらの N の総和は 5000 を超えない。

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは、以下の形式で与えられる。

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

T 行出力せよ。 i 行目には、i 個目のテストケースにおいて AB と一致させることが出来るならば Yes、出来ないならば No を出力せよ。


入力例 1

3
2
1 2
2 2
4
2 3 1 1
2 1 1 2
2
1 1
2 2

出力例 1

Yes
Yes
No

1 個目のテストケースでは、以下のように操作することにより AB と一致させることが出来ます。

  • i=1 を選ぶ。A_1A_2 で置き換える。A=(2,2) となる。

2 個目のテストケースでは、以下のように操作することにより AB と一致させることが出来ます。

  • i=4 を選ぶ。A_4A_1 で置き換える。A=(2,3,1,2) となる。
  • i=2 を選ぶ。A_2A_3 で置き換える。A=(2,1,1,2) となる。

3 個目のテストケースでは、どのように操作しても AB と一致させることは出来ません。

Score : 500 points

Problem Statement

You are given sequences of positive integers of length N: A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N).

You can repeat the following operation any number of times (possibly zero).

  • Choose an integer i such that 1 \le i \le N and replace A_i with A_{i+1}.

Here, regard A_{N+1} as A_1.

Determine whether it is possible to make A equal B.

You have T test cases to solve.

Constraints

  • 1 \le T \le 5000
  • 1 \le N \le 5000
  • 1 \le A_i,B_i \le N
  • For each input file, the sum of N over all test cases does not exceed 5000.

Input

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is in the following format:

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print T lines. The i-th line should contain Yes if it is possible to make A equal B in the i-th test case, and No otherwise.


Sample Input 1

3
2
1 2
2 2
4
2 3 1 1
2 1 1 2
2
1 1
2 2

Sample Output 1

Yes
Yes
No

In the first test case, you can make A equal B as follows.

  • Choose i=1 to replace A_1 with A_2, making A=(2,2).

In the second test case, you can make A equal B as follows.

  • Choose i=4 to replace A_4 with A_1, making A=(2,3,1,2).
  • Choose i=2 to replace A_2 with A_3, making A=(2,1,1,2).

In the third test case, there is no way to make A equal B.

D - A + B > C ?

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

PCT 君は (1,2,\dots,N) の順列 (P_1,P_2,\dots,P_N) を持っています。あなたには N のみが伝えられます。

あなたは PCT 君に以下の質問を 25000 回以下行うことができます。

  • 1 \le i,j,k \le N を満たす整数の組 (i,j,k)1 個指定し、P_i + P_j > P_k かどうかを聞く。

P_1,P_2,\dots,P_N を全て求めてください。

制約

  • 1 \le N \le 2000
  • P はプログラムとジャッジの対話の開始前に決定される。

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)である。

まず、あなたのプログラムに標準入力から順列の長さ N が与えられる。

N

その後、あなたは質問を行うことが出来る。 質問は標準出力に以下の形式で出力せよ。(末尾に改行を入れること。)

? i j k

質問が正当な場合、その質問の答え ans が標準入力から与えられる。

ans

ここで、ansYes または No である。

質問のフォーマットが間違っている、または質問を規定の回数より多く行ったという理由で質問が不正と判断された場合、-1 が標準入力から与えられる。

-1

この時、提出はすでに不正解と判定されている。ジャッジプログラムはこの時点で対話を終了するため、あなたのプログラムも終了するのが望ましい。

P_1,P_2,\dots,P_N が全て分かったら、標準出力に以下の形式で出力せよ。(末尾に改行を入れること。)

! P_1 P_2 \dots P_N

ジャッジ

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush せよ。そうしなかった場合、ジャッジ結果が TLE となる可能性がある。
  • 答えを出力したら(または -1 を受け取ったら)直ちにプログラムを正常終了せよ。そうしなかった場合、ジャッジ結果は不定である。
  • 余計な改行は不正なフォーマットの出力とみなされることに注意せよ。

入出力例

以下は、N = 4,P=(3,1,2,4) の場合の入出力例です。

入力 出力 説明
4 N が与えられます。
? 1 2 3 1 個目の質問として、P_1 + P_2 > P_3 かどうかを聞きます。
Yes P_1 + P_2=4,P_3=2 であるため、返答は Yes です。
? 2 3 3 2 個目の質問として、P_2 + P_3 > P_3 かどうかを聞きます。
Yes P_2 + P_3=3,P_3=2 であるため、返答は Yes です。
? 2 3 4 3 個目の質問として、P_2 + P_3 > P_4 かどうかを聞きます。
No P_2 + P_3=3,P_4=4 であるため、返答は No です。
! 3 1 2 4 P_1,P_2,P_3,P_4 を出力します。実際に、P=(3,1,2,4) であるため、AC となります。

Score : 700 points

Problem Statement

PCT has a permutation (P_1,P_2,\dots,P_N) of (1,2,\dots,N). You are only informed of N.

You can ask him at most 25000 questions of the following form.

  • Specify a triple of integers (i,j,k) such that 1 \le i,j,k \le N and ask whether P_i + P_j > P_k.

Find all of P_1,P_2,\dots,P_N.

Constraints

  • 1 \le N \le 2000
  • P is decided before the start of the interaction of your program and the judge.

Input and Output

This is an interactive task, where your program and the judge interact via input and output.

First, your program is given N, the length of the permutation, from Standard Input:

N

Then, you get to ask questions. Print your question to Standard Output in the following format: (There should be a newline at the end.)

? i j k

If the question is valid, the answer ans will be given from Standard Input:

ans

Here, ans is Yes or No.

If the question is malformed or judged invalid because you have asked more questions than allowed, -1 will be given from Standard Input:

-1

Here, the submission has already been judged incorrect. The judge will end the interaction at this point; preferably, your program should also quit.

Once you have identified all of P_1, P_2, \dots, P_N, print them to Standard Output in the following format: (There should be a newline at the end.)

! P_1 P_2 \dots P_N

Judging

  • Each time you print something, end it with a newline and flush Standard Output. Otherwise, you might get a TLE verdict.
  • After printing the answer (or receiving -1), immediately terminate the program normally. Otherwise, the verdict will be indeterminate.
  • Note that unnecessary newlines will be considered as malformed output.

Sample Interaction

Below is an interaction with N = 4 and P=(3,1,2,4).

Input Output Description
4 N is given.
? 1 2 3 As the first question, you ask whether P_1 + P_2 > P_3.
Yes We have P_1 + P_2=4 and P_3=2, so the answer is Yes.
? 2 3 3 As the second question, you ask whether P_2 + P_3 > P_3.
Yes We have P_2 + P_3=3 and P_3=2, so the answer is Yes.
? 2 3 4 As the third question, you ask whether P_2 + P_3 > P_4.
No We have P_2 + P_3=3 and P_4=4, so the answer is No.
! 3 1 2 4 You print P_1,P_2,P_3,P_4. We do have P=(3,1,2,4), so you get an AC.
E - Reverse and Inversion

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

(1,2,\dots,N) の順列 Q=(Q_1,Q_2,\dots,Q_N) に対する以下の値を f(Q) と置きます。

1 \le i < j \le N かつ Q_i > Q_j を満たす整数の組 (i,j) 全てに対する j-i の総和

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。

以下の操作を M 回繰り返します。

  • 1 \le i \le j \le N を満たす整数の組 (i,j) を選ぶ。P_i,P_{i+1},\dots,P_j を反転する。厳密には、P_i,P_{i+1},\dots,P_j の値を P_j,P_{j-1},\dots,P_i の値に同時に置き換える。

操作を行う方法は \left(\frac{N(N+1)}{2}\right)^{M} 通りありますが、その全てに対して操作終了後の f(P) を求めたとします。

これらの \left(\frac{N(N+1)}{2}\right)^{M} 個の値の総和を 998244353 で割ったあまりを求めてください。

制約

  • 1 \le N,M \le 2 \times 10^5
  • (P_1,P_2,\dots,P_N)(1,2,\dots,N) の順列である。

入力

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

N M
P_1 P_2 \dots P_N

出力

答えを 1 行に出力せよ。


入力例 1

2 1
1 2

出力例 1

1

あり得る操作は以下の 3 通りです。

  • (i,j)=(1,1) を選ぶ。P=(1,2) となる。f(P)=0 である。
  • (i,j)=(1,2) を選ぶ。P=(2,1) となる。f(P)=1 である。
  • (i,j)=(2,2) を選ぶ。P=(1,2) となる。f(P)=0 である。

よって、答えは 0+1+0=1 です。


入力例 2

3 2
3 2 1

出力例 2

90

入力例 3

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

出力例 3

543960046

Score : 900 points

Problem Statement

For a permutation Q=(Q_1,Q_2,\dots,Q_N) of (1,2,\dots,N), let f(Q) be the following value:

the sum of j-i over all pairs of integers (i,j) such that 1 \le i < j \le N and Q_i > Q_j.

You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N).

Let us repeat the following operation M times.

  • Choose a pair of integers (i,j) such that 1 \le i \le j \le N. Reverse P_i,P_{i+1},\dots,P_j. Formally, replace the values of P_i,P_{i+1},\dots,P_j with P_j,P_{j-1},\dots,P_i simultaneously.

There are \left(\frac{N(N+1)}{2}\right)^{M} ways to repeat the operation. Assume that we have computed f(P) for all those ways.

Find the sum of these \left(\frac{N(N+1)}{2}\right)^{M} values, modulo 998244353.

Constraints

  • 1 \le N,M \le 2 \times 10^5
  • (P_1,P_2,\dots,P_N) is a permutation of (1,2,\dots,N).

Input

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

N M
P_1 P_2 \dots P_N

Output

Print a single line containing the answer.


Sample Input 1

2 1
1 2

Sample Output 1

1

There are three ways to perform the operation, as follows.

  • Choose (i,j)=(1,1), making P=(1,2), where f(P)=0.
  • Choose (i,j)=(1,2), making P=(2,1), where f(P)=1.
  • Choose (i,j)=(2,2), making P=(1,2), where f(P)=0.

Thus, the answer is 0+1+0=1.


Sample Input 2

3 2
3 2 1

Sample Output 2

90

Sample Input 3

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

Sample Output 3

543960046
F - Dice Game

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 900

問題文

全ての目が出る確率が等しい N 面サイコロがあります。このサイコロを、全ての目が出るまで振り続けます。

1 \le i \le M を満たす整数 i に対して、サイコロを振る回数の i 乗の期待値 \bmod\ 998244353 を求めてください。

期待値 \bmod\ 998244353 の定義

求める期待値は必ず有理数になることが証明できます。また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \neq 0 \pmod{998244353} となることも証明できます。よって、R \times Q = P \pmod{998244353},0 \le R < 998244353 を満たす整数 R が一意に定まります。この R を答えてください。

制約

  • 1 \le N,M \le 2 \times 10^5
  • 入力は全て整数である。

入力

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

N M

出力

M 行出力せよ。

i 行目には、サイコロを振る回数の i 乗の期待値 \bmod\ 998244353 を出力せよ。


入力例 1

3 3

出力例 1

499122182
37
748683574

i=1 の場合、求めるべき期待値は全ての目が出るまでの操作回数です。その値は \frac{11}{2} です。


入力例 2

7 8

出力例 2

449209977
705980975
631316005
119321168
62397541
596241562
584585746
378338599

入力例 3

2023 7

出力例 3

442614988
884066164
757979000
548628857
593993207
780067557
524115712

Score : 900 points

Problem Statement

We have an N-sided die where all sides have the same probability to show up. Let us repeat rolling this die until every side has shown up.

For integers i such that 1 \le i \le M, find the expected value, modulo 998244353, of the i-th power of the number of times we roll the die.

Definition of expected value modulo 998244353

It can be proved that the sought expected values are always rational numbers. Additionally, under the constraints of this problem, when such a value is represented as an irreducible fraction \frac{P}{Q}, it can be proved that Q \neq 0 \pmod{998244353}. Thus, there is a unique integer R such that R \times Q = P \pmod{998244353} and 0 \le R < 998244353. Print this R.

Constraints

  • 1 \le N,M \le 2 \times 10^5
  • All values in the input are integers.

Input

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

N M

Output

Print M lines.

The i-th line should contain the expected value, modulo 998244353, of the i-th power of the number of times we roll the die.


Sample Input 1

3 3

Sample Output 1

499122182
37
748683574

For i=1, you should find the expected value of the number of times we roll the die, which is \frac{11}{2}.


Sample Input 2

7 8

Sample Output 2

449209977
705980975
631316005
119321168
62397541
596241562
584585746
378338599

Sample Input 3

2023 7

Sample Output 3

442614988
884066164
757979000
548628857
593993207
780067557
524115712