A13727 | x-prime Substrings
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an integer value $x$ and a string $s$ consisting of digits from $1$ to $9$ inclusive.
A substring of a string is a contiguous subsequence of that string.
Let $f(l, r)$ be the sum of digits of a substring $s[l..r]$ .
Let's call substring $s[l_1..r_1]$ $x$ -prime if
- $f(l_1, r_1) = x$ ;
- there are no values $l_2, r_2$ such that
- $l_1 \le l_2 \le r_2 \le r_1$ ;
- $f(l_2, r_2) \neq x$ ;
- $x$ is divisible by $f(l_2, r_2)$ .
You are allowed to erase some characters from the string. If you erase a character, the two resulting parts of the string are concatenated without changing their order.
What is the minimum number of characters you should erase from the string so that there are no $x$ -prime substrings in it? If there are no $x$ -prime substrings in the given string $s$ , then print $0$ .
A substring of a string is a contiguous subsequence of that string.
Let $f(l, r)$ be the sum of digits of a substring $s[l..r]$ .
Let's call substring $s[l_1..r_1]$ $x$ -prime if
- $f(l_1, r_1) = x$ ;
- there are no values $l_2, r_2$ such that
- $l_1 \le l_2 \le r_2 \le r_1$ ;
- $f(l_2, r_2) \neq x$ ;
- $x$ is divisible by $f(l_2, r_2)$ .
You are allowed to erase some characters from the string. If you erase a character, the two resulting parts of the string are concatenated without changing their order.
What is the minimum number of characters you should erase from the string so that there are no $x$ -prime substrings in it? If there are no $x$ -prime substrings in the given string $s$ , then print $0$ .
输入格式
The first line contains a string $s$ ( $1 \le |s| \le 1000$ ). $s$ contains only digits from $1$ to $9$ inclusive.
The second line contains an integer $x$ ( $1 \le x \le 20$ ).
The second line contains an integer $x$ ( $1 \le x \le 20$ ).
输出格式
Print a single integer — the minimum number of characters you should erase from the string so that there are no $x$ -prime substrings in it. If there are no $x$ -prime substrings in the given string $s$ , then print $0$ .
输入输出样例
输入 #1
116285317 8
输出 #1
2
输入 #2
314159265359 1
输出 #2
2
输入 #3
13 13
输出 #3
0
输入 #4
3434343434 7
输出 #4
5
In the first example there are two $8$ -prime substrings "8" and "53". You can erase these characters to get rid of both: "116285317". The resulting string "1162317" contains no $8$ -prime substrings. Removing these characters is also a valid answer: "116285317".
In the second example you just have to erase both ones.
In the third example there are no $13$ -prime substrings. There are no substrings with the sum of digits equal to $13$ at all.
In the fourth example you can have neither "34", nor "43" in a string. Thus, you have to erase either all threes or all fours. There are $5$ of each of them, so it doesn't matter which.
In the second example you just have to erase both ones.
In the third example there are no $13$ -prime substrings. There are no substrings with the sum of digits equal to $13$ at all.
In the fourth example you can have neither "34", nor "43" in a string. Thus, you have to erase either all threes or all fours. There are $5$ of each of them, so it doesn't matter which.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted