A - Generalized ABC

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

整数 K が与えられます。

英大文字を A から昇順に K 個繋げて得られる文字列を答えてください。

制約

  • K1 以上 26 以下の整数

入力

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

K

出力

答えを出力せよ。


入力例 1

3

出力例 1

ABC

英大文字は A から昇順に A, B, C, ... です。

A から昇順に 3 個繋げて得られる文字列は ABC です。


入力例 2

1

出力例 2

A

Score : 100 points

Problem Statement

You are given an integer K.

Print a string that is a concatenation of the first K uppercase English letters in ascending order, starting from A.

Constraints

  • K is an integer between 1 and 26, inclusive.

Input

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

K

Output

Print the answer.


Sample Input 1

3

Sample Output 1

ABC

The uppercase English letters in ascending order are A, B, C, ...

By concatenating the first three uppercase English letters, we get ABC.


Sample Input 2

1

Sample Output 2

A
B - Let's Get a Perfect Score

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1 から N までの番号がついた N 人の参加者が、1 から M までの番号がついた M 問からなるコンテストに参加します。

1 以上 N 以下の整数 i1 以上 M 以下の整数 j について、S_ij 番目の文字が o のとき参加者 i は問題 j を解くことが可能で、S_ij 番目の文字が x のとき参加者 i は問題 j を解くことが不可能です。

このコンテストは、二人の参加者でペアを組んで参加します。二人が協力することで M 問全てを解くことが可能であるようなペアの個数を答えてください。

より厳密には、1\leq x < y\leq N を満たす整数の組 (x,y) であって、 1 以上 M 以下の任意の整数 j について、参加者 x か参加者 y の少なくとも一方は問題 j を解くことが可能であるという条件を満たすものの個数を答えてください。

制約

  • N2 以上 30 以下の整数
  • M1 以上 30 以下の整数
  • S_io, x からなる長さ M の文字列

入力

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

N M
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5 5
ooooo
oooxx
xxooo
oxoxo
xxxxx

出力例 1

5

参加者 12 のペア、参加者 13 のペア、参加者 14 のペア、参加者 15 のペア、参加者 23 のペアの 5 個のペアが条件を満たします。

例えば参加者 24 のペアは、問題 4 が解けないので条件を満たしません。


入力例 2

3 2
ox
xo
xx

出力例 2

1

入力例 3

2 4
xxxx
oxox

出力例 3

0

Score : 200 points

Problem Statement

N participants, numbered 1 to N, will participate in a contest with M problems, numbered 1 to M.

For an integer i between 1 and N and an integer j between 1 and M, participant i can solve problem j if the j-th character of S_i is o, and cannot solve it if that character is x.

The participants must be in pairs. Print the number of ways to form a pair of participants who can collectively solve all the M problems.

More formally, print the number of pairs (x,y) of integers satisfying 1\leq x < y\leq N such that for any integer j between 1 and M, at least one of participant x and participant y can solve problem j.

Constraints

  • N is an integer between 2 and 30, inclusive.
  • M is an integer between 1 and 30, inclusive.
  • S_i is a string of length M consisting of o and x.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5 5
ooooo
oooxx
xxooo
oxoxo
xxxxx

Sample Output 1

5

The following five pairs satisfy the condition: participants 1 and 2, participants 1 and 3, participants 1 and 4, participants 1 and 5, and participants 2 and 3.

On the other hand, the pair of participants 2 and 4, for instance, does not satisfy the condition because they cannot solve problem 4.


Sample Input 2

3 2
ox
xo
xx

Sample Output 2

1

Sample Input 3

2 4
xxxx
oxox

Sample Output 3

0
C - String Delimiter

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

英小文字、," からなる長さ N の文字列 S が与えられます。S に含まれる " の個数は偶数であることが保証されています。

S に含まれる " の個数を 2K 個とすると、各 i=1,2,\ldots,K について 2i-1 番目の " から 2i 番目の " までの文字のことを 括られた文字 と呼びます。

あなたの仕事は、 S に含まれる , のうち、括られた文字 でないもの. で置き換えて得られる文字列を答えることです。

制約

  • N1 以上 2\times 10^5 以下の整数
  • S は英小文字、," からなる長さ N の文字列
  • S に含まれる " の個数は偶数

入力

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

N
S

出力

答えを出力せよ。


入力例 1

8
"a,b"c,d

出力例 1

"a,b"c.d

S のうち "a,b" が括られた文字であり、c,d は括られた文字ではありません。

S に含まれる , のうち、括られた文字でないのは S の左から 7 番目の文字なので、7 番目の文字を . で置き換えたものが答えとなります。


入力例 2

5
,,,,,

出力例 2

.....

入力例 3

20
a,"t,"c,"o,"d,"e,"r,

出力例 3

a."t,"c."o,"d."e,"r.

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, ,, and ". It is guaranteed that S contains an even number of ".

Let 2K be the number of " in S. For each i=1,2,\ldots,K, the characters from the (2i-1)-th " through the (2i)-th " are said to be enclosed.

Your task is to replace each , in S that is not an enclosed character with . and print the resulting string.

Constraints

  • N is an integer between 1 and 2\times 10^5, inclusive.
  • S is a string of length N consisting of lowercase English letters, ,, and ".
  • S contains an even number of ".

Input

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

N
S

Output

Print the answer.


Sample Input 1

8
"a,b"c,d

Sample Output 1

"a,b"c.d

In S, "a,b" are enclosed characters, and c,d are not.

The , in S that is not an enclosed character is the seventh character from the left in S, so replace that character with . to get the answer.


Sample Input 2

5
,,,,,

Sample Output 2

.....

Sample Input 3

20
a,"t,"c,"o,"d,"e,"r,

Sample Output 3

a."t,"c."o,"d."e,"r.
D - Make Bipartite 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N 個の頂点と M 本の辺からなる単純な(すなわち、自己ループも多重辺も含まない)無向グラフ G が与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。

1 \leq u \lt v \leq N を満たす整数の組 (u, v) であって、下記の 2 つの条件をともに満たすものの個数を出力してください。

  • グラフ G において、頂点 u と頂点 v を結ぶ辺は存在しない。
  • グラフ G に、頂点 u と頂点 v を結ぶ辺を追加して得られるグラフは、二部グラフである。
二部グラフとは?

無向グラフが二部グラフであるとは、下記の条件を満たすように各頂点を黒または白のどちらかの色で塗ることができることを言います。

  • 同じ色に塗られた頂点どうしを結ぶ辺は存在しない。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \lbrace 2 \times 10^5, N(N-1)/2 \rbrace
  • 1 \leq u_i, v_i \leq N
  • グラフ G は単純
  • 入力はすべて整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

5 4
4 2
3 1
5 2
3 2

出力例 1

2

問題文中の条件を満たす整数の組 (u, v) は、(1, 4)(1, 5)2 つです。よって、2 を出力します。
他の組については、例えば、(1, 3) はグラフ G において頂点 1 と頂点 3 を結ぶ辺が存在することから、 (4, 5) はグラフ G に頂点 4 と頂点 5 を結ぶ辺を追加して得られるグラフが二部グラフではないことから、 それぞれ問題文中の条件を満たしません。


入力例 2

4 3
3 1
3 2
1 2

出力例 2

0

与えられるグラフが二部グラフであったり連結であるとは限らないことに注意してください。


入力例 3

9 11
4 9
9 1
8 2
8 3
9 2
8 4
6 7
4 6
7 5
4 5
7 8

出力例 3

9

Score : 400 points

Problem Statement

You are given a simple undirected graph G with N vertices and M edges (a simple graph does not contain self-loops or multi-edges). For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i.

Print the number of pairs of integers (u, v) that satisfy 1 \leq u \lt v \leq N and both of the following conditions.

  • The graph G does not have an edge connecting vertex u and vertex v.
  • Adding an edge connecting vertex u and vertex v in the graph G results in a bipartite graph.
What is a bipartite graph?

An undirected graph is said to be bipartite if and only if one can paint each vertex black or white to satisfy the following condition.

  • No edge connects vertices painted in the same color.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \lbrace 2 \times 10^5, N(N-1)/2 \rbrace
  • 1 \leq u_i, v_i \leq N
  • The graph G is simple.
  • All values in the input are integers.

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 the answer.


Sample Input 1

5 4
4 2
3 1
5 2
3 2

Sample Output 1

2

We have two pairs of integers (u, v) that satisfy the conditions in the problem statement: (1, 4) and (1, 5). Thus, you should print 2.
The other pairs do not satisfy the conditions. For instance, for (1, 3), the graph G has an edge connecting vertex 1 and vertex 3; for (4, 5), adding an edge connecting vertex 4 and vertex 5 in the graph G does not result in a bipartite graph.


Sample Input 2

4 3
3 1
3 2
1 2

Sample Output 2

0

Note that the given graph may not be bipartite or connected.


Sample Input 3

9 11
4 9
9 1
8 2
8 3
9 2
8 4
6 7
4 6
7 5
4 5
7 8

Sample Output 3

9
E - Choose Two and Eat One

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

箱の中に N 個のボールが入っており、各ボールには 1 以上 M-1 以下の整数が書かれています。 i = 1, 2, \ldots, N について、i 番目のボールに書かれた整数は A_i です。

高橋君は、箱の中に 2 個以上のボールが残っている限り、下記の行動を繰り返します。

  • まず、箱の中から 2 つのボールを任意に選んで取り出す。
  • 次に、取り出した 2 つのボールに書かれた整数をそれぞれ x, y とおき、x^y + y^xM で割ったあまりに等しい得点を獲得する。
  • その後、取り出した 2 つのボールのうち、任意に選んだ一方を食べ、もう一方を箱の中に戻す。

高橋君が獲得する合計得点としてあり得る最大値を出力してください。

制約

  • 2 \leq N \leq 500
  • 2 \leq M \leq 10^9
  • 1 \leq A_i \leq M-1
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 10
4 2 3 2

出力例 1

20

高橋君が下記の通りに行動する場合を考えます。以下、X \bmod Y で 非負整数 X を正整数 Y で割ったあまりを表します。

  1. 箱の中から 1 番目のボールと 3 番目のボールを取り出し、(4^3 + 3^4) \bmod 10 = 5 点を獲得します。その後、1 番目のボールを食べ、3 番目のボールを箱の中に戻します。その結果、箱の中には 2, 3, 4 番目のボールが残ります。
  2. 箱の中から 3 番目のボールと 4 番目のボールを取り出し、(3^2 + 2^3) \bmod 10 = 7 点を獲得します。その後、3 番目のボールを食べ、4 番目のボールを箱の中に戻します。その結果、箱の中には 2, 4 番目のボールが残ります。
  3. 箱の中から 2 番目のボールと 4 番目のボールを取り出し、(2^2 + 2^2) \bmod 10 = 8 点を獲得します。その後、2 番目のボールを食べ、4 番目のボールを箱の中に戻します。その結果、箱の中には 4 番目のボールのみが残ります。

このとき、高橋君が獲得する合計得点は 5 + 7 + 8 = 20 点であり、これがあり得る最大値です。


入力例 2

20 100
29 31 68 20 83 66 23 84 69 96 41 61 83 37 52 71 18 55 40 8

出力例 2

1733

Score : 500 points

Problem Statement

A box contains N balls, each with an integer between 1 and M-1 written on it. For i = 1, 2, \ldots, N, the integer written on the i-th ball is A_i.

While the box has two or more balls remaining, Takahashi will repeat the following.

  • First, choose two balls arbitrarily.
  • Then, get a score equal to the remainder when x^y + y^x is divided by M, where x and y are the integers written on the two balls.
  • Finally, choose one of the two balls arbitrarily, eat it, and return the other to the box.

Print the maximum possible total score Takahashi will get.

Constraints

  • 2 \leq N \leq 500
  • 2 \leq M \leq 10^9
  • 1 \leq A_i \leq M-1
  • All values in the input are integers.

Input

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

N M
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4 10
4 2 3 2

Sample Output 1

20

Consider the following scenario. Below, X \bmod Y denotes the remainder when a non-negative integer X is divided by a positive integer Y.

  1. Take the first and third balls from the box to score (4^3 + 3^4) \bmod 10 = 5 points. Then, eat the first ball and return the third to the box. Now, the box has the second, third, and fourth balls.
  2. Take the third and fourth balls from the box to score (3^2 + 2^3) \bmod 10 = 7 points. Then, eat the third ball and return the fourth to the box. Now, the box has the second and fourth balls.
  3. Take the second and fourth balls from the box to score (2^2 + 2^2) \bmod 10 = 8 points. Then, eat the second ball and return the fourth to the box. Now, the box has just the fourth ball.

Here, Takahashi scores a total of 5 + 7 + 8 = 20 points, which is the maximum possible value.


Sample Input 2

20 100
29 31 68 20 83 66 23 84 69 96 41 61 83 37 52 71 18 55 40 8

Sample Output 2

1733
F - Union of Two Sets

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

あなたとジャッジは下記の手順を行います。 手順はフェイズ 1 とフェイズ 2 からなり、まずフェイズ 1 を行った直後、続けてフェイズ 2 を行います。

(フェイズ 1

  • ジャッジから整数 N が与えられる。
  • あなたは 1 以上 50000 以下の整数 M を出力する。
  • さらにあなたは、すべての i = 1, 2, \ldots, M について 1 \leq l_i \leq r_i \leq N を満たす、M 個の整数の組 (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) を出力する(M 個の整数の組が相異なる必要はない)。

(フェイズ 2

  • ジャッジから整数 Q が与えられる。
  • その後、あなたとジャッジは下記の手順を Q 回繰り返す。
    • ジャッジからクエリとして 2 つの整数 L, R が与えられる。
    • それに対する応答として、あなたは 1 以上 M 以下の 2 つの整数 a, b を出力する( a = b でもよい)。 このとき、ab は下記の条件を満たさなければならない。もし満たさなかった場合は不正解となる。
      • 集合 \lbrace l_a, l_a+1, \ldots, r_a\rbrace と集合 \lbrace l_b, l_b+1, \ldots, r_b\rbrace の和集合が、集合 \lbrace L, L+1, \ldots, R\rbrace と一致する。

上記の手順を行った後、直ちにプログラムを終了することで正解となります。

制約

  • 1 \leq N \leq 4000
  • 1 \leq Q \leq 10^5
  • 1 \leq L \leq R \leq N
  • 入力はすべて整数

入出力

この問題はインタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

(フェイズ 1

  • まず、N が入力から与えられます。
  • 次に、1 以上 50000 以下の整数 M を出力してください。
  • その後、M 回にわたって (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) を出力してください。 具体的には、i = 1, 2, \ldots, M について、i 回目の出力では (l_i, r_i) を下記の形式で出力してください。
l_i r_i

(フェイズ 2

  • まず、Q が入力から与えられます。
  • 各クエリでは、クエリを表す整数 L, R が下記の形式で与えられます。
L R
  • 各クエリに対する応答では、2 つの整数 a, b を下記の形式で出力してください。
a b

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。 特に、プログラムの実行中に実行時エラーが起こった場合に、ジャッジ結果が ではなく TLE になる可能性があることに注意してください。
  • フェイズ 2 を終了したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
  • フェイズ 2 で与えられる L, R は、あなたがフェイズ 1 で出力した (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) に応じて決定されます。

入出力例

以下は、N = 4, Q = 4 の場合の入出力例です。

入力 出力 説明
4 N が与えられます。
6 M を出力します。
3 3 (l_1, r_1) = (3, 3) を出力します。
4 4 (l_2, r_2) = (4, 4) を出力します。
1 1 (l_3, r_3) = (1, 1) を出力します。
2 4 (l_4, r_4) = (2, 4) を出力します。
1 3 (l_5, r_5) = (1, 3) を出力します。
2 2 (l_6, r_6) = (2, 2) を出力します。
4 Q が与えられます。
1 3 1 個目のクエリとして L = 1, R = 3 が与えられます。
1 5 1 個目のクエリに対する応答として a = 1, b = 5 を出力します。
3 4 2 個目のクエリとして L = 3, R = 4 が与えられます。
2 1 2 個目のクエリに対する応答として a = 2, b = 1 を出力します。
2 4 3 個目のクエリとして L = 2, R = 4 が与えられます。
4 4 3 個目のクエリに対する応答として a = 4, b = 4 を出力します。
1 1 4 個目のクエリとして L = 1, R = 1 が与えられます。
3 3 4 個目のクエリに対する応答として a = 3, b = 3 を出力します。

Score : 500 points

Problem Statement

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

You and the judge will follow the procedure below. The procedure consists of phases 1 and 2; phase 1 is immediately followed by phase 2.

(Phase 1)

  • The judge gives you an integer N.
  • You print an integer M between 1 and 50000, inclusive.
  • You also print M pairs of integers (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) such that 1 \leq l_i \leq r_i \leq N for every i = 1, 2, \ldots, M (the M pairs do not have to be distinct).

(Phase 2)

  • The judge gives you an integer Q.
  • You and the judge repeats the following Q times.
    • The judge gives you two integers L and R as a query.
    • You respond with two integers a and b between 1 and M, inclusive (possibly with a = b). Here, a and b must satisfy the condition below. Otherwise, your submission will be judged incorrect.
      • The union of the set \lbrace l_a, l_a+1, \ldots, r_a\rbrace and the set \lbrace l_b, l_b+1, \ldots, r_b\rbrace equals the set \lbrace L, L+1, \ldots, R\rbrace.

After the procedure above, terminate the program immediately to be judged correct.

Constraints

  • 1 \leq N \leq 4000
  • 1 \leq Q \leq 10^5
  • 1 \leq L \leq R \leq N
  • All values in the input are integers.

Input and Output

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

(Phase 1)

  • First, N is given from the input.
  • Next, an integer M between 1 and 50000, inclusive, should be printed.
  • Then, (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) should be printed, one at a time. Specifically, for each i = 1, 2, \ldots, M, the i-th output should be (l_i, r_i) in the following format:
l_i r_i

(Phase 2)

  • First, Q is given from the input.
  • In each query, integers L and R representing the query are given in the following format:
L R
  • In response to each query, two integers a and b should be printed in the following format:
a b

Cautions

  • At the end of each output, print a newline and flush Standard Output. Otherwise, you may get the TLE verdict.
  • If your program prints a malformed output or quits prematurely, the verdict will be indeterminate. Particularly, note that in case of a runtime error, the verdict may be or TLE instead of .
  • After phase 2, immediately terminate the program. Otherwise, the verdict will be indeterminate.
  • L and R given in phase 2 will be decided according to (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) you print in phase 1.

Sample Interaction

Below is a sample interaction with N = 4 and Q = 4.

Input Output Description
4 N is given.
6 You print M.
3 3 You print (l_1, r_1) = (3, 3).
4 4 You print (l_2, r_2) = (4, 4).
1 1 You print (l_3, r_3) = (1, 1).
2 4 You print (l_4, r_4) = (2, 4).
1 3 You print (l_5, r_5) = (1, 3).
2 2 You print (l_6, r_6) = (2, 2).
4 Q is given.
1 3 As the first query, L = 1 and R = 3 are given.
1 5 You respond with a = 1 and b = 5.
3 4 As the second query, L = 3 and R = 4 are given.
2 1 You respond with a = 2 and b = 1.
2 4 As the third query, L = 2 and R = 4 are given.
4 4 You respond with a = 4 and b = 4.
1 1 As the fourth query, L = 1 and R = 1 are given.
3 3 You respond with a = 3 and b = 3.
G - Similar Permutation

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

(1,2,\ldots,N) の順列を、以下では単に順列と呼びます。

二つの順列 A=(A_1,A_2,\ldots,A_N),B=(B_1,B_2,\ldots,B_N) にたいして、類似度 を以下の条件を満たす 1 以上 N-1 以下の整数 i の個数で定めます。

  • (A_{i+1}-A_i)(B_{i+1}-B_i)>0

二つの順列の組 (A,B) であって、類似度が K であるものの個数を素数 P で割ったあまりを答えてください。

制約

  • 2\leq N \leq 100
  • 0\leq K \leq N-1
  • 10^8 \leq P \leq 10^9
  • P は素数
  • 入力は全て整数である

入力

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

N K P

出力

答えを出力せよ。


入力例 1

3 1 282282277

出力例 1

16

例えば条件を満たす順列の組の一つとして、以下のものが考えられます。

  • A=(1,2,3)
  • B=(1,3,2)

この例では、(A_2 - A_1)(B_2 -B_1) > 0, (A_3 - A_2)(B_3 -B_2) < 0 であることから、AB の類似度は 1 だとわかります。


入力例 2

50 25 998244353

出力例 2

131276976

個数を P で割ったあまりを答えてください。

Score : 600 points

Problem Statement

Below, a permutation of (1,2,\ldots,N) is simply called a permutation.

For two permutations A=(A_1,A_2,\ldots,A_N),B=(B_1,B_2,\ldots,B_N), let us define their similarity as the number of integers i between 1 and N-1 such that:

  • (A_{i+1}-A_i)(B_{i+1}-B_i)>0.

Find the number, modulo a prime number P, of pairs of permutations (A,B) whose similarity is K.

Constraints

  • 2\leq N \leq 100
  • 0\leq K \leq N-1
  • 10^8 \leq P \leq 10^9
  • P is a prime number.
  • All values in the input are integers.

Input

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

N K P

Output

Print the answer.


Sample Input 1

3 1 282282277

Sample Output 1

16

For instance, below is a pair of permutations that satisfies the condition.

  • A=(1,2,3)
  • B=(1,3,2)

Here, we have (A_2 - A_1)(B_2 -B_1) > 0 and (A_3 - A_2)(B_3 -B_2) < 0, so the similarity of A and B is 1.


Sample Input 2

50 25 998244353

Sample Output 2

131276976

Print the number modulo P.

Ex - Min + Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

2 つの長さ N の整数列 A = (A_1, A_2, \ldots, A_N) および B = (B_1, B_2, \ldots, B_N) が与えられます。

1 \leq l \leq r \leq N を満たす整数の組 (l, r) であって下記の条件を満たすものの個数を出力してください。

  • \min\lbrace A_l, A_{l+1}, \ldots, A_r \rbrace + (B_l + B_{l+1} + \cdots + B_r) \leq S

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq S \leq 3 \times 10^{14}
  • 0 \leq A_i \leq 10^{14}
  • 0 \leq B_i \leq 10^9
  • 入力はすべて整数

入力

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

N S
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

答えを出力せよ。


入力例 1

4 15
9 2 6 5
3 5 8 9

出力例 1

6

1 \leq l \leq r \leq N を満たす整数の組 (l, r) であって問題文中の条件を満たすものは、 (1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (4, 4)6 個です。


入力例 2

15 100
39 9 36 94 40 26 12 26 28 66 73 85 62 5 20
0 0 7 7 0 5 5 0 7 9 9 4 2 5 2

出力例 2

119

Score : 600 points

Problem Statement

You are given two sequences of integers of length N: A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2, \ldots, B_N).

Print the number of pairs of integers (l, r) that satisfy 1 \leq l \leq r \leq N and the following condition.

  • \min\lbrace A_l, A_{l+1}, \ldots, A_r \rbrace + (B_l + B_{l+1} + \cdots + B_r) \leq S

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq S \leq 3 \times 10^{14}
  • 0 \leq A_i \leq 10^{14}
  • 0 \leq B_i \leq 10^9
  • All values in the input are integers.

Input

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

N S
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print the answer.


Sample Input 1

4 15
9 2 6 5
3 5 8 9

Sample Output 1

6

The following six pairs of integers (l, r) satisfy 1 \leq l \leq r \leq N and the condition in the problem statement: (1, 1), (1, 2), (2, 2), (2, 3), (3, 3), and (4, 4).


Sample Input 2

15 100
39 9 36 94 40 26 12 26 28 66 73 85 62 5 20
0 0 7 7 0 5 5 0 7 9 9 4 2 5 2

Sample Output 2

119