#include #include using namespace std; #define int unsigned long long const int MAX_N = 60; int n, k; string s; bool visited[MAX_N]; int memo[MAX_N]; bool f(int); bool rec(int, int, int); bool f(int x) { memset(visited, false, sizeof(visited)); memset(memo, 0, sizeof(memo)); return rec(0, x, 0); } bool rec(int ice_i, int available_num, int accum) { if (accum >= k) { return true; } else if (available_num == 0) { return false; } else if (visited[ice_i]) { if (memo[ice_i] <= available_num) return true; else { int dec = memo[ice_i] - available_num; // n * (memo[ice_i] / dec)分だけ使える memset(visited, false, sizeof(visited)); memset(memo, 0, sizeof(memo)); return rec(ice_i, available_num % dec, accum + n * (available_num / dec)); } } else { visited[ice_i] = true; memo[ice_i] = available_num; return rec((ice_i + 1) % n, available_num + s[ice_i] - '1', accum + 1); } } signed main() { cin >> n >> k; cin >> s; int lb = 0, ub = k; while (ub - lb > 1) { int mid = (lb + ub) / 2; if (f(mid)) { ub = mid; } else { lb = mid; } } cout << ub << endl; return 0; }