E - Leftover Recipes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

冷蔵庫に N 種類の材料があります。これらを材料 1\dots、材料 N と呼びます。材料 iQ_i グラムあります。

あなたは 2 種類の料理を作れます。料理 A は、1 人分を作るのに各材料 i (1 \leq i \leq N)A_i グラム必要です。料理 B は、1 人分を作るのに各材料 iB_i グラム必要です。どちらも整数人分しか作れません。

冷蔵庫にある材料のみを使って、最大で合計何人分の料理を作れますか。

制約

  • 1 \leq N \leq 10
  • 1 \leq Q_i \leq 10^6
  • 0 \leq A_i \leq 10^6
  • A_i \geq 1 であるような i が存在する。
  • 0 \leq B_i \leq 10^6
  • B_i \geq 1 であるような i が存在する。
  • 入力値はすべて整数である。

入力

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

N
Q_1 Q_2 \dots Q_N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

最大で合計 S 人分の料理を作れるとして、整数 S を出力せよ。


入力例 1

2
800 300
100 100
200 10

出力例 1

5

この冷蔵庫には、800 グラムの材料 1300 グラムの材料 2 があります。

100 グラムの材料 1100 グラムの材料 2 で料理 A を 1 人分作れ、200 グラムの材料 110 グラムの材料 2 で料理 B を 1 人分作れます。

料理 A を 2 人分、料理 B を 3 人分作るのに必要な材料 1 の量は 100 \times 2 + 200 \times 3 = 800 グラム、材料 2 の量は 100 \times 2 + 10 \times 3 = 230 グラムで、いずれも冷蔵庫にある量を超えません。このようにして合計 5 人分の料理を作ることができますが、6 人分を作る方法はなく、答えは 5 です。


入力例 2

2
800 300
100 0
0 10

出力例 2

38

800 グラムの材料 1 で料理 A を 8 人分、300 グラムの材料 2 で料理 B を 30 人分、合計 38 人分作れます。


入力例 3

2
800 300
801 300
800 301

出力例 3

0

何も作れません。


入力例 4

10
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0

出力例 4

222222

Score: 300 points

Problem Statement

Your refrigerator has N kinds of ingredients. Let us call them ingredient 1, \dots, ingredient N. You have Q_i grams of ingredient i.

You can make two types of dishes. To make one serving of dish A, you need A_i grams of each ingredient i (1 \leq i \leq N). To make one serving of dish B, you need B_i grams of each ingredient i. You can only make an integer number of servings of each type of dish.

Using only the ingredients in the refrigerator, what is the maximum total number of servings of dishes you can make?

Constraints

  • 1 \leq N \leq 10
  • 1 \leq Q_i \leq 10^6
  • 0 \leq A_i \leq 10^6
  • There is an i such that A_i \geq 1.
  • 0 \leq B_i \leq 10^6
  • There is an i such that B_i \geq 1.
  • All input values are integers.

Input

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

N
Q_1 Q_2 \dots Q_N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Assuming that you can make a maximum total of S servings of dishes, print the integer S.


Sample Input 1

2
800 300
100 100
200 10

Sample Output 1

5

This refrigerator has 800 grams of ingredient 1 and 300 grams of ingredient 2.

You can make one serving of dish A with 100 grams of ingredient 1 and 100 grams of ingredient 2, and one serving of dish B with 200 grams of ingredient 1 and 10 grams of ingredient 2.

To make two servings of dish A and three servings of dish B, you need 100 \times 2 + 200 \times 3 = 800 grams of ingredient 1, and 100 \times 2 + 10 \times 3 = 230 grams of ingredient 2, neither of which exceeds the amount available in the refrigerator. In this way, you can make a total of five servings of dishes, but there is no way to make six, so the answer is 5.


Sample Input 2

2
800 300
100 0
0 10

Sample Output 2

38

You can make 8 servings of dish A with 800 grams of ingredient 1, and 30 servings of dish B with 300 grams of ingredient 2, for a total of 38 servings.


Sample Input 3

2
800 300
801 300
800 301

Sample Output 3

0

You cannot make any dishes.


Sample Input 4

10
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0

Sample Output 4

222222
F - Path Graph?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, 2, \dots, N の番号が、辺には 1, 2, \dots, M の番号が付けられています。
i \, (i = 1, 2, \dots, M) は頂点 u_i, v_i を結んでいます。

このグラフがパスグラフであるか判定してください。

単純無向グラフとは 単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。

パスグラフとは 頂点に 1, 2, \dots, N の番号が付けられたN 頂点のグラフがパスグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
  • 全ての i = 1, 2, \dots, N-1 に対して、頂点 v_i, v_{i+1} を結ぶ辺が存在する
  • 整数 i, j1 \leq i, j \leq N, |i - j| \geq 2 を満たすならば、頂点 v_i, v_j を結ぶ辺は存在しない

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
  • 入力される値は全て整数
  • 入力で与えられるグラフは単純

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

与えられたグラフがパスグラフなら Yes、そうでないなら No と出力せよ。


入力例 1

4 3
1 3
4 2
3 2

出力例 1

Yes

与えらえたグラフは下図のようであり、これはパスグラフです。

example_00


入力例 2

2 0

出力例 2

No

与えらえたグラフは下図のようであり、これはパスグラフではありません。

example_01


入力例 3

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

出力例 3

No

与えらえたグラフは下図のようであり、これはパスグラフではありません。

example_02

Score : 300 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, 2, \dots, N, and the edges are numbered 1, 2, \dots, M.
Edge i \, (i = 1, 2, \dots, M) connects vertices u_i and v_i.

Determine if this graph is a path graph.

What is a simple undirected graph? A simple undirected graph is a graph without self-loops or multiple edges whose edges do not have a direction.

What is a path graph? A graph with N vertices numbered 1, 2, \dots, N is said to be a path graph if and only if there is a sequence (v_1, v_2, \dots, v_N) that is a permutation of (1, 2, \dots, N) and satisfies the following conditions:
  • For all i = 1, 2, \dots, N-1, there is an edge connecting vertices v_i and v_{i+1}.
  • If integers i and j satisfies 1 \leq i, j \leq N and |i - j| \geq 2, then there is no edge that connects vertices v_i and v_j.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N \, (i = 1, 2, \dots, M)
  • All values in the input are integers.
  • The graph given in the input is simple.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print Yes if the given graph is a path graph; print No otherwise.


Sample Input 1

4 3
1 3
4 2
3 2

Sample Output 1

Yes

Illustrated below is the given graph, which is a path graph.

example_00


Sample Input 2

2 0

Sample Output 2

No

Illustrated below is the given graph, which is not a path graph.

example_01


Sample Input 3

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

Sample Output 3

No

Illustrated below is the given graph, which is not a path graph.

example_02

G - Factorial and Multiple

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

2 以上の整数 K が与えられます。
正の整数 N であって、N!K の倍数となるようなもののうち最小のものを求めてください。

ただし、N!N の階乗を表し、問題の制約下で、そのような N が必ず存在することが証明できます。

制約

  • 2\leq K\leq 10^{12}
  • K は整数

入力

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

K

出力

N!K の倍数となるような最小の正整数 N を出力せよ。


入力例 1

30

出力例 1

5
  • 1!=1
  • 2!=2\times 1=2
  • 3!=3\times 2\times 1=6
  • 4!=4\times 3\times 2\times 1=24
  • 5!=5\times 4\times 3\times 2\times 1=120

より、N!30 の倍数となる最小の正整数 N5 です。よって、5 を出力します。


入力例 2

123456789011

出力例 2

123456789011

入力例 3

280

出力例 3

7

Score : 400 points

Problem Statement

You are given an integer K greater than or equal to 2.
Find the minimum positive integer N such that N! is a multiple of K.

Here, N! denotes the factorial of N. Under the Constraints of this problem, we can prove that such an N always exists.

Constraints

  • 2\leq K\leq 10^{12}
  • K is an integer.

Input

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

K

Output

Print the minimum positive integer N such that N! is a multiple of K.


Sample Input 1

30

Sample Output 1

5
  • 1!=1
  • 2!=2\times 1=2
  • 3!=3\times 2\times 1=6
  • 4!=4\times 3\times 2\times 1=24
  • 5!=5\times 4\times 3\times 2\times 1=120

Therefore, 5 is the minimum positive integer N such that N! is a multiple of 30. Thus, 5 should be printed.


Sample Input 2

123456789011

Sample Output 2

123456789011

Sample Input 3

280

Sample Output 3

7
H - Safety Journey

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

AtCoder国には N 個の都市があり、都市 1 , 都市 2 , \ldots , 都市 N と番号付けられています。 最初、どの 2 つの相異なる都市の間も双方向に通れる道で結ばれていましたが、老朽化が進み、これらのうち M 本の道が使えなくなってしまいました。具体的には 1\leq i \leq M について都市 U_i と都市 V_i を結ぶ道が使えなくなってしまいました。

いま、高橋君は都市 1 で始まり、都市 1 で終わる K 日間の旅をしようと考えました。都市 1 で始まり、都市 1 で終わる K 日間の旅とは、 K+1 個の都市の列 (A_0, A_1, \ldots, A_K) であって、A_0=A_K=1 をみたし、 0\leq i\leq K-1 について A_iA_{i+1} が相異なり、かつ都市 A_i と都市 A_{i+1} が現在も使用可能な道で結ばれているものを指します。

都市 1 で始まり、都市 1 で終わる K 日間の相異なる旅の数を 998244353 で割った余りを出力してください。ただし、 2 つの K 日間の旅 (A_0, A_1, \ldots, A_K)(B_0, B_1, \ldots, B_K) が相異なるとは、ある i が存在して A_i\neq B_i となることを言います。

制約

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • (U_i, V_i) は全て互いに相異なる。
  • 入力は全て整数である。

入力

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

N M K
U_1 V_1
:
U_M V_M

出力

答えを出力せよ。


入力例 1

3 1 4
2 3

出力例 1

4

次のような 4 種類の旅が存在します。

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

これ以外に条件をみたすようなものは無いため、 4 を出力します。


入力例 2

3 3 3
1 2
1 3
2 3

出力例 2

0

使える道が 1 本も残っておらず、条件をみたすような旅は存在しません。


入力例 3

5 3 100
1 2
4 5
2 3

出力例 3

428417047

Score : 500 points

Problem Statement

The Republic of AtCoder has N cities, called City 1, City 2, \ldots, City N. Initially, there was a bidirectional road between every pair of different cities, but M of these roads have become unusable due to deterioration over time. More specifically, for each 1\leq i \leq M, the road connecting City U_i and City V_i has become unusable.

Takahashi will go for a K-day trip that starts and ends in City 1. Formally speaking, a K-day trip that starts and ends in City 1 is a sequence of K+1 cities (A_0, A_1, \ldots, A_K) such that A_0=A_K=1 holds and for each 0\leq i\leq K-1, A_i and A_{i+1} are different and there is still a usable road connecting City A_i and City A_{i+1}.

Print the number of different K-day trips that start and end in City 1, modulo 998244353. Here, two K-day trips (A_0, A_1, \ldots, A_K) and (B_0, B_1, \ldots, B_K) are said to be different when there exists an i such that A_i\neq B_i.

Constraints

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • All pairs (U_i, V_i) are pairwise distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
U_1 V_1
:
U_M V_M

Output

Print the answer.


Sample Input 1

3 1 4
2 3

Sample Output 1

4

There are four different trips as follows.

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

No other trip is valid, so we should print 4.


Sample Input 2

3 3 3
1 2
1 3
2 3

Sample Output 2

0

No road remains usable, so there is no valid trip.


Sample Input 3

5 3 100
1 2
4 5
2 3

Sample Output 3

428417047
I - Takahashi on Grid

Time Limit: 5 sec / Memory Limit: 1024 MiB

配点 : 575

問題文

平面上に無限個のマスがあります。 整数の 2 つ組 (x,y) すべてに対して対応するマスがひとつ存在し、マス (x,y) と呼ぶことにします。

すべてのマスは、それぞれ空きマスか壁マスのどちらか一方です。
長さ N の正整数列 L=(L _ 1,L _ 2,\dotsc,L _ N),U=(U _ 1,U _ 2,\dotsc,U _ N) が与えられます。 ここで、i=1,2,\ldots,N について L _ i,U _ i1\leq L _ i\leq U _ i\leq10 ^ 9 を満たします。
マス (x,y)\ (1\leq x\leq N,L _ x\leq y\leq U _ x) はすべて空きマスで、それ以外のマスは壁マスです。

高橋くんが空きマスであるマス (x,y) にいるとき、次の行動のいずれかを行うことができます。

  • マス (x+1,y) が空きマスならば、マス (x+1,y) に移動する。
  • マス (x-1,y) が空きマスならば、マス (x-1,y) に移動する。
  • マス (x,y+1) が空きマスならば、マス (x,y+1) に移動する。
  • マス (x,y-1) が空きマスならば、マス (x,y-1) に移動する。

どの空きマスどうしも、高橋くんが行動を繰り返すことで行き来できることが保証されます。

次の形式の Q 個の質問に答えてください。

i 番目 (1\leq i\leq Q) の質問では整数の 4 つ組 (s _ {x,i},s _ {y,i},t _ {x,i},t _ {y,i}) が与えられるので、高橋くんがマス (s _ {x,i},s _ {y,i}) にいるところからマス (t _ {x,i},t _ {y,i}) に移動するために必要な行動回数の最小値を求めてください。 各質問について、与えられる 2 つのマスは空きマスであることが保証されます。

制約

  • 1\leq N\leq2\times10 ^ 5
  • 1\leq L _ i\leq U _ i\leq10 ^ 9\ (1\leq i\leq N)
  • \lbrack L _ i,U _ i\rbrack\cap\lbrack L _ {i+1},U _ {i+1}\rbrack\neq\emptyset\ (1\leq i\lt N)
  • 1\leq Q\leq2\times10 ^ 5
  • 1\leq s _ {x,i}\leq N かつ L _ {s _ {x,i}}\leq s _ {y,i}\leq U _ {s _ {x,i}}\ (1\leq i\leq Q)
  • 1\leq t _ {x,i}\leq N かつ L _ {t _ {x,i}}\leq t _ {y,i}\leq U _ {t _ {x,i}}\ (1\leq i\leq Q)
  • 入力はすべて整数

入力

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

N
L _ 1 U _ 1
L _ 2 U _ 2
\vdots
L _ N U _ N
Q
s _ {x,1} s _ {y,1} t _ {x,1} t _ {y,1}
s _ {x,2} s _ {y,2} t _ {x,2} t _ {y,2}
\vdots
s _ {x,Q} s _ {y,Q} t _ {x,Q} t _ {y,Q}

出力

Q 行にわたって出力せよ。 i 行目 (1\leq i\leq Q) には、i 番目の質問に対する答えを出力せよ。


入力例 1

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

出力例 1

10
3
14

与えられたマスは以下のようになります。

1 つめの質問では、例えば以下のように移動することでマス (1,4) からマス (6,3)10 回の行動で移動することができます。

9 回以下の行動でマス (1,4) からマス (6,3) へ移動することはできないため、10 を出力してください。


入力例 2

12
1 1000000000
1000000000 1000000000
1 1000000000
1 1
1 1000000000
1000000000 1000000000
1 1000000000
1 1
1 1000000000
1000000000 1000000000
1 1000000000
1 1
1
1 1 12 1

出力例 2

6000000005

出力すべき値が 32\operatorname{bit} 整数に収まらない場合があることに注意してください。


入力例 3

10
1694 7483
3396 5566
2567 6970
1255 3799
2657 3195
3158 8007
3368 8266
1447 6359
5365 8614
3141 7245
15
3 3911 6 4694
7 5850 10 4641
1 5586 6 4808
2 3401 8 2676
3 3023 6 6923
8 4082 3 6531
6 3216 7 6282
8 5121 8 3459
8 4388 1 6339
6 6001 3 6771
10 5873 8 5780
1 6512 6 6832
8 5345 7 4975
10 4010 8 2355
7 5837 9 6279

出力例 3

2218
1212
4009
1077
3903
4228
3067
1662
4344
6385
95
6959
371
4367
444

Score : 575 points

Problem Statement

There are infinitely many cells on a plane. For every pair of integers (x,y), there is a corresponding cell, which we will call cell (x,y).

Each cell is either an empty cell or a wall cell.
You are given two sequences of positive integers of length N: L=(L _ 1,L _ 2,\dotsc,L _ N) and U=(U _ 1,U _ 2,\dotsc,U _ N). Here, L _ i and U _ i satisfy 1\leq L _ i\leq U _ i\leq10 ^ 9 for i=1,2,\ldots,N.
All cells (x,y)\ (1\leq x\leq N,L _ x\leq y\leq U _ x) are empty cells, and all other cells are wall cells.

When Takahashi is at an empty cell (x,y), he can perform one of the following actions.

  • If cell (x+1,y) is an empty cell, move to cell (x+1,y).
  • If cell (x-1,y) is an empty cell, move to cell (x-1,y).
  • If cell (x,y+1) is an empty cell, move to cell (x,y+1).
  • If cell (x,y-1) is an empty cell, move to cell (x,y-1).

It is guaranteed that he can travel between any two empty cells by repeating his actions.

Answer Q queries in the following format.

For the i-th query (1\leq i\leq Q), you are given a quadruple of integers (s _ {x,i},s _ {y,i},t _ {x,i},t _ {y,i}). Find the minimum number of actions required for Takahashi to move from cell (s _ {x,i},s _ {y,i}) to cell (t _ {x,i},t _ {y,i}). For each query, it is guaranteed that the given two cells are empty cells.

Constraints

  • 1\leq N\leq2\times10 ^ 5
  • 1\leq L _ i\leq U _ i\leq10 ^ 9\ (1\leq i\leq N)
  • \lbrack L _ i,U _ i\rbrack\cap\lbrack L _ {i+1},U _ {i+1}\rbrack\neq\emptyset\ (1\leq i\lt N)
  • 1\leq Q\leq2\times10 ^ 5
  • 1\leq s _ {x,i}\leq N and L _ {s _ {x,i}}\leq s _ {y,i}\leq U _ {s _ {x,i}}\ (1\leq i\leq Q)
  • 1\leq t _ {x,i}\leq N and L _ {t _ {x,i}}\leq t _ {y,i}\leq U _ {t _ {x,i}}\ (1\leq i\leq Q)
  • All input values are integers.

Input

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

N
L _ 1 U _ 1
L _ 2 U _ 2
\vdots
L _ N U _ N
Q
s _ {x,1} s _ {y,1} t _ {x,1} t _ {y,1}
s _ {x,2} s _ {y,2} t _ {x,2} t _ {y,2}
\vdots
s _ {x,Q} s _ {y,Q} t _ {x,Q} t _ {y,Q}

Output

Print Q lines. The i-th line (1\leq i\leq Q) should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

10
3
14

The given cells are as follows.

For the first query, for example, Takahashi can move from cell (1,4) to cell (6,3) in ten actions as follows.

It is impossible to move from cell (1,4) to cell (6,3) in nine or fewer actions, so print 10.


Sample Input 2

12
1 1000000000
1000000000 1000000000
1 1000000000
1 1
1 1000000000
1000000000 1000000000
1 1000000000
1 1
1 1000000000
1000000000 1000000000
1 1000000000
1 1
1
1 1 12 1

Sample Output 2

6000000005

Note that the output value may not fit in a 32-bit integer.


Sample Input 3

10
1694 7483
3396 5566
2567 6970
1255 3799
2657 3195
3158 8007
3368 8266
1447 6359
5365 8614
3141 7245
15
3 3911 6 4694
7 5850 10 4641
1 5586 6 4808
2 3401 8 2676
3 3023 6 6923
8 4082 3 6531
6 3216 7 6282
8 5121 8 3459
8 4388 1 6339
6 6001 3 6771
10 5873 8 5780
1 6512 6 6832
8 5345 7 4975
10 4010 8 2355
7 5837 9 6279

Sample Output 3

2218
1212
4009
1077
3903
4228
3067
1662
4344
6385
95
6959
371
4367
444