A - Task Failed Successfully

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

配点 : 100

問題文

高橋君は N 日間のタスク目標を設定しました。

高橋君は i (1 \leq i \leq N) 日目に A_i 個のタスクを完了することを目標としており、実際には B_i 個のタスクを完了しました。

高橋君が目標より多くのタスクを完了した日数を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i, B_i \leq 100
  • 入力される値はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

4
2 8
5 5
5 4
6 7

出力例 1

2

高橋君は 1 日目と 4 日目に目標より多くのタスクを完了しました。


入力例 2

5
1 1
1 1
1 1
1 1
1 1

出力例 2

0

入力例 3

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

出力例 3

3

Score : 100 points

Problem Statement

Takahashi set task goals for N days.

He aimed to complete A_i tasks on day i (1 \leq i \leq N), and actually completed B_i tasks.

Find the number of days when he completed more tasks than his goal.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i, B_i \leq 100
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Output the answer.


Sample Input 1

4
2 8
5 5
5 4
6 7

Sample Output 1

2

Takahashi completed more tasks than his goal on the 1st and 4th days.


Sample Input 2

5
1 1
1 1
1 1
1 1
1 1

Sample Output 2

0

Sample Input 3

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

Sample Output 3

3
B - Precondition

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

配点 : 200

問題文

英小文字および英大文字のみからなる文字列 S, T が与えられます。

文字列 S が以下の条件を満たしているか判定してください。

  • S の先頭でない英大文字の直前の文字はすべて T に含まれる。より形式的には、2 \leq i \leq |S| なる整数 i について Si 番目の文字が英大文字ならば、Si-1 番目の文字は T に含まれる。

制約

  • S, T は長さ 1 以上 100 以下の英小文字および英大文字のみからなる文字列

入力

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

S
T

出力

S が問題文中の条件を満たしているとき Yes と出力せよ。そうでないとき、No と出力せよ。


入力例 1

AtCoder
Total

出力例 1

Yes

S の先頭でない英大文字は 3 番目の文字の C のみです。この直前の文字である tT に含まれているため、Yes と出力すればよいです。


入力例 2

aBCdE
abcdcba

出力例 2

No

S3 番目の文字は英大文字 C であり、その直前の文字は B ですが、BT に含まれていません。


入力例 3

abcde
XYZ

出力例 3

Yes

Score : 200 points

Problem Statement

You are given strings S and T consisting of lowercase and uppercase English letters.

Determine whether the string S satisfies the following condition:

  • Every uppercase letter in S that is not at the beginning is immediately preceded by a character contained in T. More formally, for all integers i such that 2 \leq i \leq |S|, if the i-th character of S is uppercase, then the (i-1)-th character of S is contained in T.

Constraints

  • Each of S and T is a string consisting of lowercase and uppercase English letters with length between 1 and 100, inclusive.

Input

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

S
T

Output

If S satisfies the condition in the problem statement, output Yes. Otherwise, output No.


Sample Input 1

AtCoder
Total

Sample Output 1

Yes

The only uppercase letter in S that is not at the beginning is the 3rd character C. The immediately preceding character t is contained in T, so output Yes.


Sample Input 2

aBCdE
abcdcba

Sample Output 2

No

The 3rd character of S is the uppercase letter C, and its immediately preceding character is B, but B is not contained in T.


Sample Input 3

abcde
XYZ

Sample Output 3

Yes
C - Giant Domino

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

配点 : 300

問題文

1 から N までの番号がついた N 個のドミノがあります。ドミノ i の大きさは S_i です。
いくつかのドミノを左右一列に並べたあとにドミノを倒すことを考えます。ドミノ i が右に向けて倒れる時、ドミノ i のすぐ右に置かれているドミノの大きさが 2 S_i 以下ならばそのドミノも右に向けて倒れます。

あなたは 2 個以上のドミノを選んで左右一列に並べることにしました。ただし、ドミノの並べ方は次の条件を満たす必要があります。 

  • 一番左のドミノはドミノ 1 である。
  • 一番右のドミノはドミノ N である。
  • ドミノ 1 のみを右に向けて倒した時に、最終的にドミノ N も右に向けて倒れる。

条件を満たすドミノの並べ方は存在しますか?また、存在する場合は最小で何個のドミノを並べる必要がありますか?

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

制約

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • 全てのテストケースに対する N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 番目のテストケースを意味する。

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

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

N
S_1 S_2 \dots S_N

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、条件を満たすドミノの並べ方が存在しない場合は -1 を、存在する場合は並べるドミノの最小個数を出力せよ。


入力例 1

3
4
1 3 2 5
2
1 100
10
298077099 766294630 440423914 59187620 725560241 585990757 965580536 623321126 550925214 917827435

出力例 1

4
-1
3

1 番目のテストケースについて、ドミノを左から順にドミノ 1, ドミノ 3, ドミノ 2, ドミノ 4 の順に並べることで問題文の条件を満たすことができます。特に 3 番目の条件については、ドミノ 1 のみを右に向けて倒した時に以下の順にドミノが倒れます。

  • ドミノ 1 の右にはドミノ 3 が置かれている。ドミノ 3 の大きさ S_3 = 2S_1 \times 2 = 1 \times 2 = 2 以下であるから、ドミノ 3 も右に向けて倒れる。
  • ドミノ 3 の右にはドミノ 2 が置かれている。ドミノ 2 の大きさ S_2 = 3S_3 \times 2 = 2 \times 2 = 4 以下であるから、ドミノ 2 も右に向けて倒れる。
  • ドミノ 2 の右にはドミノ 4 が置かれている。ドミノ 4 の大きさ S_4 = 5S_2 \times 2 = 3 \times 2 = 6 以下であるから、ドミノ 4 も右に向けて倒れる。

3 個以下のドミノを並べて問題文の条件を達成することはできないので、答えは 4 個です。

Score : 300 points

Problem Statement

There are N dominoes numbered from 1 to N. The size of domino i is S_i.
Consider arranging some dominoes in a line from left to right and then toppling them. When domino i falls to the right, if the size of the domino placed immediately to the right of domino i is at most 2 S_i, then that domino also falls to the right.

You decided to choose two or more dominoes and arrange them in a line from left to right. The arrangement of dominoes must satisfy the following conditions:

  • The leftmost domino is domino 1.
  • The rightmost domino is domino N.
  • When only domino 1 is toppled to the right, domino N eventually falls to the right as well.

Does an arrangement of dominoes satisfying the conditions exist? If it exists, what is the minimum number of dominoes that need to be arranged?

You are given T test cases, solve the problem for each of them.

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • The sum of N over all test cases is at most 2 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i means the i-th test case:

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

Each test case is given in the following format:

N
S_1 S_2 \dots S_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, if there is no arrangement of dominoes satisfying the conditions, output -1; otherwise, output the minimum number of dominoes to arrange.


Sample Input 1

3
4
1 3 2 5
2
1 100
10
298077099 766294630 440423914 59187620 725560241 585990757 965580536 623321126 550925214 917827435

Sample Output 1

4
-1
3

For the 1st test case, arranging the dominoes from left to right in the order domino 1, domino 3, domino 2, domino 4 satisfies the conditions in the problem statement. Specifically, for the 3rd condition, when only domino 1 is toppled to the right, the dominoes fall in the following order:

  • Domino 3 is placed to the right of domino 1. Since the size of domino 3, S_3 = 2, is not greater than S_1 \times 2 = 1 \times 2 = 2, domino 3 also falls to the right.
  • Domino 2 is placed to the right of domino 3. Since the size of domino 2, S_2 = 3, is not greater than S_3 \times 2 = 2 \times 2 = 4, domino 2 also falls to the right.
  • Domino 4 is placed to the right of domino 2. Since the size of domino 4, S_4 = 5, is not greater than S_2 \times 2 = 3 \times 2 = 6, domino 4 also falls to the right.

It is impossible to achieve the conditions in the problem statement by arranging 3 or fewer dominoes, so the answer is 4.

D - Make 2-Regular Graph

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

配点 : 425

問題文

N 頂点 M 辺の単純無向グラフ G があります。頂点には 1, 2, \ldots, N の番号が付けられており、i 番目の辺は頂点 A_i と頂点 B_i を結ぶ無向辺です。

あなたは、以下の 2 つの操作を好きな順序で好きな回数繰り返すことができます。

  • G に無向辺を 1 つ追加する
  • G から無向辺を 1 つ削除する

G をすべての頂点の次数が 2 である単純無向グラフにするために行う操作回数として考えられる最小値を求めてください。

単純無向グラフとは

単純無向グラフとは、自己ループと多重辺を持たない無向グラフのことを指します。

制約

  • 3 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 入力で与えられるグラフ G は単純無向グラフ
  • 入力される値はすべて整数

入力

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

N M
A_1 B_1 
\vdots
A_M B_M

出力

答えを出力せよ。


入力例 1

5 4
1 2
1 5
2 4
4 5

出力例 1

3

例えば以下のようにすることで、3 回の操作で G をすべての頂点の次数が 2 の単純無向グラフにすることができます。

  • 頂点 2 と頂点 3 を結ぶ辺を追加する。
  • 頂点 2 と頂点 4 を結ぶ辺を削除する。
  • 頂点 3 と頂点 4 を結ぶ辺を追加する。

入力例 2

3 0

出力例 2

3

入力例 3

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

出力例 3

2

入力例 4

8 21
1 4
5 7
8 4
3 4
2 5
8 1
5 1
2 8
2 1
2 4
3 1
6 7
5 8
2 7
6 8
5 4
3 8
7 3
7 8
5 3
7 4

出力例 4

13

Score : 425 points

Problem Statement

There is a simple undirected graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N, and the i-th edge is an undirected edge connecting vertices A_i and B_i.

You can repeat the following two operations in any order and any number of times:

  • Add one undirected edge to G
  • Remove one undirected edge from G

Find the minimum number of operations to make G a simple undirected graph where all vertices have degree 2.

What is a simple undirected graph?

A simple undirected graph refers to an undirected graph that has no self-loops and no multi-edges.

Constraints

  • 3 \leq N \leq 8
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • The graph G given in the input is a simple undirected graph.
  • All input values are integers.

Input

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

N M
A_1 B_1 
\vdots
A_M B_M

Output

Output the answer.


Sample Input 1

5 4
1 2
1 5
2 4
4 5

Sample Output 1

3

For example, the following three operations make G a simple undirected graph where all vertices have degree 2.

  • Add an edge connecting vertices 2 and 3.
  • Remove the edge connecting vertices 2 and 4.
  • Add an edge connecting vertices 3 and 4.

Sample Input 2

3 0

Sample Output 2

3

Sample Input 3

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

Sample Output 3

2

Sample Input 4

8 21
1 4
5 7
8 4
3 4
2 5
8 1
5 1
2 8
2 1
2 4
3 1
6 7
5 8
2 7
6 8
5 4
3 8
7 3
7 8
5 3
7 4

Sample Output 4

13
E - LCM Sequence

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

配点 : 500

問題文

正整数 n について A_n1, 2, \dots, n の最小公倍数として定義します。
正整数 L, R が与えられます。数列 (A_L, A_{L+1}, \dots, A_R) の中には何種類の整数が含まれますか?

制約

  • 1 \leq L \leq R \leq 10^{14}
  • R - L \leq 10^7
  • L, R は整数

入力

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

L R

出力

数列 (A_L, A_{L+1}, \dots, A_R) に含まれる整数の種類数を出力せよ。


入力例 1

4 12

出力例 1

6

A_4 から A_{12} を列挙すると次のようになります。

  • A_4 = 12
  • A_5 = 60
  • A_6 = 60
  • A_7 = 420
  • A_8 = 840
  • A_9 = 2520
  • A_{10} = 2520
  • A_{11} = 27720
  • A_{12} = 27720

よって、(A_4, A_5, \dots, A_{12}) には 6 種類の整数が含まれています。


入力例 2

123456789 123456789

出力例 2

1

入力例 3

99999990000000 100000000000000

出力例 3

310458

Score : 500 points

Problem Statement

For a positive integer n, define A_n as the least common multiple of 1, 2, \dots, n.
You are given positive integers L and R. How many distinct integers are contained in the sequence (A_L, A_{L+1}, \dots, A_R)?

Constraints

  • 1 \leq L \leq R \leq 10^{14}
  • R - L \leq 10^7
  • L and R are integers.

Input

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

L R

Output

Output the number of distinct integers contained in the sequence (A_L, A_{L+1}, \dots, A_R).


Sample Input 1

4 12

Sample Output 1

6

Listing A_4 through A_{12}:

  • A_4 = 12
  • A_5 = 60
  • A_6 = 60
  • A_7 = 420
  • A_8 = 840
  • A_9 = 2520
  • A_{10} = 2520
  • A_{11} = 27720
  • A_{12} = 27720

Thus, (A_4, A_5, \dots, A_{12}) contains six distinct integers.


Sample Input 2

123456789 123456789

Sample Output 2

1

Sample Input 3

99999990000000 100000000000000

Sample Output 3

310458
F - Socks 4

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

配点 : 525

問題文

高橋君のタンスの中には N 色の靴下があり、色 i の靴下は A_i 枚あります。

高橋君ははじめこれらの靴下とは別に色 C の靴下を 1 枚タンスの外に持っており、操作を終える条件を満たすまで以下の操作を繰り返します。

  • タンスから靴下を 1 枚一様ランダムに選んで取り出す。その後、タンスの外に持っている 2 枚の靴下が同じ色ならば操作を終える。そうでないならば、片方の靴下を選んでタンスに戻す。ただし、高橋君は常に今後の靴下を取り出す回数の期待値が最小となるようにタンスに戻す靴下を選ぶ。

操作を終えるまでに靴下を取り出す回数の期待値を \bmod\ 998244353 で求めてください。

期待値を \bmod\ 998244353 で求めるとは

この問題の制約のもとで、期待値は存在し、有理数になることが証明できます。 また、その値を既約分数 \frac{P}{Q} (Q > 0) で表したとき、Q \not\equiv 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 期待値を \bmod\ 998244353 で求めるとは、この R の値を求めることを指します。

制約

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

入力

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

N C
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

3 2
3 1 2

出力例 1

249561092

靴下を取り出す回数の期待値は \frac{15}{4} となります。


入力例 2

8 4
4 1 6 2 5 1 7 3

出力例 2

393623786

Score : 525 points

Problem Statement

In Takahashi's chest of drawers, there are socks of N colors, with A_i socks of color i.

Initially, Takahashi has one sock of color C outside the chest of drawers, separate from these socks, and repeats the following operation until the termination condition is met:

  • Uniformly randomly choose and draw 1 sock from the chest of drawers. Then, if the two socks he has outside the chest of drawers are the same color, terminate the operation. Otherwise, choose one of the socks and put it back in the chest of drawers. He always chooses the sock to put back so as to minimize the expected number of future sock draws.

Find the expected number, modulo 998244353, of sock draws until the operation terminates.

Finding the expected value modulo 998244353

Under the constraints of this problem, it can be proved that the expected value exists and is a rational number. It can also be proved that when this value is expressed as an irreducible fraction \frac{P}{Q} (Q > 0), we have Q \not\equiv 0 \pmod{998244353}. Therefore, there uniquely exists an integer R satisfying R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Finding the expected value modulo 998244353 means finding this value of R.

Constraints

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

Input

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

N C
A_1 A_2 \ldots A_N

Output

Output the answer.


Sample Input 1

3 2
3 1 2

Sample Output 1

249561092

The expected number of sock draws is \frac{15}{4}.


Sample Input 2

8 4
4 1 6 2 5 1 7 3

Sample Output 2

393623786
G - Degree Harmony

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

配点 : 650

問題文

頂点に 1 から N の番号がついた N 頂点 M 辺の単純無向グラフ G が与えられます。i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。
G の全域部分グラフ G' であって次の条件を満たすものを 良いグラフ と呼びます。

  • 1 \leq i \leq N を満たす全ての整数 i について次の条件が成り立つ。
    • d_iG' における頂点 i の次数とする。このとき d_i \leq A_i かつ d_i \bmod 2 = A_i \bmod 2 が成り立つ。

良いグラフが存在するか判定してください。存在する場合は、良いグラフとしてあり得るグラフのうち辺数が最小のものの辺数を出力してください。

制約

  • 1 \leq N \leq 150
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq u_i \lt v_i \leq N
  • 与えられるグラフは単純
  • 1 \leq A_i \leq 150
  • 1 \leq \sum_{i=1}^N A_i \leq 150
  • 入力される値は全て整数

入力

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

N M
A_1 A_2 \dots A_N
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

良いグラフが存在しない場合は -1 を出力せよ。存在する場合は、良いグラフとしてあり得るグラフのうち辺数が最小のものの辺数を出力せよ。


入力例 1

3 3
1 2 3
1 2
1 3
2 3

出力例 1

1

辺集合が 2 番目の辺のみで構成された全域部分グラフは良いグラフです。


入力例 2

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

出力例 2

-1

入力例 3

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

出力例 3

3

Score : 650 points

Problem Statement

You are given a simple undirected graph G with N vertices and M edges, where vertices are numbered from 1 to N. The i-th edge connects vertices u_i and v_i.
A spanning subgraph G' of G that satisfies the following condition is called a good graph:

  • For all integers i satisfying 1 \leq i \leq N, the following condition holds:
    • Let d_i be the degree of vertex i in G'. Then, d_i \leq A_i and d_i \bmod 2 = A_i \bmod 2 hold.

Determine whether a good graph exists. If it exists, output the minimum number of edges among all possible good graphs.

Constraints

  • 1 \leq N \leq 150
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq u_i < v_i \leq N
  • The given graph is simple.
  • 1 \leq A_i \leq 150
  • 1 \leq \sum_{i=1}^N A_i \leq 150
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

If no good graph exists, output -1. If it exists, output the minimum number of edges among all possible good graphs.


Sample Input 1

3 3
1 2 3
1 2
1 3
2 3

Sample Output 1

1

The spanning subgraph whose edge set consists of only the 2nd edge is a good graph.


Sample Input 2

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

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

3