A - Replace Digits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N の文字列 S と長さ M の文字列 T が与えられます。ここで、 S,T はどちらも 1 から 9 までの数字からなります。

あなたは以下の操作を k=1,2,\ldots,M の順に行います。

  • 1\le i\le N を満たす整数 i を選ぶ。そして、 Si 文字目を Tk 文字目で置き換える。

M 回の操作が終わった後の文字列 S を整数としてみた値の最大値を求めてください。

制約

  • 1\le N,M\le 10^6
  • N,M は整数
  • S1 から 9 までの数字からなる長さ N の文字列
  • T1 から 9 までの数字からなる長さ M の文字列

入力

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

N M
S
T

出力

M 回の操作が終わった後の文字列 S を整数としてみた値の最大値を出力せよ。


入力例 1

3 3
191
325

出力例 1

593

以下の操作方法が最適です。

  • k=1 のとき: i=3 を選ぶ。 S= 193 となる。
  • k=2 のとき: i=1 を選ぶ。 S= 293 となる。
  • k=3 のとき: i=1 を選ぶ。 S= 593 となる。

この場合 S を整数としてみた値は 593 となり、これが最大です。


入力例 2

3 9
191
998244353

出力例 2

993

入力例 3

11 13
31415926535
2718281828459

出力例 3

98888976555

Score : 400 points

Problem Statement

You are given a string S of length N and a string T of length M, both consisting of digits from 1 to 9.

You will perform the following operation for k=1,2,\ldots,M in order:

  • Choose an integer i such that 1 \le i \le N. Then, replace the i-th character of S with the k-th character of T.

Find the maximum possible value of the resulting string S interpreted as an integer after performing the M operations.

Constraints

  • 1 \le N,M \le 10^6
  • N and M are integers.
  • S is a string of length N consisting of digits from 1 through 9.
  • T is a string of length M consisting of digits from 1 through 9.

Input

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

N M
S
T

Output

Print the maximum possible value of the resulting string S interpreted as an integer after performing the M operations.


Sample Input 1

3 3
191
325

Sample Output 1

593

The following sequence of operations is optimal:

  • For k=1: Choose i=3. Then, S = 193.
  • For k=2: Choose i=1. Then, S = 293.
  • For k=3: Choose i=1. Then, S = 593.

In this case, the value of S interpreted as an integer is 593, which is the maximum.


Sample Input 2

3 9
191
998244353

Sample Output 2

993

Sample Input 3

11 13
31415926535
2718281828459

Sample Output 3

98888976555
B - XOR = MOD

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

正整数 N,K が与えられます。 以下の条件を満たす正整数 XN と相性の良い数 と呼びます。

  • XN の排他的論理和は、XN で割ったあまりと等しい。

N と相性の良い数が K 個以上存在するか判定し、存在する場合は N と相性の良い数のうち K 番目に小さい値を求めてください。

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

排他的論理和とは

非負整数 A, B の排他的論理和、A\ \mathrm{XOR}\ B は、以下のように定義されます。

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

制約

  • 1\le T\le 2\times 10^5
  • 1\le N,K\le 10^9
  • 入力される値は全て整数

入力

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

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

ここで、 \text{case}_ii 番目のテストケースを意味する。

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

N K

出力

それぞれのテストケースについて、N と相性の良い数が K 個以上存在するならば K 番目に小さい値を、存在しないならば -1 を出力せよ。


入力例 1

4
2 1
2 2
1 7
20250126 191

出力例 1

2
3
-1
20381694

N=2 の場合について考えます。

  • X=1 のとき、 XN の排他的論理和は 3XN で割ったあまりは 1 です。したがって、 1N と相性の良い数ではありません。
  • X=2 のとき、 XN の排他的論理和は 0XN で割ったあまりは 0 です。したがって、 2N と相性の良い数です。
  • X=3 のとき、 XN の排他的論理和は 1XN で割ったあまりは 1 です。したがって、 3N と相性の良い数です。

よって、 2 と相性の良い数のうち 1 番目に小さい正整数は 22 番目に小さい正整数は 3 です。したがって、 \text{case}_1 の答えは 2\text{case}_2 の答えは 3 です。

Score : 500 points

Problem Statement

You are given two positive integers N and K. A positive integer X is called compatible with N if it satisfies the following condition:

  • The bitwise XOR of X and N is equal to the remainder when X is divided by N.

Determine whether there exist at least K such integers X that are compatible with N. If they do exist, find the K-th smallest such integer.

You are given T test cases; solve each of them.

About XOR

The bitwise XOR of nonnegative integers A and B, denoted A\ \mathrm{XOR}\ B, is defined as follows:

  • In the binary representation of A\ \mathrm{XOR}\ B, the digit in the 2^k place (for k \geq 0) is 1 if and only if exactly one of A and B has a 1 in the 2^k place in its binary representation. Otherwise, it is 0.
For example, 3\ \mathrm{XOR}\ 5 = 6 (in binary: 011\ \mathrm{XOR}\ 101 = 110).

Constraints

  • 1 \le T \le 2\times 10^5
  • 1 \le N,K \le 10^9
  • All input values are integers.

Input

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

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

Here, \text{case}_i denotes the i-th test case.
Each test case is given in the following format:

N K

Output

For each test case, if there exist at least K positive integers that are compatible with N, print the K-th smallest such integer. Otherwise, print -1.


Sample Input 1

4
2 1
2 2
1 7
20250126 191

Sample Output 1

2
3
-1
20381694

Consider the case N=2.

  • When X=1, X\ \mathrm{XOR}\ N = 3 and the remainder of X when divided by N is 1. Therefore, 1 is not compatible with N.
  • When X=2, X\ \mathrm{XOR}\ N = 0 and the remainder of X when divided by N is 0. Therefore, 2 is compatible with N.
  • When X=3, X\ \mathrm{XOR}\ N = 1 and the remainder of X when divided by N is 1. Therefore, 3 is compatible with N.

Hence, among the numbers that are compatible with 2, the smallest is 2 and the second smallest is 3. Therefore, the answer to \text{case}_1 is 2 and the answer to \text{case}_2 is 3.

C - A^n - 1

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

1 以上 10^9 以下の正整数 N が与えられます。

以下の条件を共に満たす正整数の組 (A,M) を一つ求めてください。ただし、制約下でそのような正整数の組が必ず存在することが証明できます。

  • A,M はどちらも 1 以上 10^{18} 以下の正整数である。
  • A^n-1M の倍数となるような正整数 n が存在し、その最小値は N である。

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

制約

  • 1\le T\le 10^4
  • 1\le N\le 10^9
  • 入力される値は全て整数

入力

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

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

ここで、 \text{case}_ii 番目のテストケースを意味する。

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

N

出力

それぞれのケースについて、条件を満たす正整数の組 (A,M) を以下の形式で出力せよ。

A M

条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

4
3
16
1
55

出力例 1

2 7
11 68
20250126 1
33 662

\text{case}_1 について考えます。

例えば (A,M)=(2,7) とすると

  • n=1 のとき: 2^1-1=17 の倍数ではない。
  • n=2 のとき: 2^2-1=37 の倍数ではない。
  • n=3 のとき: 2^3-1=77 の倍数である。

となり、条件を満たす最小の n3 になることが確認できます。したがって、 (A,M)=(2,7) を出力すると正答となります。 この他にも、例えば (A,M)=(100,777) などが条件を満たします。

Score : 600 points

Problem Statement

You are given a positive integer N between 1 and 10^9, inclusive.

Find one pair of positive integers (A, M) satisfying the following conditions. It can be proved that such a pair of integers always exists under the constraints.

  • Both A and M are positive integers between 1 and 10^{18}, inclusive.
  • There exists a positive integer n such that A^n - 1 is a multiple of M, and the smallest such n is N.

You are given T test cases; solve each of them.

Constraints

  • 1 \le T \le 10^4
  • 1 \le N \le 10^9
  • All input values are integers.

Input

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

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

Here, \text{case}_i denotes the i-th test case.
Each test case is given in the following format:

N

Output

For each test case, print a pair of positive integers (A, M) in the following format:

A M

If there are multiple valid solutions, any one of them is considered correct.


Sample Input 1

4
3
16
1
55

Sample Output 1

2 7
11 68
20250126 1
33 662

Consider \text{case}_1.

For example, if we choose (A,M)=(2,7), then:

  • When n=1: 2^1 - 1 = 1 is not a multiple of 7.
  • When n=2: 2^2 - 1 = 3 is not a multiple of 7.
  • When n=3: 2^3 - 1 = 7 is a multiple of 7.

Hence, the smallest n for which A^n - 1 is a multiple of M is 3. Therefore, (A,M)=(2,7) is a correct solution. Other valid solutions include (A,M)=(100,777).

D - Moving Pieces on Graph

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号が付いた N 頂点 M 辺の単純連結無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を双方向に結んでいます。

はじめ、コマ A が頂点 S に、コマ B が頂点 T にあります。ここで、 S,T は入力として与えられます。

あなたは以下の操作を好きな順序で好きな回数行うことができます。

  • コマ A かコマ B のどちらかを選び、そのコマを今の頂点から辺で結ばれた頂点に移動させる。ただし、操作後にコマ A とコマ B が同じ頂点に来るような操作はできない。

あなたの目的はコマ A が頂点 T に、コマ B が頂点 S にある状態にすることです。

あなたが目的を達成することができるか判定し、目的を達成することができる場合は必要な操作回数の最小値を求めてください。

制約

  • 2\le N\le 2\times 10^5
  • \displaystyle N-1\le M\le \min\left(\frac{N(N-1)}2,2\times 10^5 \right)
  • 1\le u_i < v_i\le N
  • 与えられるグラフは単純かつ連結
  • 1\le S,T\le N
  • S\neq T
  • 入力される値は全て整数

入力

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

N M S T
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

目的を達成することができない場合は -1 を出力せよ。

目的を達成することができる場合は必要な操作回数の最小値を出力せよ。


入力例 1

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

出力例 1

3

例えば以下のように操作することで、 3 回の操作で目的を達成することができます。

  • コマ A を頂点 2 に移動させる。
    • コマ A は頂点 2 に、コマ B は頂点 4 にある。
  • コマ B を頂点 3 に移動させる。
    • コマ A は頂点 2 に、コマ B は頂点 3 にある。
  • コマ A を頂点 4 に移動させる。
    • コマ A は頂点 4 に、コマ B は頂点 3 にある。

3 回未満の操作で目的を達成することはできないので、 3 を出力してください。


入力例 2

2 1 1 2
1 2

出力例 2

-1

どのように操作しても目的を達成することはできません。


入力例 3

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

出力例 3

4

Score : 700 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges, where the vertices are numbered 1 to N and the edges are numbered 1 to M. Edge i connects vertex u_i and vertex v_i in both directions.

Initially, there is a piece A on vertex S and a piece B on vertex T. Here, S and T are given as input.

You may perform the following operation any number of times in any order:

  • Choose either piece A or piece B, and move it from its current vertex to an adjacent vertex via an edge. However, you cannot make a move that results in both pieces ending up on the same vertex.

Your goal is to reach the state in which piece A is on vertex T and piece B is on vertex S.

Determine whether this is possible, and if it is, find the minimum number of operations required to achieve it.

Constraints

  • 2 \le N \le 2\times 10^5
  • \displaystyle N-1 \le M \le \min\left(\frac{N(N-1)}{2},\,2\times 10^5\right)
  • 1 \le u_i < v_i \le N
  • The given graph is simple and connected.
  • 1 \le S, T \le N
  • S \neq T
  • All input values are integers.

Input

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

N M S T
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

If it is impossible to achieve the goal, print -1.

If it is possible, print the minimum number of operations required.


Sample Input 1

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

Sample Output 1

3

For example, the following sequence of operations completes the goal in three moves:

  1. Move piece A to vertex 2.
    • Piece A is on vertex 2, piece B is on vertex 4.
  2. Move piece B to vertex 3.
    • Piece A is on vertex 2, piece B is on vertex 3.
  3. Move piece A to vertex 4.
    • Piece A is on vertex 4, piece B is on vertex 3.

It is impossible to complete the goal in fewer than three moves, so print 3.


Sample Input 2

2 1 1 2
1 2

Sample Output 2

-1

No matter how you move the pieces, you cannot achieve the goal.


Sample Input 3

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

Sample Output 3

4
E - Unfair Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 800

問題文

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

N 個の袋があり、それぞれ袋 1 から袋 N までの番号が付けられています。袋 i にははじめ金貨が A_i 枚、銀貨が B_i 枚入っています。

この N 個の袋を使って高橋君と青木君がゲームをすることを考えます。最初に高橋君がいくつかの袋を取り(一つも取らなくても良い)、高橋君が取らなかった袋を全て青木君が取ります。その後、高橋君から始めて 2 人は交互に以下の操作を繰り返します。

  • 自分の持っている袋のうち金貨か銀貨が 1 枚以上入っている袋を選び、選んだ袋に対し以下の 2 つのうちいずれかを行う。
    • 金貨を 1 枚取り除き、銀貨を入れる。ただし、銀貨を入れる枚数は高橋君なら X 枚、青木君なら Y 枚である。この操作は金貨が選んだ袋に 1 枚以上ある場合にのみ行うことができる。
    • 銀貨を 1 枚取り除く。この操作は銀貨が選んだ袋に 1 枚以上ある場合にのみ行うことができる。
  • その後、選んだ袋を相手に渡す。

先に操作ができなくなったプレイヤーが負けとなります。

2 人が自分が勝つために最適な行動を取ったとき、高橋君が勝てるような最初の袋の取り方が何通りあるかを 998244353 で割ったあまりを求めてください。

制約

  • 1\le N\le 2\times 10^5
  • 1\le X,Y\le 10^9
  • 0\le A_i,B_i\le 10^9
  • 入力される値は全て整数

入力

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

N X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

2 人が自分が勝つために最適な行動をしたとしたとき、高橋君が勝てるような最初の袋の取り方が何通りあるかを 998244353 で割ったあまりを出力せよ。


入力例 1

2 1 1
1 0
1 1

出力例 1

2

高橋君が最初に袋 1,2 の両方を取った場合のゲームの進行の一例を示します。

  • 高橋君が袋 2 から金貨を 1 枚取り除き、銀貨を 1 枚入れる。その後、袋 2 を青木君に渡す。
    • 高橋君は金貨が 1 枚入った袋 1 を、青木君は銀貨が 2 枚入った袋 2 を持っている。
  • 青木君が袋 2 から銀貨を 1 枚取り除く。その後、袋 2 を高橋君に渡す。
    • 高橋君は金貨が 1 枚入った袋 1 と銀貨が 1 枚入った袋 2 を持っており、青木君は何も持っていない。
  • 高橋君が袋 1 から金貨を 1 枚取り除き、銀貨を 1 枚入れる。その後、袋 1 を青木君に渡す。
    • 高橋君は銀貨が 1 枚入った袋 2 を持っており、青木君は銀貨が 1 枚入った袋 1 を持っている。
  • 青木君が袋 1 から銀貨を 1 枚取り除く。その後、袋 1 を高橋君に渡す。
    • 高橋君は空の袋 1 と銀貨が 1 枚入った袋 2 を持っており、青木君は何も持っていない。
  • 高橋君が袋 2 から銀貨を 1 枚取り除く。その後、袋 2 を青木君に渡す。
    • 高橋君は空の袋 1 を持っており、青木君は空の袋 2 を持っている。
  • 青木君は操作ができないので、青木君の負けであり、高橋君の勝ちである。

高橋君は最初に取る袋が袋 2 のみであるか袋 1,2 の両方である場合にこのゲームに勝つことができます。したがって、 2 を出力してください。


入力例 2

2 2 1
1 2
1 2

出力例 2

3

高橋君は最初に袋 1,2 のどちらか、または両方取った場合にこのゲームに勝つことができます。


入力例 3

5 8 3
0 0
0 0
0 0
0 0
0 0

出力例 3

0

最初に高橋君がどのように袋を取っても高橋君は負けます。


入力例 4

7 2025 191
1323 9953
2763 3225
2624 5938
6718 2998
3741 7040
9837 1681
8817 4471

出力例 4

40

Score : 800 points

Problem Statement

You are given positive integers N, X, and Y, and two length-N sequences of non-negative integers A = (A_1,A_2,\ldots,A_N) and B = (B_1,B_2,\ldots,B_N).

There are N bags, numbered from 1 to N. Initially, bag i contains A_i gold coins and B_i silver coins.

Consider the following game played by Takahashi and Aoki using these N bags. First, Takahashi takes some of these bags (possibly zero), and Aoki takes all remaining bags. Then, starting with Takahashi, the two players alternate performing the following operation.

  • Choose one bag held by the player with at least one gold coin or silver coin in it, and do one of the following two actions for that bag.
    • Remove one gold coin and add silver coins; the number of silver coins to be added is X for Takahashi and Y for Aoki. This action can only be done if the chosen bag has at least one gold coin.
    • Remove one silver coin. This action can only be done if the chosen bag has at least one silver coin.
  • Then, give the chosen bag to the other player.

The player who cannot perform the operation loses the game.

Find the number, modulo 998244353, of ways Takahashi can initially take bags so that he will win under optimal play by both players.

Constraints

  • 1 \le N \le 2\times 10^5
  • 1 \le X, Y \le 10^9
  • 0 \le A_i, B_i \le 10^9
  • All input values are integers.

Input

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

N X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the number, modulo 998244353, of ways Takahashi can initially take bags so that he will win under optimal play by both players.


Sample Input 1

2 1 1
1 0
1 1

Sample Output 1

2

Consider the case where Takahashi initially takes bags 1 and 2. One possible progression of the game is as follows:

  1. Takahashi chooses bag 2, removes 1 gold coin, and adds 1 silver coin. Then, he gives bag 2 to Aoki.
    • Takahashi holds bag 1 with 1 gold coin. Aoki holds bag 2 with 2 silver coins.
  2. Aoki chooses bag 2 and removes 1 silver coin. Then, gives bag 2 to Takahashi.
    • Takahashi holds bags 1 with 1 gold coin and bag 2 with 1 silver coin. Aoki holds none.
  3. Takahashi chooses bag 1, removes 1 gold coin, and adds 1 silver coin. Then, he gives bag 1 to Aoki.
    • Takahashi holds bag 2 with 1 silver coin. Aoki holds bag 1 with 1 silver coin.
  4. Aoki chooses bag 1, removes 1 silver coin. Then, he gives bag 1 to Takahashi.
    • Takahashi holds bag 1 which is empty and bag 2 with 1 silver coin. Aoki holds none.
  5. Takahashi chooses bag 2 and removes 1 silver coin. Then, he gives bag 2 to Aoki.
    • Takahashi holds bag 1 which is empty. Aoki holds bag 2 which is empty.
  6. Aoki cannot perform the operation, so Aoki loses and Takahashi wins.

Takahashi can win if he initially takes only bag 2, or if he takes both bags 1 and 2. Therefore, the answer is 2.


Sample Input 2

2 2 1
1 2
1 2

Sample Output 2

3

Takahashi wins if he initially takes bag 1, bag 2, or both.


Sample Input 3

5 8 3
0 0
0 0
0 0
0 0
0 0

Sample Output 3

0

No matter how Takahashi chooses the bags initially, he will lose.


Sample Input 4

7 2025 191
1323 9953
2763 3225
2624 5938
6718 2998
3741 7040
9837 1681
8817 4471

Sample Output 4

40