CF1367E.Necklace Assembly

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The store sells nn beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them.

A necklace is a set of beads connected in a circle.

For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options):

And the following necklaces cannot be assembled from beads sold in the store:

The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store.

We call a necklace kk-beautiful if, when it is turned clockwise by kk beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace.

As you can see, this necklace is, for example, 33-beautiful, 66-beautiful, 99-beautiful, and so on, but it is not 11-beautiful or 22-beautiful.

In particular, a necklace of length 11 is kk-beautiful for any integer kk. A necklace that consists of beads of the same color is also beautiful for any kk.

You are given the integers nn and kk, and also the string ss containing nn lowercase letters of the English alphabet — each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a kk-beautiful necklace you can assemble.

商店出售 nn 颗珠子。每颗珠子的颜色由一个英文字母小写字符("a"–"z")表示。你想购买若干颗珠子,用它们串成一条项链。

项链是一组首尾相连、构成环形的珠子。

例如,若商店出售的珠子为 "a"、"b"、"c"、"a"、"c"、"c",则你可以用它们组成如下项链(这些并非全部可能的组合):

而以下项链则无法用商店所售珠子组装而成:


第一条项链无法组装,是因为它需要三颗 "a" 珠子,而商店中仅有两颗;第二条项链无法组装,是因为它含有一颗商店未售出的 "d" 珠子。

我们称一条项链是 kk-优美的(kk-beautiful),当且仅当将其顺时针旋转 kk 颗珠子后,项链外观保持不变。例如,下图展示了一条项链连续三次旋转的过程:

如图所示,该项链例如是 33-优美、66-优美、99-优美等,但不是 11-优美或 22-优美。

特别地,长度为 11 的项链对任意整数 kk 均为 kk-优美;全部由同色珠子组成的项链也对任意 kk 均为 kk-优美。

现给你整数 nn 和 kk,以及一个长度为 nn 的字符串 ss,其中每个字符均为英文字母小写,代表商店中一颗珠子的颜色。你可以购买任意子集的珠子,并以任意顺序将它们连接成环。请找出你能组装出的最长的 kk-优美项链的长度。

输入格式

The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases in the test. Then tt test cases follow.

The first line of each test case contains two integers nn and kk (1≤n,k≤20001 \le n, k \le 2000).

The second line of each test case contains the string ss containing nn lowercase English letters — the beads in the store.

It is guaranteed that the sum of nn for all test cases does not exceed 20002000.

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n,k≤20001 \le n, k \le 2000)。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,由小写英文字母组成——表示商店中的珠子。

保证所有测试用例的 nn 值之和不超过 20002000。

输出格式

Output tt answers to the test cases. Each answer is a positive integer — the maximum length of the kk-beautiful necklace you can assemble.

输出 tt 个测试用例的答案。每个答案均为一个正整数——你能组装出的 kk-beautiful 项链的最大长度。

输入输出样例

  • 输入#1

    6
    6 3
    abcbac
    3 6
    aaa
    7 1000
    abczgyo
    5 4
    ababa
    20 10
    aaebdbabdbbddaadaadc
    20 5
    ecbedececacbcbccbdec

    输出#1

    6
    3
    5
    4
    15
    10

说明/提示

The first test case is explained in the statement.

In the second test case, a 66-beautiful necklace can be assembled from all the letters.

In the third test case, a 10001000-beautiful necklace can be assembled, for example, from beads "abzyo".

第一个测试用例已在题目描述中说明。

在第二个测试用例中,可以使用所有字母组装出一条 66-美丽的项链。

在第三个测试用例中,例如可以使用珠子 "abzyo" 组装出一条 10001000-美丽的项链。

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

首页