local ffi = require("ffi") local C = ffi.C ffi.cdef[[ long long atoll(const char*); ]] local function lltonumber(str) return C.atoll(str) end local function getpattern(n, patall, idx) local used = {} local retary = {} local div = patall for i = 1, n do used[i] = false end for i = n, 1, -1 do div = div / (1LL * i) local v_idx = idx / div idx = idx % div local tmp_idx = 0 for j = 1, n do if not used[j] then if tmp_idx == v_idx then table.insert(retary, j) used[j] = true break else tmp_idx = tmp_idx + 1 end end end end return retary end local n, len = io.read():match("(%d+) (%d+)") n = lltonumber(n) len = tonumber(len) local tot = 1LL for i = 2, len do tot = tot * i end local ary = getpattern(len, tot, n) -- print(table.concat(ary, " ")) local ainv = {} for i = 1, len do ainv[i] = 0 end for i = 1, len do ainv[ary[i]] = i end -- print(table.concat(ainv, " ")) local used = {} for i = 1, len do used[i] = false end local ret = 0LL for i = 1, len do local add = 1LL for j = i + 1, len do add = add * (j - i) end for j = 1, ainv[i] - 1 do if not used[j] then ret = ret + add end end used[ainv[i]] = true end ret = tostring(ret):gsub("LL", "") print(ret)