Codeforces Round 921 (Div. 2) A B C

A. We Got Everything Covered!

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given two positive integers

n

n

n and

k

k

k.

Your task is to find a string

s

s

s such that all possible strings of length

n

n

n that can be formed using the first

k

k

k lowercase English alphabets occur as a subsequence of

s

s

s.

If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them.

Note: A string

a

a

a is called a subsequence of another string

b

b

b if

a

a

a can be obtained by deleting some (possibly zero) characters from

b

b

b without changing the order of the remaining characters.

Input

The first line of input contains a single integer

t

t

t (

1

t

676

1leq tleq 676

1t676) denoting the number of test cases.

Each test case consists of a single line of input containing two integers

n

n

n (

1

n

26

1leq nleq 26

1n26) and

k

k

k (

1

k

26

1leq kleq 26

1k26).

Output

For each test case, print a single line containing a single string

s

s

s which satisfies the above property. If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them.

Example

input

4
1 2
2 1
2 2
2 3

output

ab
aa
baab
abcbac

Note

For the first test case, there are two strings of length

1

1

1 which can be formed using the first

2

2

2 lowercase English alphabets, and they are present in

s

s

s as a subsequence as follows:

  • a

    :

    a

    b

    texttt{a}: {color{red}{texttt{a}}}texttt{b}

    a:ab

  • b

    :

    a

    b

    texttt{b}: texttt{a}{color{red}{texttt{b}}}

    b:ab

For the second test case, there is only one string of length

2

2

2 which can be formed using the first lowercase English alphabet, and it is present in

s

s

s as a subsequence as follows:

  • aa

    :

    aa

    texttt{aa}: {color{red}{texttt{aa}}}

    aa:aa

For the third test case, there are

4

4

4 strings of length

2

2

2 which can be formed using the first

2

2

2 lowercase English alphabets, and they are present in

s

s

s as a subsequence as follows:

  • aa

    :

    b

    aa

    b

    texttt{aa}: texttt{b}{color{red}{texttt{aa}}}texttt{b}

    aa:baab

  • ab

    :

    ba

    ab

    texttt{ab}: texttt{ba}{color{red}{texttt{ab}}}

    ab:baab

  • ba

    :

    ba

    ab

    texttt{ba}: {color{red}{texttt{ba}}}texttt{ab}

    ba:baab

  • bb

    :

    b

    aa

    b

    texttt{bb}: {color{red}{texttt{b}}}texttt{aa}{color{red}{texttt{b}}}

    bb:baab

For the fourth test case, there are

9

9

9 strings of length

2

2

2 which can be formed using the first

3

3

3 lowercase English alphabets, and they are present in

s

s

s as a subsequence as follows:

  • aa

    :

    a

    bcb

    a

    c

    texttt{aa}: {color{red}{texttt{a}}}texttt{bcb}{color{red}{texttt{a}}}texttt{c}

    aa:abcbac

  • ab

    :

    ab

    cbac

    texttt{ab}: {color{red}{texttt{ab}}}texttt{cbac}

    ab:abcbac

  • ac

    :

    abcb

    ac

    texttt{ac}: texttt{abcb}{color{red}{texttt{ac}}}

    ac:abcbac

  • ba

    :

    abc

    ba

    c

    texttt{ba}: texttt{abc}{color{red}{texttt{ba}}}texttt{c}

    ba:abcbac

  • bb

    :

    a

    b

    c

    b

    ac

    texttt{bb}: texttt{a}{color{red}{texttt{b}}}texttt{c}{color{red}{texttt{b}}}texttt{ac}

    bb:abcbac

  • bc

    :

    a

    bc

    bac

    texttt{bc}: texttt{a}{color{red}{texttt{bc}}}texttt{bac}

    bc:abcbac

  • ca

    :

    ab

    c

    b

    a

    c

    texttt{ca}: texttt{ab}{color{red}{texttt{c}}}texttt{b}{color{red}{texttt{a}}}texttt{c}

    ca:abcbac

  • cb

    :

    ab

    cb

    ac

    texttt{cb}: texttt{ab}{color{red}{texttt{cb}}}texttt{ac}

    cb:abcbac

  • cc

    :

    ab

    c

    ba

    c

    texttt{cc}: texttt{ab}{color{red}{texttt{c}}}texttt{ba}{color{red}{texttt{c}}}

    cc:abcbac

Tutorial

要将字符串

a

a

a

a

aa dots aa

aaaa作为子序列,您需要在字符串中至少有

n

n

n 个字符作为

a

a

a。,对于所有

k

k

k 不同的字符也是如此。 因此,总长度至少为

n

×

k

n times k

n×k,所以将前

k

k

k 个小写英文字母组成的字符串连续输出

n

n

n 次即可满足题意,子序列的每个字母分别来自每一组由前

k

k

k 个小写英文字母组成的字符串

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl 'n'
#define int long long

void solve() {
    int n, k;
    cin >> n >> k;
    string s = "";
    for (char c = 'a'; c < 'a' + k; ++c) {
        s += c;
    }
    for (int i = 0; i < n; ++i) {
        cout << s;
    }
    cout << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}


B. A Balanced Problemset?

time limit per test: 1.5 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Jay managed to create a problem of difficulty

x

x

x and decided to make it the second problem for Codeforces Round #921.

But Yash fears that this problem will make the contest highly unbalanced, and the coordinator will reject it. So, he decided to break it up into a problemset of

n

n

n sub-problems such that the difficulties of all the sub-problems are a positive integer and their sum is equal to

x

x

x.

The coordinator, Aleksey, defines the balance of a problemset as the GCD of the difficulties of all sub-problems in the problemset.

Find the maximum balance that Yash can achieve if he chooses the difficulties of the sub-problems optimally.

Input

The first line of input contains a single integer

t

t

t (

1

t

1

0

3

1leq tleq 10^3

1t103) denoting the number of test cases.

Each test case contains a single line of input containing two integers

x

x

x (

1

x

1

0

8

1leq xleq 10^8

1x108) and

n

n

n (

1

n

x

1leq nleq x

1nx).

Output

For each test case, print a single line containing a single integer denoting the maximum balance of the problemset Yash can achieve.

Example

input

3
10 3
5 5
420 69

output

2
1
6

Note

For the first test case, one possible way is to break up the problem of difficulty

10

10

10 into a problemset having three problems of difficulties

4

4

4,

2

2

2 and

4

4

4 respectively, giving a balance equal to

2

2

2.

For the second test case, there is only one way to break up the problem of difficulty

5

5

5 into a problemset of

5

5

5 problems with each problem having a difficulty

1

1

1 giving a balance equal to

1

1

1.

Tutorial

根据

G

C

D

GCD

GCD 的性质,最终答案将始终是

x

x

x 的除数,如果

n

×

d

x

n times d leqslant x

n×dx,则可以选择子问题难度为

d

,

d

,

,

x

(

n

1

)

d

d,d,dots,x-(n-1)d

d,d,,x(n1)d,此时每个子问题的难度都是

d

d

d 的倍数

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl 'n'
#define int long long

void solve() {
    int x, n;
    cin >> x >> n;
    int ans = 1;
    for (int i = 1; i * i <= x; ++i) {
        if (x % i) {
            continue;
        }
        if (x / i >= n) {
            ans = max(ans, i);
        }
        if (i >= n) {
            ans = max(ans, x / i);
        }
    }
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}

C. Did We Get Everything Covered?

time limit per test: 2 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given two integers

n

n

n and

k

k

k along with a string

s

s

s.

Your task is to check whether all possible strings of length

n

n

n that can be formed using the first

k

k

k lowercase English alphabets occur as a subsequence of

s

s

s. If the answer is NO, you also need to print a string of length

n

n

n that can be formed using the first

k

k

k lowercase English alphabets which does not occur as a subsequence of

s

s

s.

If there are multiple answers, you may print any of them.

Note: A string

a

a

a is called a subsequence of another string

b

b

b if

a

a

a can be obtained by deleting some (possibly zero) characters from

b

b

b without changing the order of the remaining characters.

Input

The first line of input contains a single integer

t

(

1

t

1

0

5

)

t , (1 le t le 10^5)

t(1t105), the number of test cases.

The first line of each test case contains

3

3

3 integers

n

(

1

n

26

)

,

k

(

1

k

26

)

,

m

(

1

m

1000

)

n , (1 le n le 26), : k , (1 le k le 26), : m , (1 le m le 1000)

n(1n26),k(1k26),m(1m1000), where

n

n

n and

k

k

k are the same as described in the input and

m

m

m is the length of the string

s

s

s.

The second line of each test case contains a single string

s

s

s of length

m

m

m, comprising only of the first

k

k

k lowercase English alphabets.

It is guaranteed that the sum of

m

m

m and the sum of

n

n

n over all test cases does not exceed

1

0

6

10^6

106.

Output

For each test case, print YES if all possible strings of length

n

n

n that can be formed using the first

k

k

k lowercase English alphabets occur as a subsequence of

s

s

s, else print NO.

If your answer is NO, print a string of length

n

n

n that can be formed using the first

k

k

k lowercase English alphabets which does not occur as a subsequence of

s

s

s in the next line.

You may print each letter of YES or NO in any case (for example, YES, yES, YeS will all be recognized as a positive answer).

Example

input

3
2 2 4
abba
2 2 3
abb
3 3 10
aabbccabab

output

YES
NO
aa
NO
ccc

Note

For the first test case, all possible strings (aa, ab, ba, bb) of length

2

2

2 that can be formed using the first

2

2

2 English alphabets occur as a subsequence of abba.

For the second test case, the string aa is not a subsequence of abb.

Tutorial

本题为

A

A

A 题的一个检查器,需要反向判断给出的字符串

s

s

s 是否符合标准,我们可以对前

k

k

k 中字符进行一个计数,如果都出现一次后,那么其能表示的

n

n

n 就多一位,遍历结束后,如果不能满足题意,错误样例可以通过最后计数器不存在的字符构造出

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl 'n'
#define int long long

void solve() {
    int n, k, m;
    string s, S;
    cin >> n >> k >> m >> s;
    int mid = 0;
    for (int i = 0; i < m; ++i) {
        mid |= (1 << (s[i] - 'a'));
        if (mid == (1 << k) - 1) { // 一套字母集齐了
            mid = 0;
            S += s[i];
        }
    }
    if ((int)S.size() >= n) { // 每个字母都至少有 n 个
        cout << "YES" << endl;
        return;
    }
    cout << "NO" << endl;
    for (int i = 0; i < k; ++i) {
        if (mid & (1 << i)) {
            continue;
        }
        // 缺啥补啥
        while ((int)S.size() < n) {
            S += (char)('a' + i);
        }
    }
    cout << S << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>