Under construction

Still being built. Things may change

Nimber Sequence

Time limit1sMemory limit256 MB

Summary
Given a recurrence over nimbers using nim sum and nim product, find the m-th term a_m for m up to 10^18.
Level

Hard8 of 10

Topics
Matrix, Math, Divide and conquer
Solved
No attempts yet

Statement

Nimbers a_1,a_2,…,a_K−1a\_1, a\_2, \ldots, a\_{K-1}, b_1,b_2,…,b_5b\_1, b\_2, \ldots, b\_5, and c_1,c_2,…,c_5c\_1, c\_2, \ldots, c\_5 are given.

For all n≥Kn \ge K, the following holds.

a_n=(⨁_i=15a_n−i⊗b_i)⊕(⨁_i=15a_n−K+i⊗c_i)a\_n = \left(\bigoplus\_{i = 1}^{5} a\_{n - i} \otimes b\_i \right) \oplus \left(\bigoplus\_{i = 1}^{5} a\_{n - K + i} \otimes c\_i \right)

Find the value of a_ma\_m.

Input

The first line contains two positive integers KK and mm (6≤K≤1056 \le K \le 10^5, 1≤m≤10181 \le m \le 10^{18}).

The second line contains K−1K - 1 non-negative integers a_1,a_2,…,a_K−1a\_1, a\_2, \ldots, a\_{K - 1} (0≤a_i<2320 \le a\_i < 2^{32}).

The third line contains five non-negative integers b_1,b_2,…,b_5b\_1, b\_2, \ldots, b\_5 (0≤b_i<2320 \le b\_i < 2^{32}).

The fourth line contains five non-negative integers c_1,c_2,…,c_5c\_1, c\_2, \ldots, c\_5 (0≤c_i<2320 \le c\_i < 2^{32}).

Output

Output a single integer, the value of a_ma\_m.

Hint

Nimbers are non-negative integers associated with Nim games. This problem needs two operations.

  • ⊕\large\oplus is the nim sum: a⊕b=mex({a′⊕b∣0≤a′<a}∪{a⊕b′∣0≤b′<b})a \oplus b = \mathrm{mex}(\{a' \oplus b \mid 0 \le a' < a\} \cup \{a \oplus b' \mid 0 \le b' < b\}).
  • ⊗\large\otimes is the nim product: a⊗b=mex({(a′⊗b)⊕(a⊗b′)⊕(a′⊗b′)∣0≤a′<a, 0≤b′<b})a \otimes b = \mathrm{mex}(\{(a' \otimes b) \oplus (a \otimes b') \oplus (a' \otimes b') \mid 0 \le a' < a, \, 0 \le b' < b\}).

Here, mex(S)\mathrm{mex}(S) is the smallest non-negative integer dd that is not in SS.

Examples3

  1. Example 1

    Input
    6 1000000000000000000
    1 2 3 4 5
    1 0 0 0 0
    0 0 0 0 0
    
    Expected output
    5
    
  2. Example 2

    Input
    6 10
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    
    Expected output
    9
    
  3. Example 3

    Input
    11 123
    849 674 223 677 243 657 979 583 643 845
    979 282 313 567 433
    122 443 132 554 132
    
    Expected output
    32098