CF1400F.x-prime Substrings

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an integer value xx and a string ss consisting of digits from 11 to 99 inclusive.

A substring of a string is a contiguous subsequence of that string.

Let f(l,r)f(l, r) be the sum of digits of a substring s[l..r]s[l..r].

Let's call substring s[l1..r1]s[l_1..r_1] xx-prime if

  • f(l1,r1)=xf(l_1, r_1) = x;
  • there are no values l2,r2l_2, r_2 such that
    • l1≤l2≤r2≤r1l_1 \le l_2 \le r_2 \le r_1;
    • f(l2,r2)≠xf(l_2, r_2) \neq x;
    • xx is divisible by f(l2,r2)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 xx-prime substrings in it? If there are no xx-prime substrings in the given string ss, then print 00.

给你一个整数 xx 和一个由数字 11 到 99(含)组成的字符串 ss。

字符串的一个子串是该字符串的一个连续子序列。

令 f(l,r)f(l, r) 表示子串 s[l..r]s[l..r] 中所有数字之和。

若子串 s[l1..r1]s[l_1..r_1] 满足以下条件,则称其为 xx-质子串(xx-prime substring):

  • f(l1,r1)=xf(l_1, r_1) = x;
  • 不存在任何 l2,r2l_2, r_2,使得
    • l1≤l2≤r2≤r1l_1 \le l_2 \le r_2 \le r_1;
    • f(l2,r2)≠xf(l_2, r_2) \neq x;
    • f(l2,r2)f(l_2, r_2) 整除 xx。

你可以从字符串中删除若干字符。删除一个字符后,字符串剩余的左右两部分将按原顺序直接拼接。

问:最少需要删除多少个字符,才能使字符串中不再包含任何 xx-质子串?若给定字符串 ss 中原本就不存在 xx-质子串,则输出 00。

输入格式

The first line contains a string ss (1≤∣s∣≤10001 \le |s| \le 1000). ss contains only digits from 11 to 99 inclusive.

The second line contains an integer xx (1≤x≤201 \le x \le 20).

第一行包含一个字符串 ss(1≤∣s∣≤10001 \le |s| \le 1000)。ss 仅由数字 11 到 99(含)组成。

第二行包含一个整数 xx(1≤x≤201 \le x \le 20)。

输出格式

Print a single integer — the minimum number of characters you should erase from the string so that there are no xx-prime substrings in it. If there are no xx-prime substrings in the given string ss, then print 00.

输出一个整数——为使字符串中不存在 xx-素子串,你需要删除的最少字符数。如果给定字符串 ss 中本来就没有 xx-素子串,则输出 00。

输入输出样例

  • 输入#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 88-prime substrings "8" and "53". You can erase these characters to get rid of both: "116285317". The resulting string "1162317" contains no 88-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 1313-prime substrings. There are no substrings with the sum of digits equal to 1313 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 55 of each of them, so it doesn't matter which.

在第一个例子中,有两个 88-质数子串:“8”和“53”。你可以删除这些字符来同时消除二者:“116285317”。得到的字符串“1162317”不包含任何 88-质数子串。删除这些字符也是有效的答案:“116285317”。

在第二个例子中,你只需删除两个“1”。

在第三个例子中,不存在 1313-质数子串。实际上,整个字符串中没有任何子串的各位数字之和等于 1313。

在第四个例子中,字符串中既不能出现“34”,也不能出现“43”。因此,你必须删除所有的“3”或所有的“4”。其中各有 55 个,所以删哪一种都可以。

输入解题思路,AI测评打分。不知道怎么写?

首页