QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 1024 MB 總分: 100 可 Hack ✓

#18395. 選擇子序列

统计

長度為 $N$ 的數列 $A = \{a_1, a_2, a_3, \cdots, a_N\}$ 是一個首項為 $a$、公差為 $d$ 的有限等差數列。

對於正整數 $M$,請找出 $A$ 中和為 $M$ 的最長子序列。

子序列是指從給定數列中刪除 $0$ 個或多個元素,並保持原順序所得到的數列。

第一行包含四個整數 $N, a, d, M$,以空格分隔。$(1 \leq N, a, d \leq 10^6;$ $1 \leq M \leq 10^{18})$

第一行輸出和為 $M$ 的 $A$ 的最長子序列長度 $L$。

第二行輸出該子序列的 $L$ 個元素,以空格分隔。若有多種可能的答案,輸出其中任意一個即可。

若不存在這樣的子序列,則第一行輸出 $-1$。

範例

輸入格式 1

5 2 2 10

輸出格式 1

2
4 6

輸入格式 2

3 1 2 7

輸出格式 2

-1

說明

在範例 1 中,給定的數列為 $A = \{2, 4, 6, 8, 10\}$。排除長度條件後,可能的 $B$ 候選為 $\{10\}$, $\{2, 8\}$, $\{4, 6\}$。其中長度最長的子序列為 $\{2, 8\}$ 與 $\{4, 6\}$,因此輸出其中任意一個即可。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.