A - Appender

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英小文字からなる文字列 S が与えられます。

以下のようにして決まる文字列 T を出力してください。

  • S の末尾の文字が e である場合、T は S の末尾に r を付け加えた文字列である。
  • S の末尾の文字が e でない場合、T は S の末尾に er を付け加えた文字列である。

制約

  • S は英小文字からなる文字列
  • S の長さは 1 以上 10 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

live

出力例 1

liver

live の末尾の文字は e なので、live の末尾に r を付け加えた文字列 liver が出力するべき文字列 T です。


入力例 2

femur

出力例 2

femurer

femur の末尾の文字は e ではないので、femur の末尾に er を付け加えた文字列 femurer が出力するべき文字列 T です。


入力例 3

chimpanzee

出力例 3

chimpanzeer

chimpanzee の末尾の文字は e なので、chimpanzee の末尾に r を付け加えた文字列 chimpanzeer が出力するべき文字列 T です。

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters.

Output the string T determined as follows.

  • If the last character of S is e, T is the string obtained by appending r to the end of S.
  • If the last character of S is not e, T is the string obtained by appending er to the end of S.

Constraints

  • S is a string consisting of lowercase English letters.
  • The length of S is between 1 and 10, inclusive.

Input

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

S

Output

Output the answer.


Sample Input 1

live

Sample Output 1

liver

The last character of live is e, so the string liver, obtained by appending r to the end of live, is the string T to be outputted.


Sample Input 2

femur

Sample Output 2

femurer

The last character of femur is not e, so the string femurer, obtained by appending er to the end of femur, is the string T to be outputted.


Sample Input 3

chimpanzee

Sample Output 3

chimpanzeer

The last character of chimpanzee is e, so the string chimpanzeer, obtained by appending r to the end of chimpanzee, is the string T to be outputted.

B - Wild Card

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

S は英小文字からなる長さ N の文字列です。
T は英小文字および * からなる長さ N の文字列です。
S が T にマッチするとは、T に含まれる * をそれぞれ好きな英小文字で置き換えることで T を S に一致させられることをいいます。
S が T にマッチするかどうかを判定してください。

制約

  • 1 \leq N \leq 100
  • S は英小文字からなる長さ N の文字列
  • T は英小文字および * からなる長さ N の文字列

入力

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

N  
S  
T  

出力

S が T にマッチするならば Yes を、マッチしないならば No を 1 行で出力せよ。


入力例 1

8
chokudai
**o*u*ai

出力例 1

Yes

T の 1,2,4,6 文字目の * をそれぞれ c,h,k,d に置き換えることで T を S に一致させることができます。


入力例 2

5
snuke
snake

出力例 2

No

入力例 3

5
yiwiy
*****

出力例 3

Yes

Score : 200 points

Problem Statement

S is a string of length N consisting of lowercase English letters.
T is a string of length N consisting of lowercase English letters and *.
We say that S matches T if and only if T can be made equal to S by replacing each * in T with some lowercase English letter.
Determine whether S matches T.

Constraints

  • 1 \leq N \leq 100
  • S is a string of length N consisting of lowercase English letters.
  • T is a string of length N consisting of lowercase English letters and *.

Input

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

N  
S  
T  

Output

Output Yes if S matches T, and No otherwise, in one line.


Sample Input 1

8
chokudai
**o*u*ai

Sample Output 1

Yes

By replacing the first, second, fourth, and sixth characters of T, which are *, with c, h, k, and d, respectively, T can be made equal to S.


Sample Input 2

5
snuke
snake

Sample Output 2

No

Sample Input 3

5
yiwiy
*****

Sample Output 3

Yes
C - Third Largest Number

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

3 以上の整数 N と長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

k=3,4,\ldots,N それぞれに対し以下の問題を解いてください:

  • A_1,A_2,\ldots,A_k を降順に並べたとき、前から 3 番目にくる値を求めてください。

制約

  • 3\le N\le 5\times 10^5
  • 1\le A_i\le 10^9
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

N-2 行出力せよ。

i 行目 (1\le i\le N-2) には k=i+2 とした時の答えを出力せよ。


入力例 1

5
1 2 1 2 3

出力例 1

1
1
2
  • k=3 のとき:A_1,A_2,A_3 を降順に並べると 2,1,1 となります。前から 3 番目に来る値は 1 なので、1 行目には 1 を出力してください。
  • k=4 のとき:A_1,A_2,A_3,A_4 を降順に並べると 2,2,1,1 となります。前から 3 番目に来る値は 1 なので、2 行目には 1 を出力してください。
  • k=5 のとき:A_1,A_2,A_3,A_4,A_5 を降順に並べると 3,2,2,1,1 となります。前から 3 番目に来る値は 2 なので、3 行目には 2 を出力してください。

入力例 2

10
1 1 1 3 2 5 4 3 6 5

出力例 2

1
1
1
2
3
3
4
5

入力例 3

10
11 9 1 3 17 19 10 19 17 3

出力例 3

1
3
9
11
11
17
17
17

Score : 300 points

Problem Statement

You are given an integer N not less than 3 and a length-N sequence of positive integers A=(A_1,A_2,\ldots,A_N).

For each k=3,4,\ldots,N, solve the following problem:

  • When A_1,A_2,\ldots,A_k are sorted in descending order, find the third value from the front.

Constraints

  • 3\le N\le 5\times 10^5
  • 1\le A_i\le 10^9
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Output N-2 lines.

The i-th line (1\le i\le N-2) should contain the answer for k=i+2.


Sample Input 1

5
1 2 1 2 3

Sample Output 1

1
1
2
  • For k=3: sorting A_1,A_2,A_3 in descending order gives 2,1,1. The third value from the front is 1, so output 1 on the first line.
  • For k=4: sorting A_1,A_2,A_3,A_4 in descending order gives 2,2,1,1. The third value from the front is 1, so output 1 on the second line.
  • For k=5: sorting A_1,A_2,A_3,A_4,A_5 in descending order gives 3,2,2,1,1. The third value from the front is 2, so output 2 on the third line.

Sample Input 2

10
1 1 1 3 2 5 4 3 6 5

Sample Output 2

1
1
1
2
3
3
4
5

Sample Input 3

10
11 9 1 3 17 19 10 19 17 3

Sample Output 3

1
3
9
11
11
17
17
17
D - Automat

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

AtCoder 王国では、1 ドル紙幣と K ドル紙幣の 2 種類の紙幣が流通しています。

AtCoder 王国にある J 社の全自動食堂では、デザートやドリンクが商品として売られています。

J 社の全自動食堂には、N 個のデザートを売るデザート販売機と、M 個のドリンクを売るドリンク販売機があります。 デザートには 1 から N までの、ドリンクには 1 から M までの番号が付けられています。

デザート i の価格は A_i ドル、ドリンク j の価格は B_j ドルです。

支払いに使う紙幣として、デザート販売機は 1 ドル紙幣と K ドル紙幣の両方を受け入れますが、ドリンク販売機は K ドル紙幣しか受け入れません。 どちらの販売機も、お釣りはすべて 1 ドル紙幣のみを使って返します。 同じ商品を 2 個以上買うことはできません。

高橋君は 1 ドル紙幣 X 枚と K ドル紙幣 Y 枚を持って J 社の全自動食堂にやってきました。

高橋君が手持ちの紙幣で買える商品の組合せにおいて、購入できる商品数の最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 2 \leq K \leq 10^9
  • 0 \leq X \leq 10^{15}
  • 0 \leq Y \leq 10^9
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
  • 入力される値はすべて整数

入力

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

N M K
X Y
A_1 \dots A_N
B_1 \dots B_M

出力

答えを出力せよ。


入力例 1

2 3 10
50 6
22 30
20 12 24

出力例 1

4

以下の手順で買い物を行うことで、4 個の商品を買うことができます。

  • 10 ドル紙幣 2 枚を払ってドリンク 1 を買う。お釣りは無し。
  • 10 ドル紙幣 2 枚を払ってドリンク 2 を買う。お釣りは 1 ドル紙幣 8 枚。
  • 10 ドル紙幣 2 枚と 1 ドル紙幣 10 枚を払ってデザート 2 を買う。お釣りは無し。
  • 1 ドル紙幣 22 枚を払ってデザート 1 を買う。お釣りは無し。

4 個よりも多く買うことはできないので、4 が答えとなります。


入力例 2

1 7 67
677677677766666 0
777666777
20 12 24 67 67 67 67

出力例 2

1

以下の手順で買い物を行うことで、1 個の商品を買うことができます。

  • 1 ドル紙幣 600000777666777 枚を払ってデザート 1 を買う。お釣りは 1 ドル紙幣 600000000000000 枚。

1 個よりも多く買うことはできないので、1 が答えとなります。


入力例 3

20 20 30
605776135 133105105
97363214 218434035 697895427 109255624 299037330 227873982 195540071 411713803 828357845 244535208 138059186 639510883 39844882 707397687 371274487 696536603 351588202 319490007 47121612 87169661
32256972 567982330 554885983 299718223 443859449 687952877 264684780 666659381 576335424 941894234 406248934 321334900 423472560 863738035 213143887 384834384 468161291 673106162 164648316 15903323

出力例 3

22

Score : 425 points

Problem Statement

In the Kingdom of AtCoder, two kinds of bills are in circulation: 1-dollar bills and K-dollar bills.

At Company J's fully automated cafeteria in the kingdom, desserts and drinks are sold as products.

The company's fully automated cafeteria has a dessert vending machine that sells N desserts, and a drink vending machine that sells M drinks. The desserts are numbered 1 through N, and the drinks are numbered 1 through M.

Dessert i costs A_i dollars, and drink j costs B_j dollars.

As payment, the dessert vending machine accepts both 1-dollar bills and K-dollar bills, but the drink vending machine accepts only K-dollar bills. Both vending machines give change using only 1-dollar bills. You cannot buy two or more of the same product.

Takahashi came to Company J's fully automated cafeteria with X 1-dollar bills and Y K-dollar bills.

Among the combinations of products that he can buy with the bills he has, find the maximum number of products he can purchase.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 2 \leq K \leq 10^9
  • 0 \leq X \leq 10^{15}
  • 0 \leq Y \leq 10^9
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
  • All input values are integers.

Input

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

N M K
X Y
A_1 \dots A_N
B_1 \dots B_M

Output

Output the answer.


Sample Input 1

2 3 10
50 6
22 30
20 12 24

Sample Output 1

4

By shopping as follows, Takahashi can buy four products.

  • Pay two 10-dollar bills to buy drink 1. No change.
  • Pay two 10-dollar bills to buy drink 2. The change is eight 1-dollar bills.
  • Pay two 10-dollar bills and ten 1-dollar bills to buy dessert 2. No change.
  • Pay twenty-two 1-dollar bills to buy dessert 1. No change.

He cannot buy more than four products, so the answer is 4.


Sample Input 2

1 7 67
677677677766666 0
777666777
20 12 24 67 67 67 67

Sample Output 2

1

By shopping as follows, Takahashi can buy one product.

  • Pay 600000777666777 1-dollar bills to buy dessert 1. The change is 600000000000000 1-dollar bills.

He cannot buy more than one product, so the answer is 1.


Sample Input 3

20 20 30
605776135 133105105
97363214 218434035 697895427 109255624 299037330 227873982 195540071 411713803 828357845 244535208 138059186 639510883 39844882 707397687 371274487 696536603 351588202 319490007 47121612 87169661
32256972 567982330 554885983 299718223 443859449 687952877 264684780 666659381 576335424 941894234 406248934 321334900 423472560 863738035 213143887 384834384 468161291 673106162 164648316 15903323

Sample Output 3

22
E - Min-Max Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) と長さ M の整数列 L=(L_1,L_2,\ldots,L_M),\ R=(R_1,R_2,\ldots,R_M) が与えられます。

この順列 P に対し、i=1,2,\ldots,M の順に以下の操作を行います:

  • P_{L_i},P_{L_i+1},\ldots,P_{R_i} のうち、最小の値をもつ要素と最大の値をもつ要素の位置を入れ替える。

M 回の操作が終わった後の P の各要素を求めてください。

制約

  • 2\le N\le 2\times 10^5
  • 1\le M\le 2\times 10^5
  • P は (1,2,\ldots,N) の順列
  • 1\le L_i < R_i\le N
  • 入力される値は全て整数

入力

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

N M
P_1 P_2 \ldots P_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M

出力

M 回の操作が終わった後の P の各要素を先頭から順に空白区切りで出力せよ。


入力例 1

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

出力例 1

3 4 2 5 1

はじめ P=(3,1,4,2,5) です。

  • i=1 のとき:P_1,P_2,P_3 のうち、最小の値は P_2 で最大の値は P_3 です。P_2 と P_3 を入れ替え、P=(3,4,1,2,5) となります。
  • i=2 のとき:P_1,P_2,P_3,P_4,P_5 のうち、最小の値は P_3 で最大の値は P_5 です。P_3 と P_5 を入れ替え、P=(3,4,5,2,1) となります。
  • i=3 のとき:P_1,P_2,P_3,P_4 のうち、最小の値は P_4 で最大の値は P_3 です。P_3 と P_4 を入れ替え、P=(3,4,2,5,1) となります。

3 回の操作が終わったあと P=(3,4,2,5,1) となります。


入力例 2

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

出力例 2

3 5 4 6 2 1

Score : 450 points

Problem Statement

You are given a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) and length-M integer sequences L=(L_1,L_2,\ldots,L_M) and R=(R_1,R_2,\ldots,R_M).

For this permutation P, perform the following operation for i=1,2,\ldots,M in this order:

  • Among P_{L_i},P_{L_i+1},\ldots,P_{R_i}, swap the positions of the element with the minimum value and the element with the maximum value.

Find each element of P after the M operations.

Constraints

  • 2\le N\le 2\times 10^5
  • 1\le M\le 2\times 10^5
  • P is a permutation of (1,2,\ldots,N).
  • 1\le L_i < R_i\le N
  • All input values are integers.

Input

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

N M
P_1 P_2 \ldots P_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Output each element of P after the M operations, in order from front to back, separated by spaces.


Sample Input 1

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

Sample Output 1

3 4 2 5 1

Initially, P=(3,1,4,2,5).

  • For i=1: among P_1,P_2,P_3, the minimum value is P_2 and the maximum value is P_3. Swapping P_2 and P_3 gives P=(3,4,1,2,5).
  • For i=2: among P_1,P_2,P_3,P_4,P_5, the minimum value is P_3 and the maximum value is P_5. Swapping P_3 and P_5 gives P=(3,4,5,2,1).
  • For i=3: among P_1,P_2,P_3,P_4, the minimum value is P_4 and the maximum value is P_3. Swapping P_3 and P_4 gives P=(3,4,2,5,1).

After the three operations, P=(3,4,2,5,1).


Sample Input 2

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

Sample Output 2

3 5 4 6 2 1
F - Chebyshev Cafe

Time Limit: 3 sec / Memory Limit: 2048 MiB

配点 : 525 点

問題文

N 行 N 列のグリッドで表される市街地があります。上から i 行目・左から j 列目のマスをマス (i,j) と表します。各マスには 1 つずつカフェがあります。

あなたはミーティングを企画しています。参加者のうち、マス (i, j) に住む人の人数は (A_i \times B_j) \bmod M です。 マス (s_r,\ s_c) に住む参加者 1 人がマス (t_r,\ t_c) にあるカフェに行くときにかかる交通費は \max(|s_r - t_r|,\ |s_c - t_c|) です。

参加者全員をマス (i, j) にあるカフェへと集めるときにかかる交通費の合計を f(i, j) とします。

各マス (i,j) に対する以下の値をすべてのマスについて求めたときの、それらの bitwise XOR として得られる値を出力してください。

  • f(i,j) + (i-1)N + (j-1)
bitwise XOR 演算とは

非負整数 A, B の bitwise XOR (A \oplus B で表す) は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011_{(2)} \oplus 101_{(2)} = 110_{(2)})。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k の bitwise XOR は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。

制約

  • 1 \leq N \leq 1500
  • 2 \leq M \leq 2 \times 10^6
  • 1 \leq A_i \leq M-1 (1 \leq i \leq N)
  • 1 \leq B_j \leq M-1 (1 \leq j \leq N)
  • 入力される値はすべて整数

入力

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

N M
A_1 \dots A_N
B_1 \dots B_N

出力

答えを出力せよ。


入力例 1

4 19
2 3 1 1
3 4 4 10

出力例 1

467

各マスに住む参加者の人数を並べると \displaystyle \begin{bmatrix} 6 & 8 & 8 & 1 \\ 9 & 12 & 12 & 11 \\ 3 & 4 & 4 & 10 \\ 3 & 4 & 4 & 10 \end{bmatrix} となります。

たとえば f(1,2) の値は、次の 16 個の値の合計値 176 として得られます。

  • マス (1,1) の住人 6 人がそれぞれ交通費 \max(|1 - 1|,\ |1 - 2|) = 1 をかけたときの総額 6 \times 1 = 6
  • マス (1,2) の住人 8 人がそれぞれ交通費 \max(|1 - 1|,\ |2 - 2|) = 0 をかけたときの総額 8 \times 0 = 0
  • マス (1,3) の住人 8 人がそれぞれ交通費 \max(|1 - 1|,\ |3 - 2|) = 1 をかけたときの総額 8 \times 1 = 8
  • マス (1,4) の住人 1 人がそれぞれ交通費 \max(|1 - 1|,\ |4 - 2|) = 2 をかけたときの総額 1 \times 2 = 2
  • \vdots
  • マス (4,1) の住人 3 人がそれぞれ交通費 \max(|4 - 1|,\ |1 - 2|) = 3 をかけたときの総額 3 \times 3 = 9
  • マス (4,2) の住人 4 人がそれぞれ交通費 \max(|4 - 1|,\ |2 - 2|) = 3 をかけたときの総額 4 \times 3 = 12
  • マス (4,3) の住人 4 人がそれぞれ交通費 \max(|4 - 1|,\ |3 - 2|) = 3 をかけたときの総額 4 \times 3 = 12
  • マス (4,4) の住人 10 人がそれぞれ交通費 \max(|4 - 1|,\ |4 - 2|) = 3 をかけたときの総額 10 \times 3 = 30

各マスにおける f(i,j) の値を並べると \displaystyle \begin{bmatrix} 220 & 176 & 179 & 224 \\ 199 & 140 & 136 & 182 \\ 212 & 159 & 143 & 178 \\ 255 & 215 & 201 & 218 \end{bmatrix} となります。


入力例 2

6 67
1 6 9 2 3 1
1 10 3 7 2 2

出力例 2

1021

入力例 3

20 676767
60932 508200 22883 481150 426836 554098 276223 307959 133025 573906 17711 413230 274246 500277 524959 477728 546547 43099 324227 660187
552697 293978 246000 159233 213736 628198 394200 194076 618720 453035 615100 331522 669419 180856 530693 399531 223149 159334 102107 598633

出力例 3

764668547

Score : 525 points

Problem Statement

There is a city represented by a grid with N rows and N columns. The cell at the i-th row from the top and j-th column from the left is denoted as cell (i,j). Each cell has one cafe.

You are planning a meeting. Among the participants, the number of people living in cell (i, j) is (A_i \times B_j) \bmod M. The travel cost for one participant living in cell (s_r,\ s_c) to go to the cafe at cell (t_r,\ t_c) is \max(|s_r - t_r|,\ |s_c - t_c|).

Let f(i, j) be the total travel cost incurred when gathering all the participants at the cafe at cell (i, j).

For each cell (i,j), find the following value, and output the bitwise XOR of these values over all cells.

  • f(i,j) + (i-1)N + (j-1)
What is the bitwise XOR operation?

The bitwise XOR of non-negative integers A and B (denoted A \oplus B) is defined as follows.

  • The digit in the 2^k's place (k \geq 0) of A \oplus B written in binary is 1 if exactly one of the digits in the 2^k's place of A and B written in binary is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011_{(2)} \oplus 101_{(2)} = 110_{(2)}).
More generally, the bitwise XOR of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 1 \leq N \leq 1500
  • 2 \leq M \leq 2 \times 10^6
  • 1 \leq A_i \leq M-1 (1 \leq i \leq N)
  • 1 \leq B_j \leq M-1 (1 \leq j \leq N)
  • All input values are integers.

Input

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

N M
A_1 \dots A_N
B_1 \dots B_N

Output

Output the answer.


Sample Input 1

4 19
2 3 1 1
3 4 4 10

Sample Output 1

467

The number of participants living in each cell is as follows: \displaystyle \begin{bmatrix} 6 & 8 & 8 & 1 \\ 9 & 12 & 12 & 11 \\ 3 & 4 & 4 & 10 \\ 3 & 4 & 4 & 10 \end{bmatrix}.

For example, the value of f(1,2) is obtained as the sum 176 of the following 16 values:

  • The total amount 6 \times 1 = 6 paid by the 6 residents of cell (1,1), each of whom pays a travel cost of \max(|1 - 1|,\ |1 - 2|) = 1
  • The total amount 8 \times 0 = 0 paid by the 8 residents of cell (1,2), each of whom pays a travel cost of \max(|1 - 1|,\ |2 - 2|) = 0
  • The total amount 8 \times 1 = 8 paid by the 8 residents of cell (1,3), each of whom pays a travel cost of \max(|1 - 1|,\ |3 - 2|) = 1
  • The total amount 1 \times 2 = 2 paid by the 1 resident of cell (1,4), each of whom pays a travel cost of \max(|1 - 1|,\ |4 - 2|) = 2
  • \vdots
  • The total amount 3 \times 3 = 9 paid by the 3 residents of cell (4,1), each of whom pays a travel cost of \max(|4 - 1|,\ |1 - 2|) = 3
  • The total amount 4 \times 3 = 12 paid by the 4 residents of cell (4,2), each of whom pays a travel cost of \max(|4 - 1|,\ |2 - 2|) = 3
  • The total amount 4 \times 3 = 12 paid by the 4 residents of cell (4,3), each of whom pays a travel cost of \max(|4 - 1|,\ |3 - 2|) = 3
  • The total amount 10 \times 3 = 30 paid by the 10 residents of cell (4,4), each of whom pays a travel cost of \max(|4 - 1|,\ |4 - 2|) = 3

The value of f(i,j) for each cell is as follows: \displaystyle \begin{bmatrix} 220 & 176 & 179 & 224 \\ 199 & 140 & 136 & 182 \\ 212 & 159 & 143 & 178 \\ 255 & 215 & 201 & 218 \end{bmatrix}.


Sample Input 2

6 67
1 6 9 2 3 1
1 10 3 7 2 2

Sample Output 2

1021

Sample Input 3

20 676767
60932 508200 22883 481150 426836 554098 276223 307959 133025 573906 17711 413230 274246 500277 524959 477728 546547 43099 324227 660187
552697 293978 246000 159233 213736 628198 394200 194076 618720 453035 615100 331522 669419 180856 530693 399531 223149 159334 102107 598633

Sample Output 3

764668547
G - Increasing Popcount

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 625 点

問題文

L\le R を満たす正整数 L,R が与えられます。

以下の条件を満たす長さ R-L+1 の正整数列 A=(A_L,A_{L+1},\ldots,A_R) を良い整数列と呼びます:

  • L\le i < j\le R を満たすすべての整数の組 (i,j) に対し、A_i = A_j ならば \operatorname{popcount}(i)<\operatorname{popcount}(j) が成り立つ。

良い整数列 A=(A_L,A_{L+1},\ldots,A_R) 全てに対する \max(A_L,A_{L+1},\ldots,A_R) の最小値を求めてください。

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

\operatorname{popcount} とは?

非負整数 x について \operatorname{popcount}(x) とは、x を 2 進法で表記したときの 1 の個数です。 より厳密には、非負整数 x について \displaystyle x=\sum _ {i=0} ^ \infty b _ i2 ^ i\ (b _ i\in\lbrace0,1\rbrace) が成り立っているとき \displaystyle\operatorname{popcount}(x)=\sum _ {i=0} ^ \infty b _ i です。

例えば、13 を 2 進法で表記すると 1101 なので、 \operatorname{popcount}(13)=3 となります。

制約

  • 1\le T\le 10^4
  • 1\le L \le R\le 10^{18}
  • 入力される値は全て整数

入力

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

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

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

L R

出力

T 行出力せよ。

i 行目 (1\le i\le T) には、i 番目のテストケース \text{case}_i に対する答えを出力せよ。


入力例 1

5
3 6
1 1
2 15
100 10000000
514284786278117031 620546740167642909

出力例 1

3
1
6
1636985
11375123021856567

1 番目のテストケースについて考えます。

(A_3,A_4,A_5,A_6)=(1,2,2,3) は良い整数列で、\max(A_3,A_4,A_5,A_6)=3 となります。

\max(A_3,A_4,A_5,A_6) < 3 を満たす良い整数列は存在しないので、1 行目には 3 を出力してください。

Score : 625 points

Problem Statement

You are given positive integers L and R satisfying L\le R.

A sequence of positive integers A=(A_L,A_{L+1},\ldots,A_R) of length R-L+1 satisfying the following condition is called a good integer sequence:

  • For every pair of integers (i,j) satisfying L\le i < j\le R, if A_i = A_j, then \operatorname{popcount}(i)<\operatorname{popcount}(j).

Find the minimum value of \max(A_L,A_{L+1},\ldots,A_R) over all good integer sequences A=(A_L,A_{L+1},\ldots,A_R).

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

What is \operatorname{popcount}?

For a non-negative integer x, \operatorname{popcount}(x) is the number of 1's when x is written in binary. More precisely, when a non-negative integer x satisfies \displaystyle x=\sum _ {i=0} ^ \infty b _ i2 ^ i\ (b _ i\in\lbrace0,1\rbrace), \displaystyle\operatorname{popcount}(x)=\sum _ {i=0} ^ \infty b _ i.

For example, 13 written in binary is 1101, so \operatorname{popcount}(13)=3.

Constraints

  • 1\le T\le 10^4
  • 1\le L \le R\le 10^{18}
  • 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

Each test case is given in the following format.

L R

Output

Output T lines.

The i-th line (1\le i\le T) should contain the answer for the i-th test case \text{case}_i.


Sample Input 1

5
3 6
1 1
2 15
100 10000000
514284786278117031 620546740167642909

Sample Output 1

3
1
6
1636985
11375123021856567

Consider the first test case.

(A_3,A_4,A_5,A_6)=(1,2,2,3) is a good integer sequence, and \max(A_3,A_4,A_5,A_6)=3.

There is no good integer sequence with \max(A_3,A_4,A_5,A_6) < 3, so output 3 on the first line.