A12740 | Subsequences (hard version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string $s$ consisting of $n$ lowercase Latin letters.
In one move you can take any subsequence $t$ of the given string and add it to the set $S$ . The set $S$ can't contain duplicates. This move costs $n - |t|$ , where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string $s$ consisting of $n$ lowercase Latin letters.
In one move you can take any subsequence $t$ of the given string and add it to the set $S$ . The set $S$ can't contain duplicates. This move costs $n - |t|$ , where $|t|$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set $S$ of size $k$ or report that it is impossible to do so.
输入格式
The first line of the input contains two integers $n$ and $k$ ( $1 \le n \le 100, 1 \le k \le 10^{12}$ ) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters.
The second line of the input contains a string $s$ consisting of $n$ lowercase Latin letters.
输出格式
Print one integer — if it is impossible to obtain the set $S$ of size $k$ , print -1. Otherwise, print the minimum possible total cost to do it.
输入输出样例
输入 #1
4 5 asdf
输出 #1
4
输入 #2
5 6 aaaaa
输出 #2
15
输入 #3
5 7 aaaaa
输出 #3
-1
输入 #4
10 100 ajihiushda
输出 #4
233
In the first example we can generate $S$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $S$ is $0$ and the cost of the others is $1$ . So the total cost of $S$ is $4$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted