Codeforces Round 928 (Div. 4) A B C D E F G

A. Vlad and the Best of Five

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vladislav has a string of length 5 5 5, whose characters are each either A \texttt{A} A or B \texttt{B} B.

Which letter appears most frequently: A \texttt{A} A or B \texttt{B} B?

Input

The first line of the input contains an integer t t t ( 1 ≤ t ≤ 32 1 \leq t \leq 32 1t32) — the number of test cases.

The only line of each test case contains a string of length 5 5 5 consisting of letters A \texttt{A} A and B \texttt{B} B.

All t t t strings in a test are different (distinct).

Output

For each test case, output one letter ( A \texttt{A} A or B \texttt{B} B) denoting the character that appears most frequently in the string.

Example

input

8
ABABB
ABABA
BBBAB
AAAAA
BBBBB
BABAA
AAAAB
BAAAA

output

B
A
B
A
B
A
A
A

Tutorial

哪个多输出哪个

Solution

for _ in range(int(input())):
    mp = Counter(input().strip())
    print("A" if mp['A'] > mp['B'] else "B")

B. Vlad and Shapes

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vladislav has a binary square grid of n × n n \times n n×n cells. A triangle or a square is drawn on the grid with symbols 1 \texttt{1} 1. As he is too busy being cool, he asks you to tell him which shape is drawn on the grid.

  • A triangle is a shape consisting of k k k ( k > 1 k>1 k>1) consecutive rows, where the i i i-th row has 2 ⋅ i − 1 2 \cdot i-1 2i1 consecutive characters 1 \texttt{1} 1, and the central 1s are located in one column. An upside down triangle is also considered a valid triangle (but not rotated by 90 degrees).

Two left pictures contain examples of triangles: k = 4 k=4 k=4, k = 3 k=3 k=3. The two right pictures don’t contain triangles.

  • A square is a shape consisting of k k k ( k > 1 k>1 k>1) consecutive rows, where the i i i-th row has k k k consecutive characters 1 \texttt{1} 1, which are positioned at an equal distance from the left edge of the grid.

Examples of two squares: k = 2 k=2 k=2, k = 4 k=4 k=4.

For the given grid, determine the type of shape that is drawn on it.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 10 2 \leq n \leq 10 2n10) — the size of the grid.

The next n n n lines each contain n n n characters 0 \texttt{0} 0 or 1 \texttt{1} 1.

The grid contains exactly one triangle or exactly one square that contains all the 1 \texttt{1} 1s in the grid. It is guaranteed that the size of the triangle or square is greater than 1 1 1 (i.e., the shape cannot consist of exactly one 1).

Output

For each test case, output “SQUARE” if all the 1 \texttt{1} 1s in the grid form a square, and “TRIANGLE” otherwise (without quotes).

Example

input

6
3
000
011
011
4
0000
0000
0100
1110
2
11
11
5
00111
00010
00000
00000
00000
10
0000000000
0000000000
0000000000
0000000000
0000000000
1111111110
0111111100
0011111000
0001110000
0000100000
3
111
111
111

output

SQUARE
TRIANGLE
SQUARE
TRIANGLE
TRIANGLE
SQUARE

Tutorial

暴力模拟判断即可

Solution

def solve():
    n = int(input())
    g = [input().strip() for __ in range(n)]
    for i in range(n):
        for j in range(n):
            if g[i][j] == '1':
                x = y = 0
                while i + x < n and g[i + x][j] == '1':
                    x += 1
                while j + y < n and g[i][j + y] == '1':
                    y += 1
                if x ^ y:
                    print("TRIANGLE")
                    return
                for a in range(x):
                    for b in range(y):
                        if g[i + a][j + b] == '0':
                            print("TRIANGLE")
                            return
                print("SQUARE")
                return

for _ in range(int(input())):
    solve()

C. Vlad and a Sum of Sum of Digits

time limit per test: 0.5 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Please note that the time limit for this problem is only 0.5 seconds per test.

Vladislav wrote the integers from 1 1 1 to n n n, inclusive, on the board. Then he replaced each integer with the sum of its digits.

What is the sum of the numbers on the board now?

For example, if n = 12 n=12 n=12 then initially the numbers on the board are:

1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. 1,2,3,4,5,6,7,8,9,10,11,12.
Then after the replacement, the numbers become:
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3. 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3. 1,2,3,4,5,6,7,8,9,1,2,3.
The sum of these numbers is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 2 + 3 = 51 1+2+3+4+5+6+7+8+9+1+2+3=51 1+2+3+4+5+6+7+8+9+1+2+3=51. Thus, for n = 12 n=12 n=12 the answer is 51 51 51.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

The only line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) — the largest number Vladislav writes.

Output

For each test case, output a single integer — the sum of the numbers at the end of the process.

Example

input

7
12
1
2
3
1434
2024
200000

output

51
1
3
6
18465
28170
4600002

Tutorial

先暴力求出所有答案,然后用 O ( 1 ) O(1) O(1) 时间复杂度得出答案

Solution

ans = [0 for _ in range(200001)]
for i in range(1, 200001):
    x = i
    while x:
        ans[i] += x % 10
        x //= 10
    ans[i] += ans[i - 1]

for _ in range(int(input())):
    print(ans[int(input())])

D. Vlad and Division

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vladislav has n n n non-negative integers, and he wants to divide all of them into several groups so that in any group, any pair of numbers does not have matching bit values among bits from 1 1 1-st to 31 31 31-st bit (i.e., considering the 31 31 31 least significant bits of the binary representation).

For an integer k k k, let k 2 ( i ) k_2(i) k2(i) denote the i i i-th bit in its binary representation (from right to left, indexing from 1). For example, if k = 43 k=43 k=43, since 43 = 10101 1 2 43=101011_2 43=1010112, then 4 3 2 ( 1 ) = 1 43_2(1)=1 432(1)=1, 4 3 2 ( 2 ) = 1 43_2(2)=1 432(2)=1, 4 3 2 ( 3 ) = 0 43_2(3)=0 432(3)=0, 4 3 2 ( 4 ) = 1 43_2(4)=1 432(4)=1, 4 3 2 ( 5 ) = 0 43_2(5)=0 432(5)=0, 4 3 2 ( 6 ) = 1 43_2(6)=1 432(6)=1, 4 3 2 ( 7 ) = 0 43_2(7)=0 432(7)=0, 4 3 2 ( 8 ) = 0 , … , 4 3 2 ( 31 ) = 0 43_2(8)=0, \dots, 43_2(31)=0 432(8)=0,,432(31)=0.

Formally, for any two numbers x x x and y y y in the same group, the condition x 2 ( i ) ≠ y 2 ( i ) x_2(i) \neq y_2(i) x2(i)=y2(i) must hold for all 1 ≤ i < 32 1 \leq i < 32 1i<32.

What is the minimum number of groups Vlad needs to achieve his goal? Each number must fall into exactly one group.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) — the total number of integers.

The second line of each test case contains n n n given integers a 1 , … , a n a_1, \ldots, a_n a1,,an ( 0 ≤ a j < 2 31 0 \leq a_j < 2^{31} 0aj<231).

The sum of n n n over all test cases in a test does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the minimum number of groups required to satisfy the condition.

Example

input

9
4
1 4 3 4
2
0 2147483647
5
476319172 261956880 2136179468 1671164475 1885526767
3
1335890506 811593141 1128223362
4
688873446 627404104 1520079543 1458610201
4
61545621 2085938026 1269342732 1430258575
4
0 0 2147483647 2147483647
3
0 0 2147483647
8
1858058912 289424735 1858058912 2024818580 1858058912 289424735 122665067 289424735

output

4
1
3
2
2
3
2
2
4

Note

In the first test case, any two numbers have the same last 31 31 31 bits, so we need to place each number in its own group.

In the second test case, a 1 = 000000000000000000000000000000 0 2 a_1=0000000000000000000000000000000_2 a1=00000000000000000000000000000002, a 2 = 111111111111111111111111111111 1 2 a_2=1111111111111111111111111111111_2 a2=11111111111111111111111111111112 so they can be placed in the same group because a 1 ( i ) ≠ a 2 ( i ) a_1(i) \ne a_2(i) a1(i)=a2(i) for each i i i between 1 1 1 and 31 31 31, inclusive.

Tutorial

我们可以便利所有的数字,检查是否已经出现一个数字可以与当前数字配对,如果我们之前遇到过 ( 2 31 − 1 ) ⨁ a i (2^{31} - 1)\bigoplus a_i (2311)ai 这个值,如果出现过,则可以合为一组,否则开一个新组

Solution

X = (1 << 31) - 1

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    mp = defaultdict(int)
    ans = 0
    for x in a:
        if mp[X - x]:
            mp[X - x] -= 1
        else:
            ans += 1
            mp[x] += 1
    print(ans)

E. Vlad and an Odd Ordering

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vladislav has n n n cards numbered 1 , 2 , … , n 1, 2, \dots, n 1,2,,n. He wants to lay them down in a row as follows:

  • First, he lays down all the odd-numbered cards from smallest to largest.
  • Next, he lays down all cards that are twice an odd number from smallest to largest (i.e. 2 2 2 multiplied by an odd number).
  • Next, he lays down all cards that are 3 3 3 times an odd number from smallest to largest (i.e. 3 3 3 multiplied by an odd number).
  • Next, he lays down all cards that are 4 4 4 times an odd number from smallest to largest (i.e. 4 4 4 multiplied by an odd number).
  • And so on, until all cards are laid down.

What is the k k k-th card he lays down in this process? Once Vladislav puts a card down, he cannot use that card again.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 5 ⋅ 1 0 4 1 \leq t \leq 5 \cdot 10^4 1t5104) — the number of test cases.

The only line of each test case contains two integers n n n and k k k ( 1 ≤ k ≤ n ≤ 1 0 9 1 \leq k \leq n \leq 10^9 1kn109) — the number of cards Vlad has, and the position of the card you need to output.

Output

For each test case, output a single integer — the k k k-th card Vladislav lays down.

Example

input

11
7 1
7 2
7 3
7 4
7 5
7 6
7 7
1 1
34 14
84 19
1000000000 1000000000

output

1
3
5
7
2
6
4
1
27
37
536870912

Note

In the first seven test cases, n = 7 n=7 n=7. Vladislav lays down the cards as follows:

  • First — all the odd-numbered cards in the order 1 1 1, 3 3 3, 5 5 5, 7 7 7.
  • Next — all cards that are twice an odd number in the order 2 2 2, 6 6 6.
  • Next, there are no remaining cards that are 3 3 3 times an odd number. (Vladislav has only one of each card.)
  • Next — all cards that are 4 4 4 times an odd number, and there is only one such card: 4 4 4.
  • There are no more cards left, so Vladislav stops.

Thus the order of cards is 1 1 1, 3 3 3, 5 5 5, 7 7 7, 2 2 2, 6 6 6, 4 4 4.

Tutorial

设倍数为 x x x,奇数是 o d d odd odd

首先如果一个数字是 3 × o d d 3 \times odd 3×odd,那么它肯定也是 1 × o d d 1 \times odd 1×odd,对于 5 × o d d 5\times odd 5×odd 7 × o d d … 7 \times odd \dots 7×odd也是同理,所以我们肯定不会在奇数倍数时放下牌。

对于偶数倍数,模拟几次即可发现所有的牌都可以分为几个部分:

  • 1 × o d d 1 \times odd 1×odd 个数字
  • 2 × o d d 2 \times odd 2×odd 个数字(是 2 2 2 的倍数但不是 4 4 4 的倍数)
  • 4 × o d d 4 \times odd 4×odd 个数字(是 4 4 4 的倍数但不是 8 8 8 的倍数)
  • 8 × o d d 8 \times odd 8×odd 个数字(是 8 8 8 的倍数但不是 16 16 16 的倍数)
  • … \dots

由此可见第一部分有 ⌈ n 2 ⌉ \lceil {n \over 2} \rceil 2n 张牌,此时还剩下 m = ⌊ n 2 ⌋ m=\lfloor {n \over 2} \rfloor m=2n 张牌,第二部分有 ⌈ m 2 ⌉ \lceil {m \over 2} \rceil 2m,以此类推…

所以如果此时 k k k 大于这一部分要出牌的数量,则将这次的牌全部出完,否则说明第 k k k 张牌将在这一部分产生,根据规律可知,当前答案为 k × x − x 2 k \times x - {x \over 2} k×x2x

Solution

for _ in range(int(input())):
    n, k = map(int, input().split())
    i, ans = 2, 0
    while i <= n * 2:
        if k <= (n + i // 2) // i:
            ans = k * i - i // 2
            break
        k -= (n + i // 2) // i
        i <<= 1
    print(ans)

F. Vlad and Avoiding X

time limit per test: 4 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vladislav has a grid of size 7 × 7 7 \times 7 7×7, where each cell is colored black or white. In one operation, he can choose any cell and change its color (black ↔ \leftrightarrow white).

Find the minimum number of operations required to ensure that there are no black cells with four diagonal neighbors also being black.

The left image shows that initially there are two black cells violating the condition. By flipping one cell, the grid will work.

Input

The first line of input contains a single integer t t t ( 1 ≤ t ≤ 200 1 \leq t \leq 200 1t200) — the number of test cases. Then follows the description of the test cases.

Each test case consists of 7 7 7 lines, each containing 7 7 7 characters. Each of these characters is either W \texttt{W} W or B \texttt{B} B, denoting a white or black cell, respectively.

Output

For each test case, output a single integer — the minimum number of operations required to ensure that there are no black cells with all four diagonal neighbors also being black.

Example

input

4
WWWWWWW
WWWWBBB
WWWWWBW
WWBBBBB
WWWBWWW
WWBBBWW
WWWWWWW
WWWWWWW
WWWWWWW
WBBBBBW
WBBBBBW
WBBBBBW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WBBBBBW
BBBBBBB
BBBBBBB
WWWWWWW
BBBBBBB
BBBBBBB
BBBBBBB

output

1
2
0
5

Note

The first test case is illustrated in the statement.

The second test case is illustrated below:

In the third test case, the grid already satisfies the condition.

Tutorial

将网格分为红色和蓝色两部分,每个格子只影响自己颜色的格子,对两个颜色的格子分别进行回溯即可

Solution

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

#define endl '\n'
#define int long long

void solve() {
    int ans = 0, cnt, p = 0;
    vector<string> g(7);
    for (string &gi : g) {
        cin >> gi;
    }

    function<void(int, int, int)> dfs = [&](int x, int y, int mid) {
        if (x == 5) {
            cnt = min(cnt, mid);
            return;
        }
        if (y == 5) {
            dfs(x + 1, 0, mid);
            return;
        }
        if (((x + y) & 1) ^ p) {
            dfs(x, y + 1, mid);
            return;
        }
        if (g[x][y] == 'B' and g[x + 2][y] == 'B' and g[x + 1][y + 1] == 'B' and g[x][y + 2] == 'B' and g[x + 2][y + 2] == 'B') {
            g[x][y] = 'W';
            dfs(x, y + 1, mid + 1);
            g[x][y] = 'B';
            g[x + 2][y] = 'W';
            dfs(x, y + 1, mid + 1);
            g[x + 2][y] = 'B';
            g[x + 2][y + 2] = 'W';
            dfs(x, y + 1, mid + 1);
            g[x + 2][y + 2] = 'B';
            g[x][y + 2] = 'W';
            dfs(x, y + 1, mid + 1);
            g[x][y + 2] = 'B';
            g[x + 1][y + 1] = 'W';
            dfs(x, y + 1, mid + 1);
            g[x + 1][y + 1] = 'B';
        } else {
            dfs(x, y + 1, mid);
        }
    };
    
    for (; p < 2; ++p) {
        cnt = 25;
        dfs(0, 0, 0);
        ans += cnt;
    }
    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;
}

G. Vlad and Trouble at MIT

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vladislav has a son who really wanted to go to MIT. The college dormitory at MIT (Moldova Institute of Technology) can be represented as a tree with n n n vertices, each vertex being a room with exactly one student. A tree is a connected undirected graph with n n n vertices and n − 1 n-1 n1 edges.

Tonight, there are three types of students:

  • students who want to party and play music (marked with P \texttt{P} P),
  • students who wish to sleep and enjoy silence (marked with S \texttt{S} S), and
  • students who don’t care (marked with C \texttt{C} C).

Initially, all the edges are thin walls which allow music to pass through, so when a partying student puts music on, it will be heard in every room. However, we can place some thick walls on any edges — thick walls don’t allow music to pass through them.

The university wants to install some thick walls so that every partying student can play music, and no sleepy student can hear it.

Because the university lost a lot of money in a naming rights lawsuit, they ask you to find the minimum number of thick walls they will need to use.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

The first line of each test case contains an integer n n n ( 2 ≤ n ≤ 1 0 5 2 \leq n \leq 10^5 2n105) — the number of vertices in the tree.

The second line of each test case contains n − 1 n-1 n1 integers a 2 , … , a n a_2, \dots , a_n a2,,an ( 1 ≤ a i < i 1 \leq a_i < i 1ai<i) — it means there is an edge between i i i and a i a_i ai in the tree.

The third line of each test case contains a string s s s of length n n n consisting of characters P \texttt{P} P, S \texttt{S} S, and C \texttt{C} C, denoting that student i i i is of type s i s_i si.

The sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case, output a single integer — the minimum number of thick walls needed.

Example

input

3
3
1 1
CSP
4
1 2 2
PCSS
4
1 2 2
PPSS

output

1
1
2

Note

In the first case, we can install one thick wall between rooms 1 1 1 and 2 2 2, as shown below. We cannot install 0 0 0 walls, since then the music from room 3 will reach room 2 where a student wants to sleep, so the answer is 1 1 1. There are other valid solutions.

Tutorial

d p i , j dp_{i,j} dpi,j 表示把点 j j j 作为 i i i 类型,其中类型 0 0 0 为聚会点,类型 1 1 1 为睡觉享受安静的点,如果这个点有参加聚会的学生,那么这个点就不可能作为类型 1 1 1,反之如果这个点有睡觉的学生,那么这个点就不可能作为类型 0 0 0,如果这里是无所谓的学生,那么这个点作为哪一个类型都可以,如果需要在睡觉的点和参加聚会的点切换,则需要在这里建一堵厚墙。

Solution

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

#define endl '\n'
#define int long long
const int INF = 0x3f3f3f3f;void solve() {
    int n;
    string s;
    cin >> n;
    vector<int> g[n + 1];
    for (int i = 2; i <= n; ++i) {
        int j;
        cin >> j;
        g[i].emplace_back(j);
        g[j].emplace_back(i);
    }
    cin >> s;
    s = " " + s;

    vector dp(2, vector<int>(n + 1)); // dp[i][j] 表示点 j 作为根时需要放的最少的隔板数
    function<void(int, int)> dfs = [&](int u, int father) {
        if (s[u] == 'P') { // e 人狂欢
            dp[0][u] = 0;
            dp[1][u] = INF;
        } else if (s[u] == 'S') { // i 人睡大觉
            dp[1][u] = 0;
            dp[0][u] = INF;
        } else {
            dp[0][u] = dp[1][u] = 0;
        }

        for (int v : g[u]) {
            if (v == father) {
                continue;
            }
            dfs(v, u);
            if (s[u] == 'P') { // e 人狂欢
                dp[0][u] += min(dp[0][v], dp[1][v] + 1);
            } else if (s[u] == 'S') { // i 人睡大觉
                dp[1][u] += min(dp[1][v], dp[0][v] + 1);
            } else {
                dp[0][u] += min(dp[0][v], dp[1][v] + 1);
                dp[1][u] += min(dp[1][v], dp[0][v] + 1);
            }
        }
    };

    dfs(1, -1);
    cout << min(dp[0][1], dp[1][1]) << endl;
}

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

  • 49
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值