A - November 30

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 100

問題文

20191130 日のような、ある月の最後の日を「月末日」といいます。

整数 M_1, D_1, M_2, D_2 が入力されます。
2019M_1D_1 日の次の日が 2019M_2D_2 日であることが分かっています。
2019M_1D_1 日が月末日であるか判定してください。

制約

  • 2019M_1D_1 日、2019M_2D_2 日はともにグレゴリオ暦において存在する日付である。
  • 2019M_1D_1 日の次の日は 2019M_2D_2 日である。

入力

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

M_1 D_1
M_2 D_2

出力

2019M_1D_1 日が月末日である場合は 1、そうでない場合は 0 と出力してください。


入力例 1

11 16
11 17

出力例 1

0

1116 日は月末日ではありません。


入力例 2

11 30
12 1

出力例 2

1

1130 日は月末日です。

Score: 100 points

Problem Statement

In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.

Integers M_1, D_1, M_2, and D_2 will be given as input.
It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.
Determine whether the date 2019-M_1-D_1 is the last day of a month.

Constraints

  • Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.
  • The date 2019-M_2-D_2 follows 2019-M_1-D_1.

Input

Input is given from Standard Input in the following format:

M_1 D_1
M_2 D_2

Output

If the date 2019-M_1-D_1 is the last day of a month, print 1; otherwise, print 0.


Sample Input 1

11 16
11 17

Sample Output 1

0

November 16 is not the last day of a month.


Sample Input 2

11 30
12 1

Sample Output 2

1

November 30 is the last day of November.

B - Tax Rate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 200

問題文

高橋君は ABC 洋菓子店でアップルパイをひとつ買い、そのとき N 円を支払ったことを覚えています。

この店で食料品を購入する際には 8 パーセントの消費税が課されます。すなわち、税抜価格 X 円のアップルパイを買う際には X \times 1.08 円 (小数点以下切り捨て) を支払わなければなりません。

高橋君はアップルパイの税抜価格 X を忘れてしまい、これを知りたくなりました。N を入力として X を求めるプログラムを書いてください。なお、X は整数とします。

ただし、考えられる税抜価格が複数存在する場合はそのうちのいずれか 1 つを求めてください。また、高橋君が支払った金額 N を覚え間違えている可能性もあるので、アップルパイの税抜価格として考えられるものが存在しない場合はその旨を報告してください。

制約

  • 1 \leq N \leq 50000
  • N は整数

入力

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

N

出力

アップルパイの税抜価格 X として考えられるものが存在する場合は、そのような整数 X の値を 1 つ出力してください。
そのような値が複数存在する場合は、そのうちのどれを出力しても構いません。
アップルパイの税抜価格として考えられるものが存在しない場合は、:( と出力してください。


入力例 1

432

出力例 1

400

アップルパイの税抜価格が 400 円のとき、これを買うために支払うべき金額は 400 \times 1.08 = 432 円です。
税抜価格がそれ以外のとき、支払うべき金額は 432 円となりません。


入力例 2

1079

出力例 2

:(

支払うべき金額が 1079 円となるような税抜価格は存在しません。


入力例 3

1001

出力例 3

927

アップルパイの税抜価格が 927 円のとき、これを買うために支払うべき金額は 927 \times 1.08 = 1001.16 の小数点以下を切り捨てた 1001 円です。

Score: 200 points

Problem Statement

Takahashi bought a piece of apple pie at ABC Confiserie. According to his memory, he paid N yen (the currency of Japan) for it.

The consumption tax rate for foods in this shop is 8 percent. That is, to buy an apple pie priced at X yen before tax, you have to pay X \times 1.08 yen (rounded down to the nearest integer).

Takahashi forgot the price of his apple pie before tax, X, and wants to know it again. Write a program that takes N as input and finds X. We assume X is an integer.

If there are multiple possible values for X, find any one of them. Also, Takahashi's memory of N, the amount he paid, may be incorrect. If no value could be X, report that fact.

Constraints

  • 1 \leq N \leq 50000
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If there are values that could be X, the price of the apple pie before tax, print any one of them.
If there are multiple such values, printing any one of them will be accepted.
If no value could be X, print :(.


Sample Input 1

432

Sample Output 1

400

If the apple pie is priced at 400 yen before tax, you have to pay 400 \times 1.08 = 432 yen to buy one.
Otherwise, the amount you have to pay will not be 432 yen.


Sample Input 2

1079

Sample Output 2

:(

There is no possible price before tax for which you have to pay 1079 yen with tax.


Sample Input 3

1001

Sample Output 3

927

If the apple pie is priced 927 yen before tax, by rounding down 927 \times 1.08 = 1001.16, you have to pay 1001 yen.

C - 100 to 105

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 300

問題文

AtCoder 商店では、以下の 6 種類の品物が 1000000 個ずつ売られています。

  • 1100 円のおにぎり
  • 1101 円のサンドイッチ
  • 1102 円のクッキー
  • 1103 円のケーキ
  • 1104 円の飴
  • 1105 円のパソコン

高橋君は、合計価格がちょうど X 円となるような買い物をしたいです。そのような買い方が存在するか判定してください。
ただし、消費税は考えないものとします。

制約

  • 1 \leq X \leq 100000
  • X は整数

入力

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

X

出力

合計値段がちょうど X 円となるような買い物をすることが可能な場合は 1、そうでない場合は 0 と出力してください。


入力例 1

615

出力例 1

1

例えば、6 種類の品物を 1 個ずつ買った場合、合計価格は 100+101+102+103+104+105=615 円となります。


入力例 2

217

出力例 2

0

どのように品物を買っても、合計価格を 217 円にすることはできません。

Score: 300 points

Problem Statement

AtCoder Mart sells 1000000 of each of the six items below:

  • Riceballs, priced at 100 yen (the currency of Japan) each
  • Sandwiches, priced at 101 yen each
  • Cookies, priced at 102 yen each
  • Cakes, priced at 103 yen each
  • Candies, priced at 104 yen each
  • Computers, priced at 105 yen each

Takahashi wants to buy some of them that cost exactly X yen in total. Determine whether this is possible.
(Ignore consumption tax.)

Constraints

  • 1 \leq X \leq 100000
  • X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If it is possible to buy some set of items that cost exactly X yen in total, print 1; otherwise, print 0.


Sample Input 1

615

Sample Output 1

1

For example, we can buy one of each kind of item, which will cost 100+101+102+103+104+105=615 yen in total.


Sample Input 2

217

Sample Output 2

0

No set of items costs 217 yen in total.

D - Lucky PIN

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 400

問題文

AtCoder 社は、オフィスの入り口に 3 桁の暗証番号を設定することにしました。

AtCoder 社には N 桁のラッキーナンバー S があります。社長の高橋君は、S から N-3 桁を消して残りの 3 桁を左から読んだものを暗証番号として設定することにしました。

このとき、設定されうる暗証番号は何種類あるでしょうか?

ただし、ラッキーナンバーや暗証番号はいずれも 0 から始まっても良いものとします。

制約

  • 4 \leq N \leq 30000
  • S は半角数字からなる長さ N の文字列

入力

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

N
S

出力

設定されうる暗証番号の種類数を出力してください。


入力例 1

4
0224

出力例 1

3

高橋君には以下の選択肢があります。

  • S1 桁目を消し、暗証番号を 224 とする。
  • S2 桁目を消し、暗証番号を 024 とする。
  • S3 桁目を消し、暗証番号を 024 とする。
  • S4 桁目を消し、暗証番号を 022 とする。

よって、設定されうる暗証番号は 022, 024, 2243 種類です。


入力例 2

6
123123

出力例 2

17

入力例 3

19
3141592653589793238

出力例 3

329

Score: 400 points

Problem Statement

AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code.

The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code.

How many different PIN codes can he set this way?

Both the lucky number and the PIN code may begin with a 0.

Constraints

  • 4 \leq N \leq 30000
  • S is a string of length N consisting of digits.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of different PIN codes Takahashi can set.


Sample Input 1

4
0224

Sample Output 1

3

Takahashi has the following options:

  • Erase the first digit of S and set 224.
  • Erase the second digit of S and set 024.
  • Erase the third digit of S and set 024.
  • Erase the fourth digit of S and set 022.

Thus, he can set three different PIN codes: 022, 024, and 224.


Sample Input 2

6
123123

Sample Output 2

17

Sample Input 3

19
3141592653589793238

Sample Output 3

329
E - Colorful Hats 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 500

問題文

N 人の人が一列に並んでおり、前から順に 1, 2, 3, ..., N と番号が付けられています。それぞれの人は、赤色・青色・緑色のいずれかの帽子を被っています。

さて、番号 i の人は以下の発言をしました。

  • 「自分より前に、自分と同じ色の帽子を被っている人はちょうど A_i 人いる。」

すべての人の発言が正しいとして、N 人の人の帽子の色の組合せとして考えられるものが何通りあるか求めてください。

ただし、答えがとても大きくなる場合があるので、代わりに 1000000007 で割った余りを計算してください。

制約

  • 1 \leq N \leq 100000
  • 0 \leq A_i \leq N-1
  • 入力中の値はすべて整数

入力

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

N
A_1 A_2 A_3 ... A_N

出力

N 人の帽子の色の組合せとして考えられるものの個数を 1000000007 で割った余りを出力してください。


入力例 1

6
0 1 2 3 4 5

出力例 1

3

以下の 3 通りの組合せが考えられます。

  • 赤, 赤, 赤, 赤, 赤, 赤
  • 青, 青, 青, 青, 青, 青
  • 緑, 緑, 緑, 緑, 緑, 緑

入力例 2

3
0 0 0

出力例 2

6

入力例 3

54
0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17

出力例 3

115295190

Score: 500 points

Problem Statement

N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green.

The person numbered i says:

  • "In front of me, exactly A_i people are wearing hats with the same color as mine."

Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats.

Since the count can be enormous, compute it modulo 1000000007.

Constraints

  • 1 \leq N \leq 100000
  • 0 \leq A_i \leq N-1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print the number of possible combinations of colors of the N people's hats, modulo 1000000007.


Sample Input 1

6
0 1 2 3 4 5

Sample Output 1

3

We have three possible combinations, as follows:

  • Red, Red, Red, Red, Red, Red
  • Blue, Blue, Blue, Blue, Blue, Blue
  • Green, Green, Green, Green, Green, Green

Sample Input 2

3
0 0 0

Sample Output 2

6

Sample Input 3

54
0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17

Sample Output 3

115295190
F - Interval Running

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 600

問題文

高橋君と青木君は、西から東に向けて一直線に果てしなく続くランニングコースで長距離走の練習をしています。

彼らは同じ地点から同時にスタートし、東に向かって次のように移動します。

  • 高橋君は最初の T_1 分間、分速 A_1 メートルで走り、次の T_2 分間、分速 A_2 メートルで走り、これを交互にいつまでも繰り返す。
  • 青木君は最初の T_1 分間、分速 B_1 メートルで走り、次の T_2 分間、分速 B_2 メートルで走り、これを交互にいつまでも繰り返す。

さて、高橋君と青木君は何回出会う、すなわち、同じ位置に来るでしょうか?スタート地点にいる時は数えません。無限回出会う場合は、その旨を報告してください。

制約

  • 1 \leq T_i \leq 100000
  • 1 \leq A_i \leq 10^{10}
  • 1 \leq B_i \leq 10^{10}
  • A_1 \neq B_1
  • A_2 \neq B_2
  • 入力中の値はすべて整数

入力

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

T_1 T_2
A_1 A_2
B_1 B_2

出力

高橋君と青木君が出会う回数を出力してください。
ただし、無限回出会う場合は代わりに infinity と出力してください。


入力例 1

1 2
10 10
12 4

出力例 1

1

彼らはスタートしてから \frac{4}{3} 分後に 1 回だけ、スタート地点から \frac{40}{3} メートルの位置で出会います。


入力例 2

100 1
101 101
102 1

出力例 2

infinity

彼らはスタートしてから 101, 202, 303, 404, 505, 606, ... 分後に出会うので、無限回出会うことになります。


入力例 3

12000 15700
3390000000 3810000000
5550000000 2130000000

出力例 3

113

入力中の値は 32 ビット整数型に収まらないことがあります。

Score: 600 points

Problem Statement

Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.

They start simultaneously at the same point and moves as follows towards the east:

  • Takahashi runs A_1 meters per minute for the first T_1 minutes, then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.
  • Aoki runs B_1 meters per minute for the first T_1 minutes, then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.

Constraints

  • 1 \leq T_i \leq 100000
  • 1 \leq A_i \leq 10^{10}
  • 1 \leq B_i \leq 10^{10}
  • A_1 \neq B_1
  • A_2 \neq B_2
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T_1 T_2
A_1 A_2
B_1 B_2

Output

Print the number of times Takahashi and Aoki will meet each other.
If they meet infinitely many times, print infinity instead.


Sample Input 1

1 2
10 10
12 4

Sample Output 1

1

They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start.


Sample Input 2

100 1
101 101
102 1

Sample Output 2

infinity

They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.


Sample Input 3

12000 15700
3390000000 3810000000
5550000000 2130000000

Sample Output 3

113

The values in input may not fit into a 32-bit integer type.