A - Coprime Pair

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 L,R (L < R) が与えられます.

すぬけ君は,以下の条件を両方満たす整数の組 (x,y) を探しています.

  • L \leq x < y \leq R
  • \gcd(x,y)=1

条件を満たす組において,(y-x) がとりうる最大の値を求めてください. なお,問題の制約より,条件を満たす組が少なくとも一つ存在することが証明できます.

制約

  • 1 \leq L < R \leq 10^{18}
  • 入力される値はすべて整数

入力

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

L R

出力

答えを出力せよ.


入力例 1

2 4

出力例 1

1

(x,y)=(2,4) とすると,\gcd(x,y)=2 となってしまい,条件を満たしません. (x,y)=(2,3) とすれば条件を満たし,このとき (y-x) の値は 1 です. (y-x) の値がこれより大きくなることはないため,答えは 1 です.


入力例 2

14 21

出力例 2

5

入力例 3

1 100

出力例 3

99

Score : 300 points

Problem Statement

You are given integers L,R (L < R).

Snuke is looking for a pair of integers (x,y) that satisfy both of the conditions below.

  • L \leq x < y \leq R
  • \gcd(x,y)=1

Find the maximum possible value of (y-x) in a pair that satisfies the conditions. It can be proved that at least one such pair exists under the Constraints.

Constraints

  • 1 \leq L < R \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

L R

Output

Print the answer.


Sample Input 1

2 4

Sample Output 1

1

For (x,y)=(2,4), we have \gcd(x,y)=2, which violates the condition. For (x,y)=(2,3), the conditions are satisfied. Here, the value of (y-x) is 1. There is no such pair with a greater value of (y-x), so the answer is 1.


Sample Input 2

14 21

Sample Output 2

5

Sample Input 3

1 100

Sample Output 3

99
B - Count 1's

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

0,1 からなる長さ N の整数列 A=(A_1,A_2,\cdots,A_N) が与えられます.

あなたはこれから,次の操作をちょうど 1 回行います.

  • A連続する部分列を選び,そこに含まれる要素を flip する.つまり,0 ならば 1 に変え,1 ならば 0 に変える. なお,ここで選ぶ部分列は空であることも許され,その場合 A の要素は全く変化しない.

あなたのスコアは,A に含まれる 1 の個数です. あなたが取るスコアとしてあり得る値が何通りあるか求めてください.

制約

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq A_i \leq 1
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ.


入力例 1

4
0 1 1 0

出力例 1

4

スコアとしてあり得る値は,0,1,2,34 通りです. 例えば,A2 番目から 4 番目までの要素を flip すると,A=(0,0,0,1) となり,スコアは 1 になります.


入力例 2

5
0 0 0 0 0

出力例 2

6

入力例 3

6
0 1 0 1 0 1

出力例 3

3

Score : 400 points

Problem Statement

You are given an integer sequence of length N consisting of 0 and 1: A=(A_1,A_2,\cdots,A_N).

You will do the operation below exactly once.

  • Choose a contiguous subsequence of A and flip the elements in it: convert 0 to 1 and vice versa. Here, you may choose an empty subsequence, in which case the elements of A do not change.

Your score will be the number of 1's in A. How many values are there that your score can take?

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq A_i \leq 1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

4
0 1 1 0

Sample Output 1

4

There are four possible values for your score: 0, 1, 2, 3. For example, if you flip the 2-nd through 4-th elements of A, you will get A=(0,0,0,1), for a score of 1.


Sample Input 2

5
0 0 0 0 0

Sample Output 2

6

Sample Input 3

6
0 1 0 1 0 1

Sample Output 3

3
C - Distinct Numbers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の非負整数列 A=(A_1,A_2,\cdots,A_N) が与えられます. ここで,A の要素はすべて互いに異なります.

Alice と Bob がゲームをします. Alice からはじめて,二人は交互に手番をプレイします. 各手番では,プレイヤーは以下の操作を行います.

  • A の中で最も大きい要素を選び,それをより小さい別の非負整数で置き換える. ただし,操作後も A の要素はすべて互いに異なる必要がある.

先に操作を行えなくなった方の負けです. 両者が最適に行動した時,どちらが勝つか判定してください.

制約

  • 2 \leq N \leq 3 \times 10^5
  • 0 \leq A_1 < A_2 < \cdots < A_N \leq 10^9
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

Alice が勝つ場合は Alice と,Bob が勝つ場合は Bob と出力せよ.


入力例 1

2
2 4

出力例 1

Alice

初手で Alice が行える操作は,40,1,3 のいずれかで置き換えることです. 40 または 1 で置き換えたとすると,次の手番で Bob が行動したあと,Alice が行動不能になり,Alice の負けになります. 一方,43 で置き換えると,その後の Bob の行動に依らず,Alice が勝利できます. よって,この入力では Alice が勝利します.


入力例 2

3
0 1 2

出力例 2

Bob

Score : 600 points

Problem Statement

You are given a non-negative integer sequence of length N: A=(A_1,A_2,\cdots,A_N). Here, all elements of A are pairwise distinct.

Alice and Bob will play a game. They will alternately play a turn, with Alice going first. In each turn, the player does the operation below.

  • Choose the largest element in A at the moment, and replace it with a smaller non-negative integer. Here, all elements in A must still be pairwise distinct after this operation.

The first player to be unable to do the operation loses. Determine the winner when both players play optimally.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 0 \leq A_1 < A_2 < \cdots < A_N \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

If Alice wins, print Alice; if Bob wins, print Bob.


Sample Input 1

2
2 4

Sample Output 1

Alice

In Alice's first turn, she may replace 4 with 0, 1, or 3. If she replaces 4 with 0 or 1, Bob's move in the next turn will make Alice unable to do the operation and lose. On the other hand, if she replaces 4 with 3, she can win regardless of Bob's subsequent moves. Thus, Alice wins in this input.


Sample Input 2

3
0 1 2

Sample Output 2

Bob
D - Prefix XORs

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 700

問題文

長さ N の整数列 A=(A_1,A_2,\cdots,A_N),及び整数 M が与えられます.

k=1,2,\cdots,M について,以下の操作をちょうど k 回行ったあとの A_N の値を求めてください.

  • すべての i (1 \leq i \leq N) について,A_i の値を A_1 \oplus A_2 \oplus \cdots \oplus A_i で置き換える. この置き換えはすべての i に対して同時に行う.

ただしここで,\oplus はビット単位 \mathrm{XOR} 演算を表します.

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR}A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。
一般に k 個の整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 10^6
  • 0 \leq A_i < 2^{30}
  • 入力される値はすべて整数

入力

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

N M
A_1 A_2 \cdots A_N

出力

k に対する答えを空白区切りで出力せよ.


入力例 1

3 2
2 1 3

出力例 1

0 1

操作の度に A は以下のように変化します.

  • 初期状態:A=(2,1,3)
  • 1 回目の操作後:A=(2,3,0)
  • 2 回目の操作後:A=(2,1,1)

入力例 2

10 12
721939838 337089195 171851101 1069204754 348295925 77134863 839878205 89360649 838712948 918594427

出力例 2

716176219 480674244 678890528 642764255 259091950 663009497 942498522 584528336 364872846 145822575 392655861 844652404

Score : 700 points

Problem Statement

You are given an integer sequence of length N: A=(A_1,A_2,\cdots,A_N), and an integer M.

For each k=1,2,\cdots,M, find the value of A_N after doing the operation below k times.

  • For every i (1 \leq i \leq N), simultaneously, replace the value of A_i with A_1 \oplus A_2 \oplus \cdots \oplus A_i.

Here, \oplus denotes bitwise \mathrm{XOR}.

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows:

  • When A \oplus B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110).
Generally, the bitwise \mathrm{XOR} of k integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k). We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots p_k.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 10^6
  • 0 \leq A_i < 2^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \cdots A_N

Output

Print the answers for respective values of k, separated by spaces.


Sample Input 1

3 2
2 1 3

Sample Output 1

0 1

Each operation changes A as follows.

  • Initially: A=(2,1,3).
  • After the first operation: A=(2,3,0).
  • After the second operation: A=(2,1,1).

Sample Input 2

10 12
721939838 337089195 171851101 1069204754 348295925 77134863 839878205 89360649 838712948 918594427

Sample Output 2

716176219 480674244 678890528 642764255 259091950 663009497 942498522 584528336 364872846 145822575 392655861 844652404
E - Bakery

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 800

問題文

すぬけ君はパン屋の経営者です. 彼はこれから N 日間の営業計画を考えています. これらの日を Day 1,2,\cdots,N と呼ぶことにします.

現在,この店にはパン職人が一人もいません. 雇う職人の候補は M 人おり,1 から M までの番号がついています.

職人 i を雇うためには,C_i 円を支払う必要があります. 職人 i を雇った場合,その職人は Day L_i,L_i+1,\cdots,R_i に働き,それぞれの日に 1 つのパンを作ります.

j (1 \leq j \leq N) について,Day j に売れるパンの個数の最大値 A_j が定まっており, Day j に作られたパンの個数を x_j とすると,その日は \min(x_j,A_j) 個のパンが売れます.

パンは一つ売れるごとに D 円の利益が得られます.

すぬけ君は,雇う職人の集合を適切に決めることで,最終的な利益=(売れたパンの個数の合計)\times D-(職人を雇うのに使った費用の合計) を最大化したいです. この最大値を求めてください.

制約

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • 1 \leq D \leq 10^9
  • 1 \leq A_j \leq M
  • 1 \leq L_i \leq R_i \leq N
  • 1 \leq C_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N M D
A_1 A_2 \cdots A_N
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_M R_M C_M

出力

答えを出力せよ.


入力例 1

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

出力例 1

11

職人 1,3,4 を雇えばよいです. この時,職人を雇うのにかかる費用の合計は 7 です. また,Day 1,2,\cdots,N に作られるパンの個数はそれぞれ 1,1,0,1,1,2,1 個です. 売れるパンの個数の合計は 6 であり,最終的な利益は 6 \times 3-7=11 になります.


入力例 2

3 1 5
1 1 1
2 2 10

出力例 2

0

職人を一人も雇わないのが最適です.


入力例 3

10 10 42
6 5 1 5 2 4 2 7 10 9
3 4 4
3 7 136
9 9 14
2 7 152
3 3 33
2 4 100
3 3 38
1 10 28
3 5 66
8 8 15

出力例 3

543

Score : 800 points

Problem Statement

Snuke runs a bakery. He is planning for the next N days. Let us call these days Day 1,2,\cdots,N.

At the moment, the bakery hires nobody. There are M bakers that Snuke can hire, numbered 1 to M.

C_i yen must be paid to hire Baker i. If Baker i is hired, they will work on Day L_i, L_i+1, \cdots, R_i, baking one loaf of bread each day.

For each j (1 \leq j \leq N), there is a limit A_j to the number of loaves of bread that can be sold on Day j. If x_j loaves of bread are baked on Day j, \min(x_j, A_j) loaves will be sold on that day.

Each loaf of bread sold will yield a profit of D yen.

Snuke wants to choose the set of bakers to hire to maximize the final profit: (The total number of loaves sold) \times D - (The total cost of hiring bakers). Find the maximum possible value of this.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • 1 \leq D \leq 10^9
  • 1 \leq A_j \leq M
  • 1 \leq L_i \leq R_i \leq N
  • 1 \leq C_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M D
A_1 A_2 \cdots A_N
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_M R_M C_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

11

Snuke should hire Baker 1, 3, 4. In this case, the total cost to hire bakers is 7, and the number of loaves baked on Day 1, 2, \cdots, N will be 1,1,0,1,1,2,1, respectively. A total of 6 loaves will be sold, for a final profit of 6 \times 3-7=11.


Sample Input 2

3 1 5
1 1 1
2 2 10

Sample Output 2

0

It is optimal to hire no baker.


Sample Input 3

10 10 42
6 5 1 5 2 4 2 7 10 9
3 4 4
3 7 136
9 9 14
2 7 152
3 3 33
2 4 100
3 3 38
1 10 28
3 5 66
8 8 15

Sample Output 3

543
F - Overlaps

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

長さ 1 の棒があります. 棒の左端から距離 x 進んだ点を,座標 x の点と呼ぶことにします.

すぬけ君はこれから N 回,以下の操作を行います.

  • [0,1] の中から一様ランダムに二つの実数 x,y をとる. 座標 \min(x,y) から座標 \max(x,y) までを覆うようなシールを棒に貼る.

なお,すべての乱数は独立であるものとします.

シール同士は重なることがあります. シールが K+1 枚以上重なっている点がない時,これを良い状態と呼ぶことにします.

N 枚のシールを張り終えたあと,良い状態である確率を \text{mod }{998244353} で求めて下さい.

確率 \text{mod }{998244353} の定義

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

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq \min(N,10^5)
  • 入力される値はすべて整数

入力

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

N K

出力

答えを出力せよ.


入力例 1

2 1

出力例 1

332748118

2 枚のシールが重ならない確率を求めればよいです.これは 1/3 になります.


入力例 2

5 3

出力例 2

66549624

入力例 3

10000 5000

出力例 3

642557092

Score : 1100 points

Problem Statement

We have a bar of length 1. A point on the bar whose distance from the left end of the bar is x is said to have a coordinate x.

Snuke will do the operation below N times.

  • Choose two real numbers x and y uniformly at random from [0, 1]. Put a sticker covering the range from the coordinate \min(x,y) to the coordinate \max(x,y).

Here, all random choices are independent of each other.

Stickers can overlap. The bar is said to be good when no point is covered by K+1 or more stickers.

Find the probability, modulo 998244353, of having a good bar after putting N stickers.

Definition of a probability modulo 998244353

It can be proved that the sought probability is always a rational number. Additionally, under the Constraints of this problem, when that value is represented as an irreducible fraction \frac{P}{Q}, it can be proved that Q \not \equiv 0 \pmod{998244353}. Thus, there is a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Report this R.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq \min(N,10^5)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.


Sample Input 1

2 1

Sample Output 1

332748118

We are to find the probability that the two stickers do not overlap, which is 1/3.


Sample Input 2

5 3

Sample Output 2

66549624

Sample Input 3

10000 5000

Sample Output 3

642557092