等紅綠燈 (APCS 2025-01)

Points 100 1.0s 256M

操場起跑線上有一個紅綠燈,綠燈為 \(a\) 秒、紅燈為 \(b\) 秒,依照「綠燈、紅燈」的順序循環(一開始是綠燈)。

有 \(n\) 個小朋友,從操場的起跑線一起騎腳踏車起跑,他們各自騎完一圈的時間為 \(t_1, t_2, \ldots, t_n\)。若騎到終點時正好是紅燈,必須等到紅燈結束、變為綠燈才可以停止騎車。求這 \(n\) 個小朋友總共需要等待多少秒的紅燈。

註:騎到終點時如果紅燈剛好開始,就要再等 \(b\) 秒。

輸入格式

第一行有兩個正整數 \(a\)、\(b\)。

第二行有一個正整數 \(n\)。

第三行有 \(n\) 個正整數,依序代表每個小朋友騎完一圈的時間。

限制

  • \(1 \le a, b \le 100\)
  • \(1 \le n \le 30\)
  • 每個 \(t_i\) 為正整數且不超過 \(1000\)

輸出格式

輸出這 \(n\) 個小朋友總共需要等待多少秒的紅燈。

評分說明

每一筆測試資料的執行時間限制均為 \(1\) 秒,依正確通過的測試資料筆數給分。其中:

  • 第 1 子題組(\(60\) 分):\(a = b = 10\) 且 \(n = 1\)。
  • 第 2 子題組(\(40\) 分):無限制。

範例輸入 1

10 10
1
14

範例輸出 1

6

(說明)循環為 \(20\) 秒,前 \(10\) 秒綠燈、後 \(10\) 秒紅燈。第 \(14\) 秒落在紅燈(第 \(11\)~\(20\) 秒),要等 \(20 - 14 = 6\) 秒。

範例輸入 2

4 3
3
12 16 25

範例輸出 2

5

(說明)循環 \(7\) 秒(綠 \(4\)、紅 \(3\))。\(12 \bmod 7 = 5 \ge 4\) 紅燈等 \(2\);\(16 \bmod 7 = 2 < 4\) 綠燈不等;\(25 \bmod 7 = 4 \ge 4\) 紅燈等 \(3\)。共 \(2 + 3 = 5\)。

題目來源

APCS 2025 年 1 月實作題第 1 題。

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.