A - LR Constraints

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 枚のカードが左から右に並んでいます。 各カードに 1 以上 K 以下の整数を書き込みます。はじめ、どのカードにも整数は書かれていません。

1 から K の番号がついた K 個の制約が与えられます。 制約 i は文字 c_i と整数 k_i からなります。 c_iL ならば、i が書かれたカードのうち最も にあるものは N 枚のカードのうち左から k_i 番目である必要があります。c_iR ならば、i が書かれたカードのうち最も にあるものは N 枚のカードのうち左から k_i 番目である必要があります。

1 以上 K 以下の各整数 i について、i が書かれたカードが少なくとも 1 つ存在する必要があることに注意してください。

上記の K 個の制約をすべて満たすようなカードへの整数の書き込み方の個数を 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq N,K \leq 1000
  • c_iL, R のいずれか
  • 1 \leq k_i \leq N
  • i \neq j ならば k_i \neq k_j

入力

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

N K
c_1 k_1
\vdots
c_K k_K

出力

問題文中の K 個の制約をすべて満たすようなカードへの整数の書き込み方の個数を 998244353 で割ったあまりを出力せよ。


入力例 1

3 2
L 1
R 2

出力例 1

1
  • 左から 1 番目のカードに 1 を、2 番目のカードに 2 を、3 番目のカードに 1 を書き込むのが 2 つの制約を満たすような唯一の書き込み方です。

入力例 2

30 10
R 6
R 8
R 7
R 25
L 26
L 13
R 14
L 11
L 23
R 30

出力例 2

343921442
  • 998244353 で割ったあまりを求めるのを忘れずに。

Score : 300 points

Problem Statement

We have N cards arranged in a row from left to right. We will write an integer between 1 and K (inclusive) on each of these cards, which are initially blank.

Given are K restrictions numbered 1 through K. Restriction i is composed of a character c_i and an integer k_i. If c_i is L, the k_i-th card from the left in the row must be the leftmost card on which we write i. If c_i is R, the k_i-th card from the left in the row must be the rightmost card on which we write i.

Note that for each integer i from 1 through K, there must be at least one card on which we write i.

Find the number of ways to write integers on the cards under the K restrictions, modulo 998244353.

Constraints

  • 1 \leq N,K \leq 1000
  • c_i is L or R.
  • 1 \leq k_i \leq N
  • k_i \neq k_j if i \neq j.

Input

Input is given from Standard Input in the following format:

N K
c_1 k_1
\vdots
c_K k_K

Output

Find the number of ways to write integers on the cards under the K restrictions in the Problem Statement, modulo 998244353.


Sample Input 1

3 2
L 1
R 2

Sample Output 1

1
  • The only way to meet the two restrictions is to write 1, 2, 1 from left to right on the three cards.

Sample Input 2

30 10
R 6
R 8
R 7
R 25
L 26
L 13
R 14
L 11
L 23
R 30

Sample Output 2

343921442
  • Be sure to find the count modulo 998244353.
B - XOR Matching 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

非負整数のみからなる長さ N の数列 a,b が与えられます。a,bi 番目の要素はそれぞれ a_i, b_i です。

非負整数 x が以下の条件を満たすとき、xよい数 と呼びます。

  • 条件:b を並べ替えて、1 \leq i \leq N を満たすどの整数 i についても a_i \text{ XOR } b_i = x が成立するようにすることができる。ここで、\text{XOR } はビットごとの排他的論理和である。

よい数を小さい方からすべて列挙してください。

\text{ XOR } とは

整数 x, y のビットごとの排他的論理和 x \text{ XOR } y は、以下のように定義されます。

  • x \text{ XOR } y を二進表記した際の 2^k (k \geq 0) の位の数は、x, y を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \text{ XOR } 5 = 6 となります (二進表記すると: 011 \text{ XOR } 101 = 110)。

制約

  • 与えられる入力は全て整数
  • 1 \leq N \leq 2000
  • 0 \leq a_i, b_i < 2^{30}

入力

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

N
a_1 \cdots a_N
b_1 \cdots b_N

出力

1 行目によい数の個数 K を出力せよ。 続けて K 行出力せよ。続く K 行の i 行目には小さい方から i 番目のよい数を出力せよ。


入力例 1

3
1 2 3
6 4 7

出力例 1

1
5
  • b(4, 7, 6) と並び替えたとき、a_1 \text{ XOR } b_1 = a_2 \text{ XOR } b_2 = a_3 \text{ XOR } b_3 = 5 となるため、5 はよい数です。他によい数はありません。

入力例 2

2
0 1
0 2

出力例 2

0

入力例 3

24
14911005 70152939 282809711 965900047 168465665 337027481 520073861 20800623 934711525 944543101 522277111 580736275 468493313 912814743 99651737 439502451 365446123 198473587 285587229 253330309 591640417 761745547 247947767 750367481
805343020 412569406 424258892 329301584 123050452 1042573510 1073384116 495212986 158432830 145726540 623594202 836660574 380872916 722447664 230460104 718360386 620079272 109804454 60321058 38178640 475708360 207775930 393038502 310271010

出力例 3

8
107543995
129376201
139205201
160626723
312334911
323172429
481902037
493346727

Score : 400 points

Problem Statement

Given are sequences a and b, each of length N, consisting of non-negative integers. The i-th elements of a and b are a_i and b_i, respectively.

A non-negative integer x is said to be good when the following condition is satisfied:

  • Condition: It is possible to permute b so that a_i \text{ XOR } b_i = x holds for every integer i such that 1 \leq i \leq N, where \text{XOR } is the bitwise XOR.

List all good integers in ascending order.

What is \mathrm{XOR}?

The bitwise \mathrm{XOR} of integers x and y, x\ \mathrm{XOR}\ y, is defined as follows:

  • When x\ \mathrm{XOR}\ y is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of x and y is 1, and 0 otherwise.
For example, we have 3\ \mathrm{XOR}\ 5 = 6 (in base two: 011\ \mathrm{XOR}\ 101 = 110).

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 2000
  • 0 \leq a_i, b_i < 2^{30}

Input

Input is given from Standard Input in the following format:

N
a_1 \cdots a_N
b_1 \cdots b_N

Output

In the first line, print K, the number of good integers. Then, print K more lines. In the i-th of these K lines, print the i-th smallest good integer.


Sample Input 1

3
1 2 3
6 4 7

Sample Output 1

1
5
  • If we permute b into (4, 7, 6), we have a_1 \text{ XOR } b_1 = a_2 \text{ XOR } b_2 = a_3 \text{ XOR } b_3 = 5, so 5 is a good integer. There are no other good integers.

Sample Input 2

2
0 1
0 2

Sample Output 2

0

Sample Input 3

24
14911005 70152939 282809711 965900047 168465665 337027481 520073861 20800623 934711525 944543101 522277111 580736275 468493313 912814743 99651737 439502451 365446123 198473587 285587229 253330309 591640417 761745547 247947767 750367481
805343020 412569406 424258892 329301584 123050452 1042573510 1073384116 495212986 158432830 145726540 623594202 836660574 380872916 722447664 230460104 718360386 620079272 109804454 60321058 38178640 475708360 207775930 393038502 310271010

Sample Output 3

8
107543995
129376201
139205201
160626723
312334911
323172429
481902037
493346727
C - LCM of GCDs

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

赤い袋と青い袋と N 個のカードパックがあります。はじめどちらの袋も空です。 それぞれのカードパックには整数が書かれた 2 枚のカードが封入されており、i 番目のカードパックに入っているカードにはそれぞれ a_i,b_i が書かれていることがわかっています。

それぞれのカードパックについて、一方のカードを赤い袋に、他方のカードを青い袋に入れます。

カードを袋に入れ終えたのち、赤い袋に入ったカードに書かれた整数全体の最大公約数を X とします。 同様に、青い袋に入ったカードに書かれた整数全体の最大公約数を Y とします。 XY の最小公倍数の値が得点となります。

得点としてありうる値の最大値を求めてください。

制約

  • 与えられる入力は全て整数
  • 1 \leq N \leq 50
  • 1 \leq a_i, b_i \leq 10^9

入力

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

N
a_1 b_1
\vdots
a_N b_N

出力

得点としてありうる値の最大値を出力せよ。


入力例 1

2
2 15
10 6

出力例 1

10
  • 2 が書かれたカードを赤い袋に入れ、15 が書かれたカードを青い袋に入れ、6 が書かれたカードを赤い袋に入れ、10 が書かれたカードを青い袋に入れるのが最適な入れ方の 1 つです。
  • このとき、赤い袋に入ったカードに書かれた整数全体の最大公約数は 2、青い袋に入ったカードに書かれた整数全体の最大公約数は 5 です。
  • このときの得点は 10 です。

入力例 2

5
148834018 644854700
947642099 255192490
35137537 134714230
944287156 528403260
68656286 200621680

出力例 2

238630

入力例 3

20
557057460 31783488
843507940 794587200
640711140 620259584
1901220 499867584
190122000 41414848
349507610 620259584
890404700 609665088
392918800 211889920
507308870 722352000
156850650 498904448
806117280 862969856
193607570 992030080
660673950 422816704
622015810 563434560
207866720 316871744
63057130 117502592
482593010 366954816
605221700 705015552
702500790 900532160
171743540 353470912

出力例 3

152594452160

Score : 500 points

Problem Statement

We have a red bag, a blue bag, and N card packs. Initially, both bags are empty. Each card pack contains two cards with integers written on them. We know that the i-th card pack contains a card with a_i and another with b_i.

For each card pack, we will put one of its cards in the red bag, and the other in the blue bag.

After we put all cards in the bags, let X be the greatest common divisor of all integers written on cards in the red bag. Similarly, let Y be the greatest common divisor of all integers written on cards in the blue bag. Our score will be the least common multiple of X and Y.

Find the maximum possible score.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 50
  • 1 \leq a_i, b_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
\vdots
a_N b_N

Output

Print the maximum possible score.


Sample Input 1

2
2 15
10 6

Sample Output 1

10
  • One optimal move is to put the card with 2 in the red bag, the card with 15 in the blue bag, the card with 6 in the red bag, and the card with 10 in the blue bag.
  • Then, the greatest common divisor of all integers written on cards in the red bag will be 2, and the greatest common divisor of all integers written on cards in the blue bag will be 5.
  • The score here will be 10.

Sample Input 2

5
148834018 644854700
947642099 255192490
35137537 134714230
944287156 528403260
68656286 200621680

Sample Output 2

238630

Sample Input 3

20
557057460 31783488
843507940 794587200
640711140 620259584
1901220 499867584
190122000 41414848
349507610 620259584
890404700 609665088
392918800 211889920
507308870 722352000
156850650 498904448
806117280 862969856
193607570 992030080
660673950 422816704
622015810 563434560
207866720 316871744
63057130 117502592
482593010 366954816
605221700 705015552
702500790 900532160
171743540 353470912

Sample Output 3

152594452160
D - Yet Another Sorting Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

(1,2 \ldots, N+M) を並べ替えて得られる長さ N+M の数列 p が与えられます。 pi 番目の数は p_i です。

あなたは以下の 操作 を何回でも行うことができます。

操作:1 以上 N 以下の整数 n1 以上 M 以下の整数 m を選び、p_{n}p_{N+m} を交換する

p を昇順に並べ替えるために必要な最小の操作回数を求めてください。この問題の制約下で p を昇順に並べ替えることができることが証明できます。

制約

  • 与えられる入力は全て整数
  • 1 \leq N,M \leq 10^5
  • 1 \leq p_i \leq N+M
  • p(1,2 \ldots, N+M) を並べ替えて得られる

入力

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

N M
p_{1} \cdots p_{N+M}

出力

p を昇順に並べ替えるために必要な最小の操作回数を出力せよ。


入力例 1

2 3
1 4 2 5 3

出力例 1

3

入力例 2

5 7
9 7 12 6 1 11 2 10 3 8 4 5

出力例 2

10

Score : 700 points

Problem Statement

Given is a sequence p of length N+M, which is a permutation of (1,2 \ldots, N+M). The i-th term of p is p_i.

You can do the following Operation any number of times.

Operation: Choose an integer n between 1 and N (inclusive), and an integer m between 1 and M (inclusive). Then, swap p_{n} and p_{N+m}.

Find the minimum number of Operations needed to sort p in ascending order. We can prove that it is possible to sort p in ascending order under the Constraints of this problem.

Constraints

  • All values in input are integers.
  • 1 \leq N,M \leq 10^5
  • 1 \leq p_i \leq N+M
  • p is a permutation of (1,2 \ldots, N+M).

Input

Input is given from Standard Input in the following format:

N M
p_{1} \cdots p_{N+M}

Output

Print the minimum number of Operations needed to sort p in ascending order.


Sample Input 1

2 3
1 4 2 5 3

Sample Output 1

3

Sample Input 2

5 7
9 7 12 6 1 11 2 10 3 8 4 5

Sample Output 2

10
E - Pass to Next

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

1, 2, \ldots, N の番号がついた人が円環状に並んでいます。

1 \leq i \leq N-1 を満たす人 i の右隣に人 i+1 がおり、人 N の右隣には人 1 がいます。

i ははじめ、a_i 個のボールを持っています。

以下の処理を一度だけ行います。

  • それぞれの人が現在持っているボールのうちいくつかを選ぶ(0 個でもよい)
  • その後、選んだボールを全て右隣の人に 同時に 渡す。
  • 長さ N の数列を作る。数列の i 番目の要素は人 i が現在持っているボールの個数と等しい値である。

処理の結果できる数列としてありうるものの集合を S とします。 たとえば、a=(1,1,1) のとき S= \{(0,1,2),(0,2,1),(1,0,2),(1,1,1),(1,2,0),(2,0,1),(2,1,0) \} です。

\sum_{x \in S} \prod_{i=1}^{N} x_i998244353 で割ったあまりを計算してください。

制約

  • 与えられる入力は全て整数
  • 3 \leq N \leq 10^5
  • 0 \leq a_i \leq 10^9

入力

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

N
a_{1} a_{2} \cdots a_{N}

出力

\sum_{x \in S} \prod_{i=1}^{N} x_i998244353 で割ったあまりを出力せよ。


入力例 1

3
1 1 1

出力例 1

1
  • S= \{(0,1,2),(0,2,1),(1,0,2),(1,1,1),(1,2,0),(2,0,1),(2,1,0) \} です。
  • \sum_{x \in S} \prod_{i=1}^{N} x_i1 です。

入力例 2

3
2 1 1

出力例 2

6

入力例 3

20
5644 493 8410 8455 7843 9140 3812 2801 3725 6361 2307 1522 1177 844 654 6489 3875 3920 7832 5768

出力例 3

864609205
  • 998244353 で割ったあまりを求めるのを忘れずに。

Score : 800 points

Problem Statement

N people called Person 1, 2, \ldots, N are standing in a circle.

For each 1 \leq i \leq N-1, Person i's neighbor to the right is Person i+1, and Person N's neighbor to the right is Person 1.

Person i initially has a_i balls.

They will do the following procedure just once.

  • Each person chooses some (possibly zero) of the balls they have.
  • Then, each person simultaneously hands the chosen balls to the neighbor to the right.
  • Now, make a sequence of length N, where the i-th term is the number of balls Person i has at the moment.

Let S be the set of all sequences that can result from the procedure. For example, when a=(1,1,1), we have S= \{(0,1,2),(0,2,1),(1,0,2),(1,1,1),(1,2,0),(2,0,1),(2,1,0) \}.

Compute \sum_{x \in S} \prod_{i=1}^{N} x_i, modulo 998244353.

Constraints

  • All values in input are integers.
  • 3 \leq N \leq 10^5
  • 0 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_{1} a_{2} \cdots a_{N}

Output

Print \sum_{x \in S} \prod_{i=1}^{N} x_i, modulo 998244353.


Sample Input 1

3
1 1 1

Sample Output 1

1
  • We have S= \{(0,1,2),(0,2,1),(1,0,2),(1,1,1),(1,2,0),(2,0,1),(2,1,0) \}.
  • \sum_{x \in S} \prod_{i=1}^{N} x_i is 1.

Sample Input 2

3
2 1 1

Sample Output 2

6

Sample Input 3

20
5644 493 8410 8455 7843 9140 3812 2801 3725 6361 2307 1522 1177 844 654 6489 3875 3920 7832 5768

Sample Output 3

864609205
  • Be sure to compute it modulo 998244353.
F - Chance Meeting

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

HW 列のマス目が与えられます。 このマス目の上から i 行目、左から j 列目のマスを (i,j) とします。

はじめ、マス (1,1) にラクダが、マス (H,1) に猫がいます。

あなたは以下の 4 種類の命令を送ることができます。

  • R: (i,j) にいるラクダを (i,j+1) に移動させる
  • D: (i,j) にいるラクダを (i+1,j) に移動させる
  • r: (i,j) にいる猫を (i,j+1) に移動させる
  • u: (i,j) にいる猫を (i-1,j) に移動させる

以下の 4 つの条件全てを満たす命令列を よい命令列 といいます。よい命令列の個数を 998244353 で割ったあまりを求めてください。

  1. ラクダが最終的に (H,W) に到達する
  2. 猫が最終的に (1,W) に到達する
  3. ラクダと猫が命令による移動後、同じマスにいるということが ちょうど 1 回ある
  4. ラクダや猫がマス目から出ることはない

制約

  • 与えられる入力は全て整数
  • 2 \leq H,W \leq 2 \times 10^{5}

入力

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

H W

出力

よい命令列の個数を 998244353 で割ったあまりを出力せよ。


入力例 1

2 2

出力例 1

16
  • 例えば DRurDurRRruDRDru はよい命令列ですが、DRruRRR などはよい命令列ではありません。

入力例 2

200000 200000

出力例 2

412709667
  • 998244353 で割ったあまりを出力するのを忘れずに。

Score : 900 points

Problem Statement

Given is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

Initially, a camel is on (1, 1), and a cat is on (H, 1).

You can send the following four kinds of orders.

  • R: Move the camel on (i, j) to (i, j+1).
  • D: Move the camel on (i, j) to (i+1, j).
  • r: Move the cat on (i, j) to (i, j+1).
  • u: Move the cat on (i, j) to (i-1, j).

A sequence of orders satisfying all of the four conditions below is said to be good. Find the number of good sequences of orders, modulo 998244353.

  1. The final position of the camel will be (H, W).
  2. The final position of the cat will be (1, W).
  3. The following will happen exactly once: the camel and the cat are on the same square after an order is processed.
  4. Neither the camel nor the cat will leave the grid.

Constraints

  • All values in input are integers.
  • 2 \leq H,W \leq 2 \times 10^{5}

Input

Input is given from Standard Input in the following format:

H W

Output

Print the number of good sequences of orders, modulo 998244353.


Sample Input 1

2 2

Sample Output 1

16
  • The good sequences of orders include DRur, DurR, RruD, RDru, but not DRru, RRR.

Sample Input 2

200000 200000

Sample Output 2

412709667
  • Be sure to print the count modulo 998244353.