Universal Cup Judging System

Universal Cup

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

眾所周知,你正在參加中國構造題競賽(Chinese Constructive Problem Contest, CCPC),毫無疑問,作為「構造王國」的命題學校——中山大學,非常期待你挑戰一些相關的問題。

小 U 是構造王國的一名學生。在演算法課堂上,小 U 學習了 MCOP(矩陣鏈乘順序問題,Matrix Chain Ordering Problem)。以下是 MCOP 的介紹:

給定一個長度為 $n \ge 3$ 的正整數序列 $[w_1, w_2, \dots, w_n]$ 作為演算法的輸入。以下公式將 $n - 1$ 個矩陣相乘,最終得到一個新矩陣:

$$M = M_1 \times M_2 \times \cdots \times M_{n-1}$$

其中 $M_i$ 是一個大小為 $w_i \times w_{i+1}$ 的矩陣。矩陣乘法滿足結合律,因此改變相乘的順序會得到相同的最終矩陣 $M$,但所需的運算次數可能會有所不同。該問題的目標是在 $(n - 2)!$ 種可能的策略中找出一種最佳的乘法策略,使得矩陣相乘所需的總運算次數最小。這裡假設將大小為 $p \times q$ 與 $q \times r$ 的矩陣相乘會得到一個大小為 $p \times r$ 的矩陣,需要 $pqr$ 次運算。在最佳策略下計算 $M$ 所需的最小總運算次數記為 $\text{MCOP}([w_1, w_2, \dots, w_n])$。

小 U 在課堂上已經學會如何以足夠好的時間複雜度解決這個問題,但作為構造王國的學生,小 U 自然而然地想到了以下問題:

給定一個整數 $X$,滿足 $1 \le X \le 10^9$。你需要構造一個長度為 $l$ 的整數序列 $[a_1, a_2, \dots, a_l]$,滿足以下要求:

  • $3 \le l \le 6$;
  • 對於所有 $i \in [1, l]$,$1 \le a_i \le 4000$;
  • $\text{MCOP}([a_1, a_2, \dots, a_l]) = X$。

小 U 已經使用課堂上學到的演算法實作了一個檢查器(checker),而你需要做的是解決這個構造問題。

輸入

本題包含多組測試資料。輸入的第一行包含一個整數 $T$($1 \le T \le 500$),代表測試資料的組數。

對於每組測試資料,輸入包含一行,其中有一個整數 $X$($1 \le X \le 10^9$),其意義已在題目中給出。

輸出

對於每組測試資料:首先輸出一行包含一個整數 $l$($3 \le l \le 6$),代表構造序列的長度。接著輸出一行包含 $l$ 個整數,代表構造的序列。

可以證明對於任何輸入 $X$,總是存在滿足要求的構造。若有多種構造滿足要求,輸出其中任意一種即可。

範例

範例輸入 1

2
90
10

範例輸出 1

4
5 3 2 6
3
1 1 10

說明

對於第一組測試資料,構造的序列為 $[5, 3, 2, 6]$,代表需要對大小分別為 $5 \times 3$、$3 \times 2$ 與 $2 \times 6$ 的三個矩陣進行鏈乘。考慮兩種乘法策略:

  • 首先將 $M_1$ 與 $M_2$ 相乘得到一個大小為 $5 \times 2$ 的矩陣,接著與 $M_3$ 相乘,對應表示式 $(M_1 \times M_2) \times M_3$。此時的總運算次數為 $5 \times 3 \times 2 + 5 \times 2 \times 6 = 90$;
  • 首先將 $M_2$ 與 $M_3$ 相乘得到一個大小為 $3 \times 6$ 的矩陣,接著與 $M_1$ 相乘,對應表示式 $M_1 \times (M_2 \times M_3)$。此時的總運算次數為 $3 \times 2 \times 6 + 5 \times 3 \times 6 = 126$。

這裡的目標是最小化總運算次數,因此 $\text{MCOP}([5, 3, 2, 6]) = \min(90, 126) = 90$。

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.