Enumerate Sequences

AtCoder
IDabc367_c
Time2000ms
Memory256MB
Difficulty
Print all integer sequences of length $N$ that satisfy the following conditions, in ascending lexicographical order. * The $i$\-th element is between $1$ and $R_i$, inclusive. * The sum of all elements is a multiple of $K$. What is lexicographical order for sequences? A sequence $A = (A_1, \ldots, A_{|A|})$ is **lexicographically smaller** than $B = (B_1, \ldots, B_{|B|})$ if either 1. or 2. below holds: 1. $|A|<|B|$ and $(A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|})$. 2. There exists an integer $1\leq i\leq \min{|A|,|B|}$ such that both of the following are true: * $(A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})$ * $A_i < B_i$ ## Constraints * All input values are integers. * $1 \le N \le 8$ * $2 \le K \le 10$ * $1 \le R_i \le 5$ ## Input The input is given from Standard Input in the following format: $N$ $K$ $R_1$ $R_2$ $\dots$ $R_N$ [samples]
Samples
Input #1
3 2
2 1 3
Output #1
1 1 2
2 1 1
2 1 3

There are three sequences to be printed, which are $(1,1,2),(2,1,1),(2,1,3)$ in lexicographical order.
Input #2
1 2
1
Output #2
There may be no sequences to print.  
In this case, the output can be empty.
Input #3
5 5
2 3 2 3 2
Output #3
1 1 1 1 1
1 2 2 3 2
1 3 1 3 2
1 3 2 2 2
1 3 2 3 1
2 1 2 3 2
2 2 1 3 2
2 2 2 2 2
2 2 2 3 1
2 3 1 2 2
2 3 1 3 1
2 3 2 1 2
2 3 2 2 1
API Response (JSON)
{
  "problem": {
    "name": "Enumerate Sequences",
    "description": {
      "content": "Print all integer sequences of length $N$ that satisfy the following conditions, in ascending lexicographical order. *   The $i$\\-th element is between $1$ and $R_i$, inclusive. *   The sum of all el",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc367_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Print all integer sequences of length $N$ that satisfy the following conditions, in ascending lexicographical order.\n\n*   The $i$\\-th element is between $1$ and $R_i$, inclusive.\n*   The sum of all el...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments