A - Simple Math 2

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300

問題文

正整数 N, M が与えられます。\lfloor \frac{10^N}{M} \rfloorM で割った余りを求めてください。

\lfloor x \rfloor について \lfloor x \rfloor は、 x を超えない最大の整数を表します。例としては次のようになります。
  • \lfloor 2.5 \rfloor = 2
  • \lfloor 3 \rfloor = 3
  • \lfloor 9.9999999 \rfloor = 9
  • \lfloor \frac{100}{3} \rfloor = \lfloor 33.33... \rfloor = 33

制約

  • 1 \leq N \leq 10^{18}
  • 1 \leq M \leq 10000

入力

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

N M

出力

答えを出力せよ。


入力例 1

1 2

出力例 1

1

\lfloor \frac{10^1}{2} \rfloor = 5 なので、52 で割った余りの 1 を出力します。


入力例 2

2 7

出力例 2

0

入力例 3

1000000000000000000 9997

出力例 3

9015

Score : 300 points

Problem Statement

Given positive integers N and M, find the remainder when \lfloor \frac{10^N}{M} \rfloor is divided by M.

What is \lfloor x \rfloor? \lfloor x \rfloor denotes the greatest integer not exceeding x. For example:
  • \lfloor 2.5 \rfloor = 2
  • \lfloor 3 \rfloor = 3
  • \lfloor 9.9999999 \rfloor = 9
  • \lfloor \frac{100}{3} \rfloor = \lfloor 33.33... \rfloor = 33

Constraints

  • 1 \leq N \leq 10^{18}
  • 1 \leq M \leq 10000

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.


Sample Input 1

1 2

Sample Output 1

1

We have \lfloor \frac{10^1}{2} \rfloor = 5, so we should print the remainder when 5 is divided by 2, that is, 1.


Sample Input 2

2 7

Sample Output 2

0

Sample Input 3

1000000000000000000 9997

Sample Output 3

9015
B - Reversible Cards

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 400

問題文

1 から N の番号がついた N 枚のカードがあり、各カードの両面には正整数で表される色がついています。

カード i の片面の色は a_i, もう片面の色は b_i です。

各カードについてどちらの面を表にするか自由に選べるとき、表側に見える色の種類数の最大値はいくつになるか求めてください。

制約

  • 1 \leq N \leq 200000
  • 1 \leq a_i,b_i \leq 400000
  • 入力される数はすべて整数

入力

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

N
a_1 b_1
a_2 b_2
:
a_N b_N

出力

答えを出力せよ。


入力例 1

4
1 2
1 3
4 2
2 3

出力例 1

4

それぞれ、1,3,4,2 の側を表にすることで 4 色を達成できます。


入力例 2

2
111 111
111 111

出力例 2

1

そもそも一色しか使われていません。


入力例 3

12
5 2
5 6
1 2
9 7
2 7
5 5
4 2
6 7
2 2
7 8
9 7
1 8

出力例 3

8

Score : 400 points

Problem Statement

We have N cards numbered 1 to N. Each side of each card has a color represented by a positive integer.

One side of Card i has a color a_i, and the other side has a color b_i.

For each card, you can choose which side shows up. Find the maximum possible number of different colors showing up.

Constraints

  • 1 \leq N \leq 200000
  • 1 \leq a_i,b_i \leq 400000
  • All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N

Output

Print the answer.


Sample Input 1

4
1 2
1 3
4 2
2 3

Sample Output 1

4

We can choose the sides with 1, 3, 4, 2 to have four colors.


Sample Input 2

2
111 111
111 111

Sample Output 2

1

They are painted with just one color.


Sample Input 3

12
5 2
5 6
1 2
9 7
2 7
5 5
4 2
6 7
2 2
7 8
9 7
1 8

Sample Output 3

8
C - Too Heavy

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 600

問題文

1 から N の番号がついた N 人の人間と、同じく 1 から N の番号がついた N 個の荷物があります。 人 i の体重は a_i, 荷物 j の重さは b_j です。 はじめ人 i は 荷物 p_i を持っており、以下の操作を 0 回以上繰り返すことで全ての人が自分と同じ番号の荷物を持っている状態にしたいです。

  • i,j (i \neq j) を選び、人 i と人 j の荷物を交換する。

ただし、人は自分の体重以上の重さの荷物を持つと疲れてしまい、その後一切操作には加われません (すなわち、i,jとして選べません)。 特に、 a_i \leq b_{p_i} なら一度も操作に加われません。

条件を満たす操作列があるか判定し、存在するならばそのうち操作回数が最小であるものをひとつ構成してください。

制約

  • 1 \leq N \leq 200000
  • 1 \leq a_i,b_i \leq 10^9
  • p1, \ldots ,N の順列
  • 入力される数はすべて整数

入力

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

N
a_1 a_2 \ldots a_N
b_1 b_2 \ldots b_N
p_1 p_2 \ldots p_N

出力

どのように操作しても条件を満たせない場合、-1 を出力せよ。 条件を満たすことが可能な場合、以下のフォーマットで操作列を出力せよ。

K
i_1 j_1
i_2 j_2
:
i_K j_K

ここで K は操作回数であり、 i_t,j_tt 回目の操作で選んだ人間の番号である。

解が複数存在する場合、どれを出力しても構わない。


入力例 1

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

出力例 1

3
3 4
1 3
4 2

初期状態で人 1,2,3,4 が持っている荷物の重さはそれぞれ 1,3,3,5 なので、初期状態では誰も疲れていません。

まず人 34 で操作をします。人 1,2,3,4 が持っている荷物の重さはそれぞれ 1,3,5,3 なので、まだ誰も疲れていません。

次に人 13 で操作をします。人 1,2,3,4 が持っている荷物の重さはそれぞれ 5,3,1,3 なので、人 1 が疲れます。

最後に人 42 で操作をします。人 1,2,3,4 が持っている荷物の重さはそれぞれ 5,3,1,3 なので、これで新たに疲れる人はいません。

これによって全員が正しい荷物を持っている状態になり、さらにこれは最小の操作回数なので、正しい出力の一つです。


入力例 2

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

出力例 2

-1

条件を満たすように操作することは出来ません。


入力例 3

1
58
998244353
1

出力例 3

0

初期状態で条件を満たしています。

Score : 600 points

Problem Statement

There are N people numbered 1 to N and N pieces of baggage numbered numbered 1 to N. Person i has a weight of a_i, and Baggage j has a weight of b_j. Initially, Person i has Baggage p_i. We want to do the operation below zero or more times so that each Person i will have Baggage i.

  • Choose i,j (i \neq j), and swap the baggages of Person i and Person j.

However, when a person has a piece of baggage with a weight greater than or equal to his/her own weight, the person gets tired and becomes unable to take part in a future operation (that is, we cannot choose him/her as Person i or j). Particularly, if a_i \leq b_{p_i}, Person i cannot take part in any operation.

Determine whether there is a sequence of operations that achieves the objective, and if so, construct one such sequence with the minimum possible number of operations.

Constraints

  • 1 \leq N \leq 200000
  • 1 \leq a_i,b_i \leq 10^9
  • p is a permutation of 1, \ldots, N.
  • All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N
b_1 b_2 \ldots b_N
p_1 p_2 \ldots p_N

Output

If no sequence of operations achieves the objective, print -1. Otherwise, print a sequence of operations in the following format:

K
i_1 j_1
i_2 j_2
:
i_K j_K

Here, K is the number of operations, and i_t, j_t represent the people chosen in the t-th operation.

If there are multiple solutions, any of them will be accepted.


Sample Input 1

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

Sample Output 1

3
3 4
1 3
4 2

Initially, People 1, 2, 3, 4 has pieces of baggage with weights 1, 3, 3, 5, respectively, so no one is tired.

First, we do the operation choosing Person 3 and 4. Now, people 1, 2, 3, 4 has pieces of baggage with weights 1, 3, 5, 3, so no one is tired yet.

Second, we do the operation choosing Person 1 and 3. Now, people 1, 2, 3, 4 has pieces of baggage with weights 5, 3, 1, 3, so Person 1 gets tired.

Lastly, we do the operation choosing Person 4 and 2. Now, people 1, 2, 3, 4 has pieces of baggage with weights 5, 3, 1, 3, so no one gets tired.

This sequence of operations has made everyone have the right piece of baggage, with the minimum possible number of operations, so it is valid.


Sample Input 2

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

Sample Output 2

-1

No sequence of operations achieves the objective.


Sample Input 3

1
58
998244353
1

Sample Output 3

0

The objective is already achieved in the initial state.

D - Orientation

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の単純な無向グラフが与えられます。頂点には 1, \cdots, N の番号がついています。i 番目の辺は頂点 a_i, b_i を繋いでいます。 また、正整数列 c_1, c_2, \cdots, c_N も与えられます。

このグラフを、次の条件を満たすように有向グラフに変換してください。つまり、各 i について無向辺 (a_i, b_i) を削除し、有向辺 a_i \to b_ib_i \to a_i のどちらか 1 つを張ってください。

  • 全ての i = 1, 2, \cdots, N について、頂点 i から(有向辺を好きな回数使うことで)到達可能な頂点数がちょうど c_i 個。なお、頂点 i 自身も 1 個と数える。

なお、この問題では、解が存在するような入力のみが与えられます。

制約

  • 1 \leq N \leq 100
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq a_i, b_i \leq N
  • 与えられるグラフに自己ループや多重辺は存在しない
  • 1 \leq c_i \leq N
  • 必ず題意を満たす解が存在する

入力

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

N M
a_1 b_1
:
a_M b_M
c_1 c_2 ... c_N

出力

M 行出力せよ。

i 行目には、i 番目の辺について a_i \to b_i に辺を張りたい場合 ->a_i \gets b_i に辺を張りたい場合 <- を出力せよ。

解が複数存在する場合、どれを出力しても構わない。


入力例 1

3 3
1 2
2 3
3 1
3 3 3

出力例 1

->
->
->

長さ 3 のサイクルでは、どの頂点からも全ての頂点に到達できます。


入力例 2

3 2
1 2
2 3
1 2 3

出力例 2

<-
<-

入力例 3

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

出力例 3

<-
->
->

グラフは非連結のこともあります。

Score : 600 points

Problem Statement

Given is a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \cdots, N, and the i-th edge connects Vertices a_i and b_i. Also given is a sequence of positive integers: c_1, c_2, \cdots, c_N.

Convert this graph into a directed graph that satisfies the condition below, that is, for each i, delete the undirected edge (a_i, b_i) and add one of the two direted edges a_i \to b_i and b_i \to a_i.

  • For every i = 1, 2, \cdots, N, there are exactly c_i vertices reachable from Vertex i (by traversing some number of directed edges), including Vertex i itself.

In this problem, it is guaranteed that the given input always has a solution.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq a_i, b_i \leq N
  • The given graph has no self-loops and no multi-edges.
  • 1 \leq c_i \leq N
  • There always exists a valid solution.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M
c_1 c_2 ... c_N

Output

Print M lines.

To add the edge a_i \to b_i for the i-th edge, print -> in the i-th line; to add the edge b_i \to a_i for the i-th edge, print <-.

If there are multiple solutions, any of them will be accepted.


Sample Input 1

3 3
1 2
2 3
3 1
3 3 3

Sample Output 1

->
->
->

In a cycle of length 3, you can reach every vertex from any vertex.


Sample Input 2

3 2
1 2
2 3
1 2 3

Sample Output 2

<-
<-

Sample Input 3

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

Sample Output 3

<-
->
->

The graph may be disconnected.

E - Simple Math 3

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 800

問題文

この問題では一つの入力につきテストケースが T 個与えられます。

整数 A, B, C, D が与えられます。次の条件を満たす正整数 i はいくつあるか求めてください。

  • A + B \times i 以上 A + C \times i 以下の整数はいずれも、D の倍数ではない。

なお、制約より答えが有限であることが証明できます。

制約

  • 1 \leq T \leq 10{,}000
  • 1 \leq A < D
  • 0 \leq B < C < D
  • 2 \leq D \leq 10^8

入力

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

T
A_1 B_1 C_1 D_1
:
A_T B_T C_T D_T

出力

T 行出力せよ。

i 行目には、i ケース目 (A_i, B_i, C_i, D_i) の答えを出力せよ。


入力例 1

2
3 1 2 5
99 101 103 105

出力例 1

1
25

1 ケース目の (A + B \times i, A + C \times i) を列挙すると次のようになります。 i = 3 のみ条件を満たすことがわかります。

  • i = 1: (4, 5)
  • i = 2: (5, 7)
  • i = 3: (6, 9)
  • i = 4: (7, 11)
  • i = 5: (8, 13)
  • :

Score : 800 points

Problem Statement

In this problem, you will be given T test cases for each input.

Given integers A, B, C, and D, find the number of positive integers i satisfying the following condition:

  • None of the integers between A + B \times i and A + C \times i (inclusive) is a multiple of D.

We can prove from the constraints that the count is finite.

Constraints

  • 1 \leq T \leq 10{,}000
  • 1 \leq A < D
  • 0 \leq B < C < D
  • 2 \leq D \leq 10^8

Input

Input is given from Standard Input in the following format:

T
A_1 B_1 C_1 D_1
:
A_T B_T C_T D_T

Output

Print T lines.

The i-th line should contain the answer for the i-th case (A_i, B_i, C_i, D_i).


Sample Input 1

2
3 1 2 5
99 101 103 105

Sample Output 1

1
25

The pairs (A + B \times i, A + C \times i) for the first case are listed below. We can see that only i = 3 satisfies the condition.

  • i = 1: (4, 5)
  • i = 2: (5, 7)
  • i = 3: (6, 9)
  • i = 4: (7, 11)
  • i = 5: (8, 13)
  • :
F - Do you like query problems?

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 1000

問題文

yosupoくんはクエリの問題が大好きなので、次のような問題を作りました。


A Query Problem

長さ N の整数列 a_1,\ldots,a_N があります。はじめは a_i = 0 (1 \leq i \leq N) です。 また、ans という変数があり、はじめは ans = 0 です。 ここに、次の形式のクエリが Q 個来ます。

  • クエリ1:

    • t_i (=1) l_i r_i v_i

    • j = l_i,\ldots,r_i に対して、a_j := \min(a_j,v_i)

  • クエリ2:

    • t_i (=2) l_i r_i v_i

    • j = l_i,\ldots,r_i に対して、a_j := \max(a_j,v_i)

  • クエリ3:

    • t_i (=3) l_i r_i

    • a_{l_i} + \ldots + a_{r_i} を計算して、ans に足す

最終的な ans の値を出力してください。

ただし、各クエリについて、1 \leq l_i \leq r_i \leq N が、さらにクエリ1,2については 0 \leq v_i \leq M-1 が成立する。


この問題を見たmaroonくんは簡単すぎると感じたため、次の問題を考えました。


Query Problems

正整数 N,M,Q が与えられます。問題 "A Query Problem" に対する入力は ( \frac{(N+1)N}{2} \cdot (M+M+1) )^Q 通りありますが、それらに対する出力のすべての和を 998{,}244{,}353 で割った余りを求めてください。


求めてください。

制約

  • 1 \leq N,M,Q \leq 200000
  • 入力される数はすべて整数

入力

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

N M Q

出力

答えを出力せよ。


入力例 1

1 2 2

出力例 1

1

ありうる入力は 25 通りありますが、そのうち ans が正になるような入力は、次の一通りです:

t_1 = 2, l_1 = 1, r_1 = 1, v_1 = 1, t_2 = 3, l_2 = 1, r_2 = 1

このとき ans1 になるので、答えは 1 です。


入力例 2

3 1 4

出力例 2

0

入力例 3

111 112 113

出力例 3

451848306

Score : 1000 points

Problem Statement

Yosupo loves query problems. He made the following problem:


A Query Problem

We have an integer sequence of length N: a_1,\ldots,a_N. Initially, a_i = 0 (1 \leq i \leq N). We also have a variable ans, which is initially 0. Here, you will be given Q queries of the following forms:

  • Type 1:

    • t_i (=1) l_i r_i v_i

    • For each j = l_i,\ldots,r_i, a_j := \min(a_j,v_i).

  • Type 2:

    • t_i (=2) l_i r_i v_i

    • For each j = l_i,\ldots,r_i, a_j := \max(a_j,v_i).

  • Type 3:

    • t_i (=3) l_i r_i

    • Compute a_{l_i} + \ldots + a_{r_i} and add the result to ans.

Print the final value of ans.

Here, for each query, 1 \leq l_i \leq r_i \leq N holds. Additionally, for Type 1 and 2, 0 \leq v_i \leq M-1 holds.


Maroon saw this problem, thought it was too easy, and came up with the following problem:


Query Problems

Given are positive integers N,M,Q. There are ( \frac{(N+1)N}{2} \cdot (M+M+1) )^Q valid inputs for "A Query Problem". Find the sum of outputs over all those inputs, modulo 998{,}244{,}353.


Find it.

Constraints

  • 1 \leq N,M,Q \leq 200000
  • All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q

Output

Print the answer.


Sample Input 1

1 2 2

Sample Output 1

1

There are 25 valid inputs, and just one of them - shown below - results in a positive value of ans.

t_1 = 2, l_1 = 1, r_1 = 1, v_1 = 1, t_2 = 3, l_2 = 1, r_2 = 1

ans will be 1 in this case, so the answer is 1.


Sample Input 2

3 1 4

Sample Output 2

0

Sample Input 3

111 112 113

Sample Output 3

451848306