A. Digits Sequence Dividing

Description:

You are given a sequence s s s consisting of n n n digits from 1 1 1 to 9 9 9.
You have to divide it into at least two segments (segment — is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exactly one segment and if the resulting division will be represented as an integer numbers sequence then each next element of this sequence will be strictly greater than the previous one.
More formally: if the resulting division of the sequence is t 1 , t 2 , , t k t_1, t_2, \dots, t_k t1,t2,,tk, where k k k is the number of element in a division, then for each i i i from 1 1 1 to k 1 k-1 k1 the condition t i &lt; t i + 1 t_{i} &lt; t_{i + 1} ti<ti+1 (using numerical comparing, it means that the integer representations of strings are compared) should be satisfied.
For example, if s = 654 s=654 s=654 then you can divide it into parts [ 6 , 54 ] [6, 54] [6,54] and it will be suitable division. But if you will divide it into parts [ 65 , 4 ] [65, 4] [65,4] then it will be bad division because 65 &gt; 4 65 &gt; 4 65>4. If s = 123 s=123 s=123 then you can divide it into parts [ 1 , 23 ] [1, 23] [1,23], [ 1 , 2 , 3 ] [1, 2, 3] [1,2,3] but not into parts [ 12 , 3 ] [12, 3] [12,3].
Your task is to find any suitable division for each of the q q q independent queries.

Input:

The first line of the input contains one integer q q q ( 1 q 300 1 \le q \le 300 1q300) — the number of queries.
The first line of the i i i-th query contains one integer number n i n_i ni ( 2 n i 300 2 \le n_i \le 300 2ni300) — the number of digits in the i i i-th query.
The second line of the i i i-th query contains one string s i s_i si of length n i n_i ni consisting only of digits from 1 1 1 to 9 9 9.

Output

If the sequence of digits in the i i i-th query cannot be divided into at least two parts in a way described in the problem statement, print the single line “NO” for this query.
Otherwise in the first line of the answer to this query print “YES”, on the second line print k i k_i ki — the number of parts in your division of the i i i-th query sequence and in the third line print k i k_i ki strings t i , 1 , t i , 2 , , t i , k i t_{i, 1}, t_{i, 2}, \dots, t_{i, k_i} ti,1,ti,2,,ti,ki — your division. Parts should be printed in order of the initial string digits. It means that if you write the parts one after another without changing their order then you’ll get the string s i s_i si.
See examples for better understanding.

Sample Input:

4
6
654321
4
1337
2
33
4
2122

Sample Output:

YES
3
6 54 321
YES
3
1 3 37
NO
YES
2
21 22

题目链接

判断一串数字字符串是否能够被划分为至少两段的严格递增数列

若字符串长度为 2 2 2 判断第一位是否小于第二位即可

否则一定可以构造为符合要求的两个数(按照位数划分第一个数一定可以比第二个数位数少)

AC代码:

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

int main(int argc, char *argv[]) {
    int Q; cin >> Q;
    for (int Query = 1; Query <= Q; ++Query) {
        int N; string Str; cin >> N >> Str;
        if (N == 2 && Str[0] >= Str[1]) {
            cout << "NO" << endl;
            continue;
        }
        cout << "YES" << endl << "2" << endl;
        cout << Str[0] << " " << Str.substr(1, N - 1) << endl;
    }
    return 0;
}

B. Digital root

Description:

Today at the lesson of mathematics, Petya learns about the digital root.
The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
Let’s denote the digital root of x x x as S ( x ) S(x) S(x). Then S ( 5 ) = 5 S(5)=5 S(5)=5, S ( 38 ) = S ( 3 + 8 = 11 ) = S ( 1 + 1 = 2 ) = 2 S(38)=S(3+8=11)=S(1+1=2)=2 S(38)=S(3+8=11)=S(1+1=2)=2, S ( 10 ) = S ( 1 + 0 = 1 ) = 1 S(10)=S(1+0=1)=1 S(10)=S(1+0=1)=1.
As a homework Petya got n n n tasks of the form: find k k k-th positive number whose digital root is x x x.
Petya has already solved all the problems, but he doesn’t know if it’s right. Your task is to solve all n n n tasks from Petya’s homework.

Input:

The first line contains a single integer n n n ( 1 n 1 0 3 1 \le n \le 10^3 1n103) — the number of tasks in Petya’s homework. The next n n n lines contain two integers k i k_i ki ( 1 k i 1 0 12 1 \le k_i \le 10^{12} 1ki1012) and x i x_i xi ( 1 x i 9 1 \le x_i \le 9 1xi9) — i i i-th Petya’s task in which you need to find a k i k_i ki-th positive number, the digital root of which is x i x_i xi.

Output

Output n n n lines, i i i-th line should contain a single integer — the answer to the i i i-th problem.

Sample Input:

3
1 5
5 2
3 1

Sample Output:

5
38
19

题目链接

一个数的数根为其所有位数之和直到此数为个位数,求第 k k k 个数根为 x x x 的数

观察可得一个数 x x x 的数根即为 x m o d &ThinSpace;&ThinSpace; 9 x\mod9 xmod9 ,所以第 k k k 个数根为 x x x 的数即为 ( k 1 ) × 9 + x (k-1)\times9+x (k1)×9+x

AC代码:

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

int main(int argc, char *argv[]) {
    ll n; cin >> n;
    for (ll i = 0, k, x; i < n; ++i) {
        cin >> k >> x;
        cout << (k - 1) * 9 + x << endl;
    }
    return 0;
}

C. Brutality

Description:

You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent’s character.
You are playing the game on the new generation console so your gamepad have 26 26 26 buttons. Each button has a single lowercase Latin letter from ‘a’ to ‘z’ written on it. All the letters on buttons are pairwise distinct.
You are given a sequence of hits, the i i i-th hit deals a i a_i ai units of damage to the opponent’s character. To perform the i i i-th hit you have to press the button s i s_i si on your gamepad. Hits are numbered from 1 1 1 to n n n.
You know that if you press some button more than k k k times in a row then it’ll break. You cherish your gamepad and don’t want to break any of its buttons.
To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of a i a_i ai over all i i i for the hits which weren’t skipped.
Note that if you skip the hit then the counter of consecutive presses the button won’t reset.
Your task is to skip some hits to deal the maximum possible total damage to the opponent’s character and not break your gamepad buttons.

Input:

The first line of the input contains two integers n n n and k k k ( 1 k n 2 1 0 5 1 \le k \le n \le 2 \cdot 10^5 1kn2105) — the number of hits and the maximum number of times you can push the same button in a row.
The second line of the input contains n n n integers a 1 , a 2 , , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 a i 1 0 9 1 \le a_i \le 10^9 1ai109), where a i a_i ai is the damage of the i i i-th hit.
The third line of the input contains the string s s s consisting of exactly n n n lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).

Output

Print one integer d m g dmg dmg — the maximum possible damage to the opponent’s character you can deal without breaking your gamepad buttons.

Sample Input:

7 3
1 5 16 18 7 2 10
baaaaca

Sample Output:

54

Sample Input:

5 5
2 4 1 3 1000
aaaaa

Sample Output:

1010

Sample Input:

5 4
2 4 1 3 1000
aaaaa

Sample Output:

1009

Sample Input:

8 1
10 15 2 1 4 8 15 16
qqwweerr

Sample Output:

41

Sample Input:

6 3
14 18 9 19 2 15
cccccc

Sample Output:

52

Sample Input:

2 1
10 10
qq

Sample Output:

10

题目链接

一串由小写字母组成的字符串每个字符均有一个权值,按照下标升序依次拿取字符,相同字符最多可以连续拿 k k k 次,求可拿的最大权值之和

先处理字符相同的连续区间,之后在区间内拿权值最大的 k k k 个即可

AC代码:

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

int main(int argc, char *argv[]) {
    int n, k; cin >> n >> k;
    vector<pair<int, char> > a(n);
    for (int i = 0; i < n; ++i) cin >> a[i].first;
    for (int i = 0; i < n; ++i) cin >> a[i].second;
    vector<pair<int, int> > seg;
    for (int l = 0, r = 0; l < n; l = r) {
        while (r < n && a[r].second == a[l].second) r++;
        seg.push_back(make_pair(l, r));
    }
    ll ans = 0;
    for (auto it : seg) {
        sort(a.begin() + it.first, a.begin() + it.second);
        reverse(a.begin() + it.first, a.begin() + it.second);
        for (int i = it.first; i < min(it.second, it.first + k); ++i) ans += a[i].first;
    }
    cout << ans << endl;
    return 0;
}

D. Compression

Description:

You are given a binary matrix A A​ A of size n × n n \times n​ n×n. Let’s denote an x x​ x-compression of the given matrix as a matrix B B​ B of size n x × n x \frac{n}{x} \times \frac{n}{x}​ xn×xn such that for every i [ 1 , n ] , j [ 1 , n ] i \in [1, n], j \in [1, n]​ i[1,n],j[1,n] the condition A [ i ] [ j ] = B [ i x ] [ j x ] A[i][j] = B[\lceil \frac{i}{x} \rceil][\lceil \frac{j}{x} \rceil]​ A[i][j]=B[xi][xj] is met.
Obviously, x x​ x-compression is possible only if x x​ x divides n n​ n, but this condition is not enough. For example, the following matrix of size 2 × 2 2 \times 2​ 2×2 does not have any 2 2​ 2-compression:

01 10 01\\ 10 0110

For the given matrix A A A, find maximum x x x such that an x x x-compression of this matrix is possible.
Note that the input is given in compressed form. But even though it is compressed, you’d better use fast input.

Input:

The first line contains one number n n n ( 4 n 5200 4 \le n \le 5200 4n5200) — the number of rows and columns in the matrix A A A. It is guaranteed that n n n is divisible by 4 4 4.
Then the representation of matrix follows. Each of n n n next lines contains n 4 \frac{n}{4} 4n one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 0 0 to 9 9 9 or as uppercase Latin letters from A A A to F F F). Binary representation of each of these numbers denotes next 4 4 4 elements of the matrix in the corresponding row. For example, if the number B B B is given, then the corresponding elements are 1011, and if the number is 5 5 5, then the corresponding elements are 0101.
Elements are not separated by whitespaces.

Output

Print one number: maximum x x x such that an x x x-compression of the given matrix is possible.

Sample Input:

8
E7
E7
E7
00
00
E7
E7
E7

Sample Output:

1

Sample Input:

4
7
F
F
F

Sample Output:

1

题目链接

一个由 0 1 0、1 01 组成的 n × n n\times n n×n 矩阵 A A A 每行按 16 16 16 进制给出(矩阵每一位即为 16 16 16 进制数在 2 2 2 进制下的每一位),求最大的 x ( x n ) x(x|n) x(xn) 使得可以构造出一个 n x × n x \lfloor\frac{n}{x}\rfloor\times\lfloor\frac{n}{x}\rfloor xn×xn 的矩阵使得对于任意 i [ 1 , n ] , j [ 1 , n ] i\in[1,n],j\in[1,n] i[1,n],j[1,n] 满足 A [ i ] [ j ] = B [ i x ] [ j x ] A[i][j]=B[\lfloor\frac{i}{x}\rfloor][\lfloor\frac{j}{x}\rfloor] A[i][j]=B[xi][xj]

其实题意就是压缩矩阵使得矩阵分块的每一部分都相等然后压缩为一个数,求压缩后矩阵大小的最大值

枚举压缩后的矩阵大小(因为是求最大值所以可以由 n n n 开始从大往小枚举),因为在对于不同的 i j i、j ij 会有相同的 i x j x \lfloor\frac{i}{x}\rfloor、\lfloor\frac{j}{x}\rfloor​ xixj 所以相同的值就自然被归并到一块内(整除分块),所以必须要求所有块内所有元素相等才可成立,这里可以预处理原矩阵每行、列是否与上一行、列相同进行判断,同一行内若有块内列不与上一列(两列位处同一块内)完全相同则表明块内元素不完全相同即不可压缩,同理同一列内若有块内行不与上一行(两行位处同一块内)完全相同则不可压缩

AC代码:

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

int main(int argc, char *argv[]) {
    int n; cin >> n;
    vector<vector<bool>> matrix(n, vector<bool>(n, false));
    for (int i = 0; i < n; ++i) {
        string hex; cin >> hex;
        for (int j = 0, x; j < hex.length(); ++j) {
            if (isdigit(hex[j])) x = hex[j] - '0';
            else x = hex[j] - 'A' + 10;
            for (int k = 3; k >= 0; --k) {
                int site = 4 - k - 1;
                matrix[i][j * 4 + k] = x & 1;
                x >>= 1;
            }
        }
    }
    vector<bool> r(n, false);
    for (int i = 1; i < n; ++i) {
        r[i] = true;
        for (int j = 0; j < n; ++j) {
            if (matrix[i][j] != matrix[i - 1][j]) r[i] = false;
        }
    }
    vector<bool> c(n, false);
    for (int j = 1; j < n; ++j) {
        c[j] = true;
        for (int i = 0; i < n; ++i) {
            if (matrix[i][j] != matrix[i][j - 1]) c[j] = false;
        }
    }
    for (int x = n; x > 0; --x) {
        if (n % x) continue;
        int flag = true;
        for (int i = 0; i < n; ++i) {
            if ((i % x) && (!c[i] || !r[i])) flag = false;
        }
        if (flag) {
            cout << x << endl;
            break;
        }
    }
    return 0;
}