実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 233 点
問題文
高橋君は夏祭りの屋台を経営しています。屋台では N 種類の商品を販売しており、 M 人のアルバイトを雇っています。
高橋君は開業資金として S 円を持っています。高橋君は、この資金の範囲内でアルバイトへの日給を支払いながら、できるだけ多くの日数屋台を営業したいと考えています。
各アルバイトにはそれぞれ 1 日あたりの日給が決まっており、 j 番目のアルバイトの日給は B_j 円です。屋台を 1 日営業するには、全アルバイトに日給を支払う必要があります。
一方、屋台の売上は各商品の人気度によって決まります。 i 番目の商品には「人気度」 A_i が設定されており、 1 日あたりの売上は全商品の人気度の合計、すなわち \sum_{i=1}^{N} A_i 円です。
1 日の営業では、売上を得てからアルバイト全員への日給を支払います。つまり、ある日の営業終了時点での所持金の変化は +\left(\sum_{i=1}^{N} A_i\right) - \left(\sum_{j=1}^{M} B_j\right) 円です。
高橋君は、営業日ごとに日給を支払った後の所持金が 0 円未満になってはいけません( 0 円ちょうどは許容されます)。
高橋君が最大で何日間屋台を営業できるかを求めてください。ただし、もし何日でも営業を続けられる場合は -1 を出力してください。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 0 \leq S \leq 10^{18}
- 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 S A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
- 1 行目には、商品の種類数を表す N 、アルバイトの人数を表す M 、高橋君の開業資金を表す S が、スペース区切りで与えられる。
- 2 行目には、各商品の人気度を表す A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 3 行目には、各アルバイトの 1 日あたりの日給を表す B_1, B_2, \ldots, B_M が、スペース区切りで与えられる。
出力
高橋君が営業できる最大日数を 1 行で出力せよ。何日でも営業を続けられる場合は -1 を出力せよ。
入力例 1
2 2 10 3 2 4 4
出力例 1
3
入力例 2
3 2 5 10 5 5 8 7
出力例 2
-1
入力例 3
3 4 1000000000000000000 100000000 200000000 300000000 300000000 200000000 150000000 100000000
出力例 3
6666666666
Score : 233 pts
Problem Statement
Takahashi runs a stall at a summer festival. The stall sells N types of products, and he has hired M part-time workers.
Takahashi has S yen as his starting capital. He wants to operate the stall for as many days as possible while paying the daily wages to the part-time workers within this budget.
Each part-time worker has a fixed daily wage. The daily wage of the j-th part-time worker is B_j yen. To operate the stall for one day, he must pay the daily wages to all part-time workers.
On the other hand, the stall's revenue is determined by the popularity of each product. The i-th product has a "popularity" value A_i, and the daily revenue is the sum of the popularity values of all products, i.e., \sum_{i=1}^{N} A_i yen.
During one day of operation, the revenue is earned first, and then the daily wages are paid to all part-time workers. In other words, the change in the amount of money held at the end of a day's operation is +\left(\sum_{i=1}^{N} A_i\right) - \left(\sum_{j=1}^{M} B_j\right) yen.
Takahashi's money after paying the daily wages on each business day must not become less than 0 yen (exactly 0 yen is acceptable).
Determine the maximum number of days Takahashi can operate the stall. If he can continue operating indefinitely, output -1.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 0 \leq S \leq 10^{18}
- 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 inputs are integers.
Input
N M S A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
- The first line contains N representing the number of product types, M representing the number of part-time workers, and S representing Takahashi's starting capital, separated by spaces.
- The second line contains the popularity values of each product A_1, A_2, \ldots, A_N, separated by spaces.
- The third line contains the daily wage of each part-time worker B_1, B_2, \ldots, B_M, separated by spaces.
Output
Output the maximum number of days Takahashi can operate the stall in one line. If he can continue operating indefinitely, output -1.
Sample Input 1
2 2 10 3 2 4 4
Sample Output 1
3
Sample Input 2
3 2 5 10 5 5 8 7
Sample Output 2
-1
Sample Input 3
3 4 1000000000000000000 100000000 200000000 300000000 300000000 200000000 150000000 100000000
Sample Output 3
6666666666
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 333 点
問題文
高橋君は料理コンテストに出場します。コンテストでは N 個の食材を使って料理を作ります。
食材には 1 から N までの番号がついており、i 番目の食材のおいしさは A_i です。高橋君は各食材について代わりの食材を 1 つずつ用意しており、i 番目の食材を代わりの食材に入れ替えると、おいしさが B_i に変わります。
コンテストのルールにより、高橋君は N 個の食材の中からちょうど K 個を選び、それらを代わりの食材に入れ替えなければなりません。入れ替えなかった食材のおいしさはそのまま A_i です。
料理全体のおいしさは、入れ替え後の N 個の食材のおいしさの総和で決まります。入れ替える K 個の食材をうまく選んだとき、料理全体のおいしさの最大値を求めてください。
制約
- 1 \leq K \leq N \leq 3 \times 10^5
- -10^9 \leq A_i \leq 10^9
- -10^9 \leq B_i \leq 10^9
- 入力はすべて整数である
入力
N K A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N
- 1 行目には、食材の個数を表す N と、入れ替える食材の個数を表す K が、スペース区切りで与えられる。
- 2 行目には、現在の食材のおいしさを表す A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 3 行目には、代わりの食材のおいしさを表す B_1, B_2, \ldots, B_N が、スペース区切りで与えられる。
出力
料理全体のおいしさの最大値を 1 行で出力してください。
入力例 1
4 2 5 2 4 1 3 10 1 7
出力例 1
26
入力例 2
3 2 8 -1 5 4 -3 2
出力例 2
7
入力例 3
8 4 3 -5 12 7 0 9 -2 4 8 -10 6 20 -1 9 5 -3
出力例 3
53
入力例 4
20 9 4 -7 13 0 25 -12 8 19 -3 6 11 -1 14 -9 2 17 5 -6 10 1 9 -20 5 7 30 -4 -8 18 12 6 -2 3 16 -15 20 10 -1 14 0 1
出力例 4
181
入力例 5
1 1 -1000000000 1000000000
出力例 5
1000000000
Score : 333 pts
Problem Statement
Takahashi is participating in a cooking contest. In the contest, he will prepare a dish using N ingredients.
The ingredients are numbered from 1 to N, and the deliciousness of the i-th ingredient is A_i. Takahashi has prepared one substitute ingredient for each ingredient. If he replaces the i-th ingredient with its substitute, its deliciousness changes to B_i.
According to the contest rules, Takahashi must choose exactly K out of the N ingredients and replace them with their substitute ingredients. The deliciousness of ingredients that are not replaced remains A_i.
The overall deliciousness of the dish is determined by the sum of the deliciousness values of all N ingredients after replacement. Find the maximum possible overall deliciousness of the dish when the K ingredients to replace are chosen optimally.
Constraints
- 1 \leq K \leq N \leq 3 \times 10^5
- -10^9 \leq A_i \leq 10^9
- -10^9 \leq B_i \leq 10^9
- All input values are integers
Input
N K A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N
- The first line contains N, the number of ingredients, and K, the number of ingredients to replace, separated by a space.
- The second line contains A_1, A_2, \ldots, A_N, the deliciousness values of the current ingredients, separated by spaces.
- The third line contains B_1, B_2, \ldots, B_N, the deliciousness values of the substitute ingredients, separated by spaces.
Output
Print the maximum possible overall deliciousness of the dish in one line.
Sample Input 1
4 2 5 2 4 1 3 10 1 7
Sample Output 1
26
Sample Input 2
3 2 8 -1 5 4 -3 2
Sample Output 2
7
Sample Input 3
8 4 3 -5 12 7 0 9 -2 4 8 -10 6 20 -1 9 5 -3
Sample Output 3
53
Sample Input 4
20 9 4 -7 13 0 25 -12 8 19 -3 6 11 -1 14 -9 2 17 5 -6 10 1 9 -20 5 7 30 -4 -8 18 12 6 -2 3 16 -15 20 10 -1 14 0 1
Sample Output 4
181
Sample Input 5
1 1 -1000000000 1000000000
Sample Output 5
1000000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は山岳写真家です。東西に一直線に並んだ N 本の山の峰があり、西から i 番目の峰の標高は H_i です。
高橋君は、区間 [l, r](1 \leq l \leq r \leq N)に含まれる峰を写真に収めたいと考えています。ここで区間 [l, r] とは、西から l 番目、l+1 番目、\ldots、r 番目の峰からなる連続した峰の並びを指します。
美しい写真を撮るためには、峰の並びが 山型 である必要があります。
区間 [l, r] の峰の並びが 山型 であるとは、ある整数 k(l \leq k \leq r)が存在して、
H_l < H_{l+1} < \cdots < H_k > H_{k+1} > \cdots > H_r
が成り立つことを言います。すなわち、l から k までは標高が狭義単調増加し、k から r までは標高が狭義単調減少します。特に、l = r の場合(峰が 1 つだけ)も山型です。また、k = l の場合は全体が狭義単調減少、k = r の場合は全体が狭義単調増加となりますが、これらも山型に含まれます。
(補足:山型の定義では狭義の不等号を用いているため、隣接する峰の標高が等しい箇所(H_i = H_{i+1})を含む区間は山型にはなりません。)
さらに、高橋君はダイナミックな写真を撮りたいので、区間内の標高の最大値と最小値の差、すなわち
\max(H_l, H_{l+1}, \ldots, H_r) - \min(H_l, H_{l+1}, \ldots, H_r)
が K 以上である山型の区間のみを 撮影候補 とします。
撮影候補の区間が存在する場合、その中で区間に含まれる峰の数 r - l + 1 の最大値を求めてください。撮影候補が 1 つも存在しない場合は 0 を出力してください。
制約
- 1 \leq N \leq 10^6
- 1 \leq K \leq 10^9
- 1 \leq H_i \leq 10^9
- H_i は互いに異なるとは限らない
- 入力はすべて整数である
入力
N K H_1 H_2 \ldots H_N
- 1 行目には、峰の数を表す整数 N と、標高差の下限を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、西から i 番目の峰の標高を表す整数 H_i が N 個、スペース区切りで与えられる。
出力
撮影候補の区間に含まれる峰の数の最大値を 1 行で出力せよ。撮影候補が存在しない場合は 0 を出力せよ。
入力例 1
7 4 1 3 5 4 2 6 1
出力例 1
5
入力例 2
5 10 1 2 3 4 5
出力例 2
0
入力例 3
15 20 10 15 30 25 20 20 21 22 19 18 40 35 30 28 50
出力例 3
5
入力例 4
50 100 500 480 460 470 520 610 700 650 600 550 550 560 570 580 590 300 310 320 330 340 330 320 310 305 1000 900 800 700 600 500 400 410 420 430 440 450 460 470 480 490 490 100 200 300 400 350 250 150 50 25
出力例 4
9
入力例 5
1 1000000000 1000000000
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is a mountain photographer. There are N mountain peaks lined up in a straight line from west to east, and the elevation of the i-th peak from the west is H_i.
Takahashi wants to take a photo of the peaks contained in an interval [l, r] (1 \leq l \leq r \leq N). Here, the interval [l, r] refers to the contiguous sequence of peaks consisting of the l-th, (l+1)-th, \ldots, r-th peaks from the west.
To take a beautiful photo, the sequence of peaks must be mountain-shaped.
An interval [l, r] of peaks is mountain-shaped if there exists an integer k (l \leq k \leq r) such that:
H_l < H_{l+1} < \cdots < H_k > H_{k+1} > \cdots > H_r
That is, the elevation strictly increases from l to k, and strictly decreases from k to r. In particular, if l = r (only one peak), it is also mountain-shaped. If k = l, the entire sequence is strictly decreasing, and if k = r, the entire sequence is strictly increasing; these cases are also considered mountain-shaped.
(Note: Because the definition of mountain-shaped uses strict inequalities, an interval containing adjacent peaks of equal elevation (H_i = H_{i+1}) cannot be mountain-shaped.)
Furthermore, Takahashi wants to take a dynamic photo, so he only considers an interval as a shooting candidate if it is mountain-shaped and the difference between the maximum and minimum elevations in the interval, i.e.,
\max(H_l, H_{l+1}, \ldots, H_r) - \min(H_l, H_{l+1}, \ldots, H_r)
is at least K.
If there are any shooting candidate intervals, find the maximum number of peaks r - l + 1 contained in such an interval. If no shooting candidates exist, output 0.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq K \leq 10^9
- 1 \leq H_i \leq 10^9
- H_i are not necessarily distinct.
- All input values are integers.
Input
N K H_1 H_2 \ldots H_N
- The first line contains the integer N, representing the number of peaks, and the integer K, representing the lower bound of the elevation difference, separated by a space.
- The second line contains N space-separated integers, where the i-th integer represents the elevation H_i of the i-th peak from the west.
Output
Print the maximum number of peaks in a shooting candidate interval in a single line. If no shooting candidates exist, output 0.
Sample Input 1
7 4 1 3 5 4 2 6 1
Sample Output 1
5
Sample Input 2
5 10 1 2 3 4 5
Sample Output 2
0
Sample Input 3
15 20 10 15 30 25 20 20 21 22 19 18 40 35 30 28 50
Sample Output 3
5
Sample Input 4
50 100 500 480 460 470 520 610 700 650 600 550 550 560 570 580 590 300 310 320 330 340 330 320 310 305 1000 900 800 700 600 500 400 410 420 430 440 450 460 470 480 490 490 100 200 300 400 350 250 150 50 25
Sample Output 4
9
Sample Input 5
1 1000000000 1000000000
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は大学の印刷センターで管理者として働いています。この印刷センターには N 台のプリンターがあり、それぞれ 1 から N までの番号が付けられています。
各プリンター i には、1回の印刷で処理できる最大ページ数 W_i と、1件の依頼を処理するのにかかる時間 T_i が定められています。依頼のページ数によらず、プリンター i で1件の依頼を処理する時間は常に T_i です。
今日、印刷センターには M 件の印刷依頼が届きました。各依頼 j には、印刷すべきページ数 P_j が指定されています。
1つの依頼は分割できず、そのページ数以上の最大ページ数を持つプリンター1台で処理しなければなりません。すなわち、依頼 j をプリンター i で処理するには W_i \geq P_j である必要があります。
各プリンターは同時に1つの依頼しか処理できません。プリンターは依頼の処理が終わると、すぐに次の依頼を処理できます。すべてのプリンターは時刻 0 から稼働可能です。各依頼はどの時刻に処理を開始しても構いませんが、すべての依頼を処理しなければなりません。
高橋君は、すべての依頼を処理し終える最も早い時刻、すなわち最後の依頼の処理が完了する時刻を最小化したいと考えています。
すべての M 件の依頼を処理できる場合は、すべての依頼が完了する最も早い時刻を出力してください。処理できない依頼が存在する場合、すなわちどのプリンターでも処理できないページ数の依頼がある場合は -1 を出力してください。
制約
- 1 \leq N, M
- N + M \leq 2 \times 10^5
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数である。
入力
N M W_1 T_1 W_2 T_2 : W_N T_N P_1 P_2 : P_M
- 1 行目には、プリンターの台数を表す N と、印刷依頼の件数を表す M が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各プリンターの情報が与えられる。
- 1 + i 行目には、プリンター i の最大ページ数 W_i と印刷時間 T_i がスペース区切りで与えられる。
- N + 2 行目から N + M + 1 行目では、各依頼のページ数が与えられる。
- N + 1 + j 行目には、依頼 j のページ数 P_j が与えられる。
出力
すべての依頼を処理できる場合は、すべての依頼が完了する最も早い時刻を 1 行で出力せよ。処理できない依頼が存在する場合は -1 を 1 行で出力せよ。
入力例 1
2 3 10 5 20 7 8 15 10
出力例 1
10
入力例 2
2 3 5 3 10 2 4 11 8
出力例 2
-1
入力例 3
5 10 30 8 10 3 50 15 20 6 40 10 5 12 18 25 35 45 9 20 30 40
出力例 3
20
入力例 4
10 25 100 12 50 5 200 30 80 9 150 18 60 7 120 14 40 4 180 25 90 10 35 42 58 61 79 83 99 101 119 121 145 149 151 175 180 190 200 50 50 75 110 130 160 20 100
出力例 4
90
入力例 5
1 1 1000000000 1000000000 1000000000
出力例 5
1000000000
Score : 400 pts
Problem Statement
Takahashi works as an administrator at a university printing center. In this printing center, there are N printers, numbered 1 to N.
Each printer i has a maximum page capacity W_i that it can process in a single print job, and a processing time T_i required to process one job. Regardless of the number of pages in a job, the time required for printer i to process a single job is always T_i.
Today, M print jobs have arrived at the printing center. Each job j has a specified number of pages P_j to be printed.
A single job cannot be split and must be processed by a single printer whose maximum page capacity is at least the number of pages in the job. That is, to process job j with printer i, we must have W_i \geq P_j.
Each printer can process at most one job at a time. As soon as a printer finishes processing a job, it can immediately start processing the next one. All printers are available starting from time 0. Each job can start processing at any time, but all jobs must be processed.
Takahashi wants to minimize the earliest time by which all jobs are processed, i.e., the completion time of the last job.
If it is possible to process all M jobs, output the earliest time by which all jobs can be completed. If there is any job that cannot be processed (i.e., there is a job with a number of pages that no printer can handle), output -1.
Constraints
- 1 \leq N, M
- N + M \leq 2 \times 10^5
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq T_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers.
Input
N M W_1 T_1 W_2 T_2 : W_N T_N P_1 P_2 : P_M
- The first line contains N, the number of printers, and M, the number of print jobs, separated by a space.
- The next N lines, from the 2nd line to the (N + 1)-th line, provide the information of each printer.
- The (1 + i)-th line contains W_i, the maximum page capacity of printer i, and T_i, its processing time, separated by a space.
- The next M lines, from the (N + 2)-th line to the (N + M + 1)-th line, provide the number of pages for each job.
- The (N + 1 + j)-th line contains P_j, the number of pages for job j.
Output
If it is possible to process all jobs, output the earliest time by which all jobs can be completed in a single line. If there is a job that cannot be processed, output -1 in a single line.
Sample Input 1
2 3 10 5 20 7 8 15 10
Sample Output 1
10
Sample Input 2
2 3 5 3 10 2 4 11 8
Sample Output 2
-1
Sample Input 3
5 10 30 8 10 3 50 15 20 6 40 10 5 12 18 25 35 45 9 20 30 40
Sample Output 3
20
Sample Input 4
10 25 100 12 50 5 200 30 80 9 150 18 60 7 120 14 40 4 180 25 90 10 35 42 58 61 79 83 99 101 119 121 145 149 151 175 180 190 200 50 50 75 110 130 160 20 100
Sample Output 4
90
Sample Input 5
1 1 1000000000 1000000000 1000000000
Sample Output 5
1000000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君は登山が趣味で、山道の「なめらかさ」に興味を持っています。
高橋君は山道を正の整数で表現します。正の整数 X の十進表記が k 桁であるとし、最上位桁(左端)から順に各桁の数字を d_1, d_2, \ldots, d_k とします。高橋君はこれらの数字を、左から右へ一列に並んだ k 個の地点の標高レベルと見なしています。すなわち、第 i 地点の標高レベルは d_i(0 以上 9 以下の整数)です。ただし X は先頭に余分な 0 を含まない正の整数であるため、d_1 は 1 以上 9 以下です。
このとき、隣接する2地点間の標高差を順に並べた列
e_1, e_2, \ldots, e_{k-1} \quad \text{ただし} \quad e_i = |d_i - d_{i+1}| \quad (1 \leq i \leq k-1)
を「X の段差列」と呼びます。k = 1(X が1桁の数)のとき、段差列は空列です。
高橋君は、段差列が単調非減少である正の整数を「スムーズな山道」と呼んでいます。ここで単調非減少とは、すべての 1 \leq i \leq k-2 に対して e_i \leq e_{i+1} が成り立つことをいいます。直感的には、歩き始めは段差が小さく、進むにつれて段差が大きくなっても構わないが、途中で段差が小さくなることがない山道です。X が1桁の数(段差列が空列)または2桁の数(段差列の要素が1つだけ)の場合は、条件を満たすべき i が存在しないため、常にスムーズな山道です。
例:
- X = 1234 の段差列は (|1-2|,\ |2-3|,\ |3-4|) = (1, 1, 1) です。1 \leq 1 \leq 1 と単調非減少なので、スムーズな山道です。
- X = 3152 の段差列は (|3-1|,\ |1-5|,\ |5-2|) = (2, 4, 3) です。4 > 3 となる箇所があるため、スムーズな山道ではありません。
正の整数 L と R が与えられます。L 以上 R 以下の整数のうち、スムーズな山道であるものの個数を 10^9 + 7 で割った余りを求めてください。
制約
- 1 \leq L \leq R < 10^{5000}
- L および R は先頭に余分な 0 を含まない十進表記で与えられる。したがって、L の桁数および R の桁数はいずれも 5000 以下である。
入力
L R
- 1 行目には、範囲の下限を表す正の整数 L が十進表記で与えられる。
- 2 行目には、範囲の上限を表す正の整数 R が十進表記で与えられる。
出力
L 以上 R 以下のスムーズな山道の個数を 10^9 + 7 で割った余りを1行で出力せよ。
入力例 1
1 20
出力例 1
20
入力例 2
98 123
出力例 2
24
入力例 3
1000 50000
出力例 3
6624
入力例 4
123456789012345678901234567890 987654321098765432109876543210
出力例 4
426844825
入力例 5
1 1
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi enjoys mountain climbing as a hobby and is interested in the "smoothness" of mountain paths.
Takahashi represents mountain paths as positive integers. Let the decimal representation of a positive integer X have k digits, and let the digits from the most significant digit (leftmost) to the least significant be d_1, d_2, \ldots, d_k. Takahashi regards these digits as elevation levels of k points arranged in a row from left to right. That is, the elevation level of the i-th point is d_i (an integer between 0 and 9, inclusive). Since X is a positive integer without leading zeros, d_1 is between 1 and 9, inclusive.
The sequence of elevation differences between adjacent points, listed in order,
e_1, e_2, \ldots, e_{k-1} \quad \text{where} \quad e_i = |d_i - d_{i+1}| \quad (1 \leq i \leq k-1)
is called the "step sequence of X". When k = 1 (i.e., X is a single-digit number), the step sequence is an empty sequence.
Takahashi calls a positive integer whose step sequence is monotonically non-decreasing a "smooth mountain path." Here, monotonically non-decreasing means that e_i \leq e_{i+1} holds for all 1 \leq i \leq k-2. Intuitively, this is a mountain path where the steps are small at the beginning of the walk and may become larger as you proceed, but never become smaller along the way. When X is a single-digit number (empty step sequence) or a two-digit number (step sequence with only one element), there is no i for which the condition must hold, so it is always a smooth mountain path.
Examples:
- The step sequence of X = 1234 is (|1-2|,\ |2-3|,\ |3-4|) = (1, 1, 1). Since 1 \leq 1 \leq 1, it is monotonically non-decreasing, so this is a smooth mountain path.
- The step sequence of X = 3152 is (|3-1|,\ |1-5|,\ |5-2|) = (2, 4, 3). Since there is a position where 4 > 3, this is not a smooth mountain path.
You are given positive integers L and R. Find the number of integers between L and R (inclusive) that are smooth mountain paths, modulo 10^9 + 7.
Constraints
- 1 \leq L \leq R < 10^{5000}
- L and R are given in decimal notation without leading zeros. Therefore, the number of digits of L and the number of digits of R are each at most 5000.
Input
L R
- The first line contains the positive integer L representing the lower bound of the range, given in decimal notation.
- The second line contains the positive integer R representing the upper bound of the range, given in decimal notation.
Output
Output in one line the number of smooth mountain paths between L and R (inclusive), modulo 10^9 + 7.
Sample Input 1
1 20
Sample Output 1
20
Sample Input 2
98 123
Sample Output 2
24
Sample Input 3
1000 50000
Sample Output 3
6624
Sample Input 4
123456789012345678901234567890 987654321098765432109876543210
Sample Output 4
426844825
Sample Input 5
1 1
Sample Output 5
1