A - Many A+B Problems

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 個の整数の 2 つ組 (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N) が与えられます。 各 i = 1, 2, \ldots, N について、A_i + B_i を出力してください。

制約

  • 1 \leq N \leq 1000
  • -10^9 \leq A_i, B_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には A_i+B_i を出力せよ。


入力例 1

4
3 5
2 -6
-5 0
314159265 123456789

出力例 1

8
-4
-5
437616054
  • 1 行目には、A_1 + B_1 = 3 + 5 = 8 を、
  • 2 行目には、A_2 + B_2 = 2 + (-6) = -4 を、
  • 3 行目には、A_3 + B_3 = (-5) + 0 = -5 を、
  • 4 行目には、A_4 + B_4 = 314159265 + 123456789 = 437616054 を出力します。

Score : 100 points

Problem Statement

You are given N pairs of integers: (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N). For each i = 1, 2, \ldots, N, print A_i + B_i.

Constraints

  • 1 \leq N \leq 1000
  • -10^9 \leq A_i, B_i \leq 10^9
  • All values in the input are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain A_i+B_i.


Sample Input 1

4
3 5
2 -6
-5 0
314159265 123456789

Sample Output 1

8
-4
-5
437616054
  • The first line should contain A_1 + B_1 = 3 + 5 = 8.
  • The second line should contain A_2 + B_2 = 2 + (-6) = -4.
  • The third line should contain A_3 + B_3 = (-5) + 0 = -5.
  • The fourth line should contain A_4 + B_4 = 314159265 + 123456789 = 437616054.
B - Welcome to AtCoder Land

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

高橋君は AtCoder Land を目指しています。 目の前に看板が置かれているので、ここが AtCoder Land であるかどうか判定したいです。

文字列 S,T が空白区切りで与えられます。 S= AtCoder かつ T= Land であるかどうか判定してください。

制約

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

入力

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

S T

出力

S= AtCoder かつ T= Land であるならば Yes を、そうでないならば No を出力せよ。


入力例 1

AtCoder Land

出力例 1

Yes

S= AtCoder かつ T= Land です。


入力例 2

CodeQUEEN Land

出力例 2

No

S= AtCoder ではありません。


入力例 3

aTcodeR lANd

出力例 3

No

大文字と小文字は区別します。

Score : 100 points

Problem Statement

Takahashi is heading to AtCoder Land. There is a signboard in front of him, and he wants to determine whether it says AtCoder Land.

You are given two strings S and T separated by a space. Determine whether S= AtCoder and T= Land.

Constraints

  • S and T are strings consisting of uppercase and lowercase English letters, with lengths between 1 and 10, inclusive.

Input

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

S T

Output

If S= AtCoder and T= Land, print Yes; otherwise, print No.


Sample Input 1

AtCoder Land

Sample Output 1

Yes

S= AtCoder and T= Land.


Sample Input 2

CodeQUEEN Land

Sample Output 2

No

S is not AtCoder.


Sample Input 3

aTcodeR lANd

Sample Output 3

No

Uppercase and lowercase letters are distinguished.

C - Enlarged Checker Board

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

縦 A 行、横 B 列のマスからなるタイルを縦 N 行、横 N 列に並べてできた、縦 (A\times N) 行、横 (B\times N) 列のマス目 X があります。
1\leq i,j \leq N について、上から i 行目、左から j 列目のタイルをタイル (i,j) とします。

X の各マスは以下のように塗られています。

  • 各タイルは白いタイルまたは黒いタイルである。
  • 白いタイルのすべてのマスは白で塗られ、黒いタイルのすべてのマスは黒で塗られている。
  • タイル (1,1) は白いタイルである。
  • 辺で隣接する 2 つのタイルは異なる色のタイルである。ただし、タイル (a,b) とタイル (c,d) が辺で隣接するとは、|a-c|+|b-d|=1 ( |x| を x の絶対値とする)であることを言う。

マス目 X を出力の形式に従って出力してください。

制約

  • 1 \leq N,A,B \leq 10
  • 入力は全て整数

入力

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

N A B

出力

次の条件をみたす (A\times N) 個の文字列 S_1,\ldots,S_{A\times N} を改行区切りで出力せよ。

  • S_1,\ldots,S_{A\times N} はそれぞれ長さ (B\times N) の . または # からなる文字列である。
  • 各 i,j (1 \leq i \leq A\times N,1 \leq j \leq B\times N) に対し、マス目 X の上から i 行目かつ左から j 列目のマスが白で塗られているならば S_i の j 文字目は .であり、黒く塗られているならば # である。

入力例 1

4 3 2

出力例 1

..##..##
..##..##
..##..##
##..##..
##..##..
##..##..
..##..##
..##..##
..##..##
##..##..
##..##..
##..##..

入力例 2

5 1 5

出力例 2

.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....

入力例 3

4 4 1

出力例 3

.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.
.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.

入力例 4

1 4 4

出力例 4

....
....
....
....

Score : 200 points

Problem Statement

Tiles are aligned in N horizontal rows and N vertical columns. Each tile has a grid with A horizontal rows and B vertical columns. On the whole, the tiles form a grid X with (A\times N) horizontal rows and (B\times N) vertical columns.
For 1\leq i,j \leq N, Tile (i,j) denotes the tile at the i-th row from the top and the j-th column from the left.

Each square of X is painted as follows.

  • Each tile is either a white tile or a black tile.
  • Every square in a white tile is painted white; every square in a black tile is painted black.
  • Tile (1,1) is a white tile.
  • Two tiles sharing a side have different colors. Here, Tile (a,b) and Tile (c,d) are said to be sharing a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).

Print the grid X in the format specified in the Output section.

Constraints

  • 1 \leq N,A,B \leq 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print (A\times N) strings S_1,\ldots,S_{A\times N} that satisfy the following condition, with newlines in between.

  • Each of S_1,\ldots,S_{A\times N} is a string of length (B\times N) consisting of . and #.
  • For each i and j (1 \leq i \leq A\times N,1 \leq j \leq B\times N), the j-th character of S_i is . if the square at the i-th row from the top and j-th column from the left in grid X is painted white; the character is # if the square is painted black.

Sample Input 1

4 3 2

Sample Output 1

..##..##
..##..##
..##..##
##..##..
##..##..
##..##..
..##..##
..##..##
..##..##
##..##..
##..##..
##..##..

Sample Input 2

5 1 5

Sample Output 2

.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....

Sample Input 3

4 4 1

Sample Output 3

.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.
.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.

Sample Input 4

1 4 4

Sample Output 4

....
....
....
....
D - Qual B

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

あるプログラミングコンテストの予選に N 人が参加し、参加者全員が異なる順位を得ました。
長さ N の文字列 S が与えられ、この文字列は決勝への参加希望の有無を表現します。具体的には下記の通りです。

  • S の i 文字目が o なら、予選 i 位の参加者が決勝への参加を希望した。
  • S の i 文字目が x なら、予選 i 位の参加者が決勝への参加を希望しなかった。

決勝への参加を希望した参加者のうち順位の小さい方から K 人が予選を通過します。

以下の条件を満たす長さ N の文字列 T を出力してください。

  • 予選 i 位の参加者が予選を通過する場合、 T の i 文字目は o
  • 予選 i 位の参加者が予選を通過しない場合、 T の i 文字目は x

制約

  • N,K は整数
  • 1 \le K \le N \le 100
  • S は o と x からなる長さ N の文字列
  • S には少なくとも K 個の o が含まれる

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

10 3
oxxoxooxox

出力例 1

oxxoxoxxxx

この入力の場合、予選の参加者は N=10 人であり、予選を通過する人数は K=3 人です。

  • 予選 1 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 1 人です。
  • 予選 2,3 位の参加者は決勝への参加を希望していないため、予選を通過しません。
  • 予選 4 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 2 人です。
  • 予選 5 位の参加者は決勝への参加を希望していないため、予選を通過しません。
  • 予選 6 位の参加者は決勝への参加を希望しているため、予選を通過します。この時点で、通過者は 3 人です。
  • ここで、予選を通過した人数が 3 人となりました。なので、予選 7 位以下の参加者は予選を通過しません。

Score : 200 points

Problem Statement

There were N contestants in the qualification round of a programming contest. All contestants got distinct ranks.
You are given a length-N string S, which represents whether the contestants want to participate in the final round or not. Specifically,

  • if the i-th character of S is o, the contestant ranked i-th in the qualification wants to participate in the final;
  • if the i-th character of S is x, the contestant ranked i-th in the qualification does not want to participate in the final.

Among those who want to participate in the final, K contestants with the smallest ranks advance to the final.

Print a string T of length N that satisfies the following conditions:

  • if the contestant ranked i-th in the qualification advances to the final, the i-th character of T is o;
  • if the contestant ranked i-th in the qualification does not advance to the final, the i-th character of T is x.

Constraints

  • N and K are integers.
  • 1 \le K \le N \le 100
  • S is a string of length N consisting of o and x.
  • S has at least K o's.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

10 3
oxxoxooxox

Sample Output 1

oxxoxoxxxx

In this input, N=10 people took part in the qualification round, and K=3 of them advance to the final.

  • The participant who ranked 1-st in the qualification wants to participate in the final, so the participant advances to the final. 1 participant has advanced so far.
  • The participants who ranked 2-nd and 3-rd in the qualification do not want to participate in the final, so the participants do not advance to the final.
  • The participant who ranked 4-th in the qualification wants to participate in the final, so the participant advances to the final. 2 participants have advanced so far.
  • The participants who ranked 5-th in the qualification does not want to participate in the final, so the participant does not advance to the final.
  • The participant who ranked 6-th in the qualification wants to participate in the final, so the participant advances to the final. 3 participants have advanced so far.
  • Now that 3 people have advanced to the final, no participants ranked 7-th or lower advance to the final.
E - Extra Character

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

文字列 S,T が与えられます。S は英小文字からなり、T は S に英小文字を 1 つ挿入して作られたことがわかっています。

挿入された文字は T の先頭から何番目の文字であるか求めてください。
複数の候補が考えられる場合はいずれか 1 つを求めてください。

制約

  • 1 \leq |S| \leq 5\times 10^5
  • S は英小文字からなる
  • T は S に英小文字を 1 つ挿入して作られた文字列である

入力

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

S
T

出力

答えを出力せよ。なお、答えが複数考えられる場合はどれを出力しても正解となる。


入力例 1

atcoder
atcorder

出力例 1

5

T の先頭から 5 番目の文字 r が挿入された文字です。


入力例 2

million
milllion

出力例 2

5

T の先頭から 3,4,5 番目の文字のいずれかが挿入された文字です。
よって、3,4,5 のいずれかを出力すると正解となります。


入力例 3

vvwvw
vvvwvw

出力例 3

3

Score : 300 points

Problem Statement

You are given strings S and T. S consists of lowercase English letters, and T is obtained by inserting a lowercase English letter into S.

Find the position of the inserted character in T.
If there are multiple candidates, find any of them.

Constraints

  • 1 \leq |S| \leq 5\times 10^5
  • S consists of lowercase English letters.
  • T is obtained by inserting a lowercase English letter into S.

Input

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

S
T

Output

Print an integer i, representing that the inserted character is the i-th character from the beginning of T. If there are multiple possible answers, printing any of them is accepted.


Sample Input 1

atcoder
atcorder

Sample Output 1

5

The 5-th character from the beginning of T, r, is inserted.


Sample Input 2

million
milllion

Sample Output 2

5

One of the 3-rd, 4-th, and 5-th characters from the beginning of T is inserted. Thus, printing any one of 3, 4, and 5 is accepted.


Sample Input 3

vvwvw
vvvwvw

Sample Output 3

3
F - Upgrade Required

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

ある OS のバージョンは N 個あり、古い順に 1,2,\dots,N の番号がついています。
PC が N 台あり、初めは i 番目の PC の OS のバージョンは i です。

Q 回の操作を順に行ってください。そのうち i 回目は次の通りです。

  • 現時点での OS のバージョンが X_i かそれ以前の PC 全てを、バージョンを Y_i(>X_i) にアップグレードする。その後、この操作でアップグレードを行った PC の台数を出力する。

i<Q について、 i 回目の操作でのアップグレードが行われた状態で i+1 回目の操作に進むことに注意してください。

制約

  • 入力は全て整数
  • 2 \le N \le 10^6
  • 1 \le Q \le 2 \times 10^5
  • 1 \le X_i < Y_i \le N

入力

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_Q Y_Q

出力

Q 行出力せよ。
そのうち i 行目には、 i 回目の操作でアップグレードを行った PC の台数を出力せよ。


入力例 1

8 5
2 6
3 5
1 7
5 7
7 8

出力例 1

2
1
0
3
7

この入力には 5 回の操作が含まれます。

  • はじめ、 8 台の PC のバージョンは 1,2,3,4,5,6,7,8 です。
  • 1 回目の操作で、バージョンが 2 かそれ以前のバージョンの PC をバージョン 6 にアップグレードします。
    • この操作によって 2 台の PC がアップグレードされ、各 PC のバージョンは 6,6,3,4,5,6,7,8 となります。
  • 2 回目の操作で、バージョンが 3 かそれ以前のバージョンの PC をバージョン 5 にアップグレードします。
    • この操作によって 1 台の PC がアップグレードされ、各 PC のバージョンは 6,6,5,4,5,6,7,8 となります。
  • 3 回目の操作で、バージョンが 1 かそれ以前のバージョンの PC をバージョン 7 にアップグレードします。
    • この操作によって 0 台の PC がアップグレードされ、各 PC のバージョンは 6,6,5,4,5,6,7,8 となります。
  • 4 回目の操作で、バージョンが 5 かそれ以前のバージョンの PC をバージョン 7 にアップグレードします。
    • この操作によって 3 台の PC がアップグレードされ、各 PC のバージョンは 6,6,7,7,7,6,7,8 となります。
  • 5 回目の操作で、バージョンが 7 かそれ以前のバージョンの PC をバージョン 8 にアップグレードします。
    • この操作によって 7 台の PC がアップグレードされ、各 PC のバージョンは 8,8,8,8,8,8,8,8 となります。

Score : 300 points

Problem Statement

There are N versions of a certain OS, numbered 1,2,\dots,N in chronological order.
There are N PCs, and initially the OS version of the i-th PC is i.

Perform Q operations in order. The i-th operation is as follows:

  • Upgrade all PCs whose current OS version is X_i or earlier to version Y_i(>X_i). Then, print the number of PCs upgraded in this operation.

Note that for i<Q, the upgrades from the i-th operation are performed before proceeding to the (i+1)-th operation.

Constraints

  • All input values are integers.
  • 2 \le N \le 10^6
  • 1 \le Q \le 2 \times 10^5
  • 1 \le X_i < Y_i \le N

Input

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_Q Y_Q

Output

Output Q lines.
The i-th line should contain the number of PCs upgraded in the i-th operation.


Sample Input 1

8 5
2 6
3 5
1 7
5 7
7 8

Sample Output 1

2
1
0
3
7

This input contains five operations.

  • Initially, the versions of the eight PCs are 1,2,3,4,5,6,7,8.
  • In the first operation, PCs with version 2 or earlier are upgraded to version 6.
    • This operation upgrades two PCs, and the versions of the PCs become 6,6,3,4,5,6,7,8.
  • In the second operation, PCs with version 3 or earlier are upgraded to version 5.
    • This operation upgrades one PC, and the versions of the PCs become 6,6,5,4,5,6,7,8.
  • In the third operation, PCs with version 1 or earlier are upgraded to version 7.
    • This operation upgrades zero PCs, and the versions of the PCs become 6,6,5,4,5,6,7,8.
  • In the fourth operation, PCs with version 5 or earlier are upgraded to version 7.
    • This operation upgrades three PCs, and the versions of the PCs become 6,6,7,7,7,6,7,8.
  • In the fifth operation, PCs with version 7 or earlier are upgraded to version 8.
    • This operation upgrades seven PCs, and the versions of the PCs become 8,8,8,8,8,8,8,8.
G - Cuboid Sum Query

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

正整数 N と、 1 \leq x,y,z \leq N を満たす整数の組 (x,y,z) に対して、整数 A_{x,y,z} が与えられます。

次の形式の Q 個のクエリが与えられるので、それぞれに答えてください。

i 個目 (1 \leq i \leq Q) のクエリでは 1 \leq Lx_i \leq Rx_i \leq N, 1 \leq Ly_i \leq Ry_i \leq N,1 \leq Lz_i \leq Rz_i \leq N をすべて満たす整数の組 (Lx_i, Rx_i, Ly_i, Ry_i, Lz_i, Rz_i) が与えられるので、

\displaystyle{\sum_{x=Lx_i}^{Rx_i} \sum_{y=Ly_i}^{Ry_i} \sum_{z=Lz_i}^{Rz_i} A_{x,y,z}}

を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 2 \times 10^{5}
  • 0 \leq A_{x,y,z} \leq 999 (1 \leq x,y,z \leq N)
  • 1 \leq Lx_i \leq Rx_i \leq N (1 \leq i \leq Q)
  • 1 \leq Ly_i \leq Ry_i \leq N (1 \leq i \leq Q)
  • 1 \leq Lz_i \leq Rz_i \leq N (1 \leq i \leq Q)
  • 入力はすべて整数

入力

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

N
A_{1,1,1} A_{1,1,2} \ldots A_{1,1,N}
A_{1,2,1} A_{1,2,2} \ldots A_{1,2,N}
\vdots
A_{1,N,1} A_{1,N,2} \ldots A_{1,N,N}
A_{2,1,1} A_{2,1,2} \ldots A_{2,1,N}
A_{2,2,1} A_{2,2,2} \ldots A_{2,2,N}
\vdots
A_{2,N,1} A_{2,N,2} \ldots A_{2,N,N}
\vdots
A_{N,1,1} A_{N,1,2} \ldots A_{N,1,N}
A_{N,2,1} A_{N,2,2} \ldots A_{N,2,N}
\vdots
A_{N,N,1} A_{N,N,2} \ldots A_{N,N,N}
Q
Lx_1 Rx_1 Ly_1 Ry_1 Lz_1 Rz_1
Lx_2 Rx_2 Ly_2 Ry_2 Lz_2 Rz_2
\vdots
Lx_Q Rx_Q Ly_Q Ry_Q Lz_Q Rz_Q

出力

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


入力例 1

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

出力例 1

10
26

1 個目のクエリについて、求めるべき値は A_{1,2,1}+A_{2,2,1}=3+7=10 です。よって、10 を出力します。

2 個目のクエリについて、求めるべき値は A_{2,1,1}+A_{2,1,2}+A_{2,2,1}+A_{2,2,2}=5+6+7+8=26 です。よって、26 を出力します。


入力例 2

3
733 857 714
956 208 257
123 719 648
840 881 245
245 112 746
306 942 694
58 870 849
13 208 789
687 906 783
8
3 3 3 3 1 1
1 3 2 3 3 3
2 2 2 3 1 1
1 3 1 1 1 1
2 3 2 3 2 3
1 2 1 1 1 2
3 3 2 2 1 3
1 2 2 3 2 3

出力例 2

687
3917
551
1631
5180
3311
1010
4326

Score : 400 points

Problem Statement

You are given a positive integer N, and an integer A_{x,y,z} for each triple of integers (x, y, z) such that 1 \leq x, y, z \leq N.

You will be given Q queries in the following format, which must be processed in order.

For the i-th query (1 \leq i \leq Q), you are given a tuple of integers (Lx_i, Rx_i, Ly_i, Ry_i, Lz_i, Rz_i) such that 1 \leq Lx_i \leq Rx_i \leq N, 1 \leq Ly_i \leq Ry_i \leq N, and 1 \leq Lz_i \leq Rz_i \leq N. Find:

\displaystyle{\sum_{x=Lx_i}^{Rx_i} \sum_{y=Ly_i}^{Ry_i} \sum_{z=Lz_i}^{Rz_i} A_{x,y,z}}.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 2 \times 10^{5}
  • 0 \leq A_{x,y,z} \leq 999 (1 \leq x, y, z \leq N)
  • 1 \leq Lx_i \leq Rx_i \leq N (1 \leq i \leq Q)
  • 1 \leq Ly_i \leq Ry_i \leq N (1 \leq i \leq Q)
  • 1 \leq Lz_i \leq Rz_i \leq N (1 \leq i \leq Q)
  • All input values are integers.

Input

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

N
A_{1,1,1} A_{1,1,2} \ldots A_{1,1,N}
A_{1,2,1} A_{1,2,2} \ldots A_{1,2,N}
\vdots
A_{1,N,1} A_{1,N,2} \ldots A_{1,N,N}
A_{2,1,1} A_{2,1,2} \ldots A_{2,1,N}
A_{2,2,1} A_{2,2,2} \ldots A_{2,2,N}
\vdots
A_{2,N,1} A_{2,N,2} \ldots A_{2,N,N}
\vdots
A_{N,1,1} A_{N,1,2} \ldots A_{N,1,N}
A_{N,2,1} A_{N,2,2} \ldots A_{N,2,N}
\vdots
A_{N,N,1} A_{N,N,2} \ldots A_{N,N,N}
Q
Lx_1 Rx_1 Ly_1 Ry_1 Lz_1 Rz_1
Lx_2 Rx_2 Ly_2 Ry_2 Lz_2 Rz_2
\vdots
Lx_Q Rx_Q Ly_Q Ry_Q Lz_Q Rz_Q

Output

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


Sample Input 1

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

Sample Output 1

10
26

For the 1st query, the sought value is A_{1,2,1} + A_{2,2,1} = 3 + 7 = 10. Thus, print 10.

For the 2nd query, the sought value is A_{2,1,1} + A_{2,1,2} + A_{2,2,1} + A_{2,2,2} = 5 + 6 + 7 + 8 = 26. Thus, print 26.


Sample Input 2

3
733 857 714
956 208 257
123 719 648
840 881 245
245 112 746
306 942 694
58 870 849
13 208 789
687 906 783
8
3 3 3 3 1 1
1 3 2 3 3 3
2 2 2 3 1 1
1 3 1 1 1 1
2 3 2 3 2 3
1 2 1 1 1 2
3 3 2 2 1 3
1 2 2 3 2 3

Sample Output 2

687
3917
551
1631
5180
3311
1010
4326
H - Shift String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

0, 1 からなる長さの等しい文字列 A,B が与えられます。

A に対して以下の操作を 0 回以上何度でも行うことができます。

  • A の先頭の文字を末尾に移動させる。

A=B とするために必要な最小の操作回数を求めてください。
但し、どのように操作しても A=B とできない場合、代わりに -1 と出力してください。

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

制約

  • 1 \le T \le 10000
  • A,B は 0, 1 からなる文字列
  • 2 \le |A|=|B| \le 10^6
  • ひとつの入力について、 |A| の総和は 10^6 を超えない

入力

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

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

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

A
B

出力

T 行出力せよ。

i 行目には i 番目のテストケースについて、答えを出力せよ。


入力例 1

5
1010001
1000110
000
111
01010
01010
0101
0011
100001101110000001010110110001
101100011000011011100000010101

出力例 1

2
-1
0
-1
22

この入力には 5 個のテストケースが含まれます。

  • 1 番目のテストケースについて、 A= 1010001 、 B= 1000110 です。
    • A に操作を 2 回行うと A が 1010001 \rightarrow 0100011 \rightarrow 1000110 となり、 A=B とできます。
  • 2 番目のテストケースについて、どのように操作を行っても 000 を 111 にすることはできません。
  • 3 番目のテストケースについて、はじめから A=B です。

Score : 450 points

Problem Statement

You are given strings A and B of equal length consisting of 0 and 1.

You can perform the following operation on A zero or more times.

  • Move the first character of A to the end.

Find the minimum number of operations required to make A=B.
If it is impossible to make A=B no matter how you operate, print -1 instead.

You are given T test cases; find the answer for each of them.

Constraints

  • 1 \le T \le 10000
  • A and B are strings consisting of 0 and 1.
  • 2 \le |A|=|B| \le 10^6
  • For a single input, the sum of |A| does not exceed 10^6.

Input

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

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

Each test case is given in the following format:

A
B

Output

Print T lines.

The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
1010001
1000110
000
111
01010
01010
0101
0011
100001101110000001010110110001
101100011000011011100000010101

Sample Output 1

2
-1
0
-1
22

This input contains five test cases.

  • For the first test case, A= 1010001 and B= 1000110.
    • By performing the operation on A twice, A becomes 1010001 \rightarrow 0100011 \rightarrow 1000110, which makes A=B.
  • For the second test case, no matter how you perform the operation, you cannot change 000 to 111.
  • For the third test case, A=B from the beginning.
I - Random Gathering

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 個の皿が、皿 1, 皿 2,\ldots, 皿 N の順に左から並んでいます。 はじめ、皿 i\ (1\le i\le N) には A _ i 個の石が入っています。

この皿たちに対して M 回の操作を行います。 i 回目 (1\le i\le M) の操作では、2 つの整数 L _ i,R _ i が与えられ、次の操作を順に行います。

  • 皿 L _ i, 皿 L _ i+1,\ldots, 皿 R _ i の R _ i-L _ i+1 個の皿に入っている石をすべて皿の上から取り除く。
  • L _ i 以上 R _ i 以下の整数を一様ランダムに 1 つ取り、それを x とする。
  • 取り除いた石をすべて皿 x に乗せる。

i=1,2,\ldots,N について、M 回の操作がすべて終了したときに皿 i に置かれている石の個数の期待値を{}\bmod998244353 で求めてください。

期待値を{}\bmod{998244353} で求めるとは

求める期待値は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \not\equiv 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R \lt 998244353 を満たす整数 R が一意に定まります。期待値を{}\bmod{998244353} で求めるとは、この R を求めることを指します。

制約

  • 1\le N\le2\times10 ^ 5
  • 1\le M\le2\times10 ^ 5
  • 0\le A _ i\lt998244353\ (1\le i\le N)
  • 1\le L _ i\le R _ i\le N\ (1\le i\le M)
  • 入力はすべて整数

入力

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

N M
A _ 1 A _ 2 \ldots A _ N
L _ 1 R _ 1
L _ 2 R _ 2
\vdots
L _ M R _ M

出力

空白を区切りとして N 個の整数を 1 行に出力せよ。 i 番目 (1\leq i\leq N) には M 回の操作がすべて終了したときに皿 i に置かれている石の個数の期待値を{}\bmod998244353 で求め、出力せよ。


入力例 1

7 4
30 10 40 10 50 90 20
4 6
5 7
1 6
3 7

出力例 1

35 35 36 36 36 36 36 

例えば、操作は次のように進みます。

  • 1 回目の操作で 4 が選ばれる。皿 1, 皿 2,\ldots, 皿 7 に置かれている石の個数はそれぞれ 30 個 ,10 個 ,40 個 ,150 個 ,0 個 ,0 個 ,20 個となる。
  • 2 回目の操作で 6 が選ばれる。皿 1, 皿 2,\ldots, 皿 7 に置かれている石の個数はそれぞれ 30 個 ,10 個 ,40 個 ,150 個 ,0 個 ,20 個 ,0 個となる。
  • 3 回目の操作で 2 が選ばれる。皿 1, 皿 2,\ldots, 皿 7 に置かれている石の個数はそれぞれ 0 個 ,250 個 ,0 個 ,0 個 ,0 個 ,0 個 ,0 個となる。
  • 4 回目の操作で 3 が選ばれる。皿 1, 皿 2,\ldots, 皿 7 に置かれている石の個数はそれぞれ 0 個 ,250 個 ,0 個 ,0 個 ,0 個 ,0 個 ,0 個となる。

すべての操作が終了したときに皿 1, 皿 2 に置かれている石の個数の期待値は 35 、皿 3, 皿 4, 皿 5, 皿 6, 皿 7に置かれている石の個数の期待値は 36 なので、35 35 36 36 36 36 36 を出力してください。


入力例 2

2 1
0 1
1 2

出力例 2

499122177 499122177 

期待値を{}\bmod998244353 で求めることに注意してください。

すべての操作が終了したとき、どちらの皿についても \dfrac12 の確率で石が 1 つ置かれており、\dfrac12 の確率で石が 1 つも置かれていません。 よって、置かれている石の個数の期待値は \dfrac12 です。 499122177\times2\equiv1\pmod{998244353} なので、499122177 499122177 を出力してください。


入力例 3

15 10
61477244 450343304 812961384 836482955 280670539 405068748 318805088 304825858 518212597 316347783 589272551 505875419 944071276 364842194 5376942
2 11
5 9
8 15
6 7
6 8
1 2
1 10
4 9
12 15
6 11

出力例 3

449356308 449356308 449356308 449356308 449356308 648148154 648148154 648148154 648148154 648148154 648148154 643863031 643863031 643863031 643863031 

Score : 500 points

Problem Statement

There are N plates arranged from left to right as plate 1, plate 2,\ldots, plate N. Initially, plate i\ (1\le i\le N) contains A _ i stones.

You will perform M operations on these plates. In the i-th operation (1\le i\le M), two integers L _ i and R _ i are given, and the following operations are performed in order:

  • Remove all stones from the R _ i-L _ i+1 plates: plate L _ i, plate L _ i+1,\ldots, plate R _ i.
  • Uniformly randomly choose an integer between L _ i and R _ i, inclusive, and let it be x.
  • Place all the removed stones on plate x.

For i=1,2,\ldots,N, find the expected number, modulo 998244353, of stones placed on plate i when all M operations are completed.

Finding expected value modulo 998244353

It can be proved that the expected value you seek is always a rational number. Also, under the constraints of this problem, when that value is expressed as an irreducible fraction \frac{P}{Q}, it can be proved that Q \not\equiv 0 \pmod{998244353}. Therefore, there is a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R \lt 998244353. Finding the expected value modulo 998244353 means finding this R.

Constraints

  • 1\le N\le2\times10 ^ 5
  • 1\le M\le2\times10 ^ 5
  • 0\le A _ i\lt998244353\ (1\le i\le N)
  • 1\le L _ i\le R _ i\le N\ (1\le i\le M)
  • All input values are integers.

Input

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

N M
A _ 1 A _ 2 \ldots A _ N
L _ 1 R _ 1
L _ 2 R _ 2
\vdots
L _ M R _ M

Output

Output N integers separated by spaces on a single line. For the i-th (1\leq i\leq N), find the expected number, modulo 998244353, of stones placed on plate i when all M operations are completed, and output it.


Sample Input 1

7 4
30 10 40 10 50 90 20
4 6
5 7
1 6
3 7

Sample Output 1

35 35 36 36 36 36 36 

For example, the operations proceed as follows:

  • In the first operation, 4 is chosen. The number of stones on plates 1, 2,\ldots, 7 becomes 30, 10, 40, 150, 0, 0, 20, respectively.
  • In the second operation, 6 is chosen. The number of stones on plates 1, 2,\ldots, 7 becomes 30, 10, 40, 150, 0, 20, 0, respectively.
  • In the third operation, 2 is chosen. The number of stones on plates 1, 2,\ldots, 7 becomes 0, 250, 0, 0, 0, 0, 0, respectively.
  • In the fourth operation, 3 is chosen. The number of stones on plates 1, 2,\ldots, 7 becomes 0, 250, 0, 0, 0, 0, 0, respectively.

When all operations are completed, the expected number of stones on plates 1, 2 is 35, and the expected number of stones on plates 3, 4, 5, 6, 7 is 36, so output 35 35 36 36 36 36 36.


Sample Input 2

2 1
0 1
1 2

Sample Output 2

499122177 499122177 

Note that you need to find the expected value modulo 998244353.

When all operations are completed, for both plates, there is a \dfrac12 probability that one stone is placed, and a \dfrac12 probability that no stone is placed. Therefore, the expected number of stones placed is \dfrac12. We have 499122177\times2\equiv1\pmod{998244353}, so output 499122177 499122177.


Sample Input 3

15 10
61477244 450343304 812961384 836482955 280670539 405068748 318805088 304825858 518212597 316347783 589272551 505875419 944071276 364842194 5376942
2 11
5 9
8 15
6 7
6 8
1 2
1 10
4 9
12 15
6 11

Sample Output 3

449356308 449356308 449356308 449356308 449356308 648148154 648148154 648148154 648148154 648148154 648148154 643863031 643863031 643863031 643863031