Codeforces Round 927 (Div. 3) A B C D E F

A. Thorns and Coins

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

During your journey through computer universes, you stumbled upon a very interesting world. It is a path with n n n consecutive cells, each of which can either be empty, contain thorns, or a coin. In one move, you can move one or two cells along the path, provided that the destination cell does not contain thorns (and belongs to the path). If you move to the cell with a coin, you pick it up.

Here, green arrows correspond to legal moves, and the red arrow corresponds to an illegal move.

You want to collect as many coins as possible. Find the maximum number of coins you can collect in the discovered world if you start in the leftmost cell of the path.

Input

The first line of input contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases. Then the descriptions of the test cases follow.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 50 1 \le n \le 50 1n50) — the length of the path.

The second line of each test case contains a string of n n n characters, the description of the path. The character ‘.’ denotes an empty cell, ‘@’ denotes a cell with a coin, and ‘*’ denotes a cell with thorns. It is guaranteed that the first cell is empty.

Output

For each test case, output a single integer, the maximum number of coins you can collect.

Example

input

3
10
.@@*@.**@@
5
.@@@@
15
.@@..@***..@@@*

output

3
4
3

Note

The picture for the first example is in the problem statement.

Here is the picture for the second example:

And here is the picture for the third example:

Tutorial

根据题意暴力模拟即可

Solution

for _ in range(int(input())):
    n = int(input())
    s = input().strip()
    i, ans = 0, 0
    while i < n:
        if s[i] == '@':
            ans += 1
        if i + 1 < n and s[i + 1] != '*':
            i += 1
            continue
        elif i + 2 < n and s[i + 2] != '*':
            i += 2
            continue
        else:
            break
    print(ans)

B. Chaya Calendar

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

The Chaya tribe believes that there are n n n signs of the apocalypse. Over time, it has been found out that the i i i-th sign occurs every a i a_i ai years (in years a i a_i ai, 2 ⋅ a i 2 \cdot a_i 2ai, 3 ⋅ a i 3 \cdot a_i 3ai, … \dots ).

According to the legends, for the apocalypse to happen, the signs must occur sequentially. That is, first they wait for the first sign to occur, then strictly after it, the second sign will occur, and so on. That is, if the i i i-th sign occurred in the year x x x, the tribe starts waiting for the occurrence of the ( i + 1 ) (i+1) (i+1)-th sign, starting from the year x + 1 x+1 x+1.

In which year will the n n n-th sign occur and the apocalypse will happen?

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases. Then follow the descriptions of the test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100) — the number of signs.

The second line of each test case contains n n n integers a 1 , a 2 , a 3 , … , a n a_1, a_2, a_3, \dots, a_n a1,a2,a3,,an ( 1 ≤ a i ≤ 1 0 6 1 \le a_i \le 10^6 1ai106) — the periodicities of the signs.

Output

For each test case, output a single integer — the year in which all n n n signs will occur.

Example

input

4
6
3 2 4 5 9 18
5
1 2 3 4 5
5
1 1 1 1 1
6
50 30 711 200 503 1006

output

36
5
5
2012

Note

In the first set of input data of the example:

  • The tribe will wait for the first sign in the 3 3 3-rd year;
  • the tribe will wait for the second sign in the 4 4 4-th year (since year 2 2 2 have already passed);
  • the tribe will wait for the third sign in the 8 8 8-th year (since the second sign has already occurred in the 4 4 4-th year);
  • the tribe will wait for the fourth sign in the 10 10 10-th year (since year 5 5 5 have already passed);
  • the tribe will wait for the fifth sign in the 18 18 18-th year (since year 9 9 9 have already passed);
  • the tribe will wait for the sixth sign in the 36 36 36-th year (since the fifth sign has already occurred in the 18 18 18-th year).

Tutorial

如果当前年份为 y e a r year year 年,则对于 a i a_i ai 需要找到下一个能被 a i a_i ai 整除的年份,即 ( ⌊ y e a r a i ⌋ + 1 ) × a i (\lfloor {year \over a_i}\rfloor + 1) \times a_i (⌊aiyear+1)×ai

Solution

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    for ai in a:
        ans = (ans + ai) // ai * ai
    print(ans)

C. LR-remainders

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

You are given an array a a a of length n n n, a positive integer m m m, and a string of commands of length n n n. Each command is either the character ‘L’ or the character ‘R’.

Process all n n n commands in the order they are written in the string s s s. Processing a command is done as follows:

  • First, output the remainder of the product of all elements of the array a a a when divided by m m m.
  • Then, if the command is ‘L’, remove the leftmost element from the array a a a, if the command is ‘R’, remove the rightmost element from the array a a a.

Note that after each move, the length of the array a a a decreases by 1 1 1, and after processing all commands, it will be empty.

Write a program that will process all commands in the order they are written in the string s s s (from left to right).

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases in the input. Then descriptions of t t t test cases follow.

Each test case of the input is given by three lines.

The first line contains two integers n n n and m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ m ≤ 1 0 4 1 \le n \le 2\cdot10^5, 1 \le m \le 10^4 1n2105,1m104) — the initial length of the array a a a and the value to take the remainder by.

The second line 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 4 1 \le a_i \le 10^4 1ai104) — the elements of the array a a a.

The third line contains a string s s s consisting of n n n characters ‘L’ and ‘R’.

It is guaranteed that the sum of the values of n n n for all test cases in a test does not exceed 2 ⋅ 1 0 5 2\cdot10^5 2105.

Output

For each test case, output n n n integers b 1 , b 2 , … , b n b_1, b_2, \dots, b_n b1,b2,,bn, where b i b_i bi is the remainder when dividing the product of all elements of the current state of the array a a a by m m m at the beginning of the execution of the i i i-th command.

Example

input

4
4 6
3 1 4 2
LRRL
5 1
1 1 1 1 1
LLLLL
6 8
1 2 3 4 5 6
RLLLRR
1 10000
10000
R

output

0 2 4 1 
0 0 0 0 0 
0 0 0 4 4 4 
0 

Note

In the first test case of the example:

  • 3 ⋅ 1 ⋅ 4 ⋅ 2   m o d   6 = 24   m o d   6 = 0 3 \cdot 1 \cdot 4 \cdot 2 \bmod 6 = 24 \bmod 6 = 0 3142mod6=24mod6=0;
  • s 1 = L s_1 = \text{L} s1=L, so we remove the first element and get the array [ 1 , 4 , 2 ] [1, 4, 2] [1,4,2];
  • 1 ⋅ 4 ⋅ 2   m o d   6 = 8   m o d   6 = 2 1 \cdot 4 \cdot 2 \bmod 6 = 8 \bmod 6 = 2 142mod6=8mod6=2;
  • s 2 = R s_2 = \text{R} s2=R, so we remove the last element and get the array [ 1 , 4 ] [1, 4] [1,4];
  • 1 ⋅ 4   m o d   6 = 4   m o d   6 = 4 1 \cdot 4 \bmod 6 = 4 \bmod 6 = 4 14mod6=4mod6=4;
  • s 3 = R s_3 = \text{R} s3=R, so we remove the last element and get the array [ 1 ] [1] [1];
  • 1   m o d   6 = 1 1 \bmod 6 = 1 1mod6=1;
  • s 4 = L s_4 = \text{L} s4=L, so we remove the first element and get an empty array.

Tutorial

可以改为每次向数组中添加一个元素,然后求其乘积对模数的模,所以最初需要找到最后结束的位置,然后对结果进行倒推

Solution

for _ in range(int(input())):
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    s = input().strip()
    left, right = 0, n - 1
    for c in s[:-1]:
        if c == 'L':
            left += 1
        else:
            right -= 1
    mid = []
    ans = a[left] % m # 最后停留的位置
    mid.append(ans)
    for i in range(n - 2, -1, -1): # 反向推
        if s[i] == 'L':
            left -= 1
            ans *= a[left]
        else:
            right += 1
            ans *= a[right]
        ans %= m
        mid.append(ans)
    for x in mid[::-1]:
        print(x, end = " ")
    print()

D. Card Game

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

Two players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters ‘C’, ‘D’, ‘H’, and ‘S’, respectively. And there are 8 ranks, in increasing order: ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’.

Each card is denoted by two letters: its rank and its suit. For example, the 8 of Hearts is denoted as 8H.

At the beginning of the game, one suit is chosen as the trump suit.

In each round, players make moves like this: the first player places one of his cards on the table, and the second player must beat this card with one of their cards. After that, both cards are moved to the discard pile.

A card can beat another card if both cards have the same suit and the first card has a higher rank than the second. For example, 8S can beat 4S. Additionally, a trump card can beat any non-trump card, regardless of the rank of the cards, for example, if the trump suit is clubs (‘C’), then 3C can beat 9D. Note that trump cards can be beaten only by the trump cards of higher rank.

There were n n n rounds played in the game, so the discard pile now contains 2 n 2n 2n cards. You want to reconstruct the rounds played in the game, but the cards in the discard pile are shuffled. Find any possible sequence of n n n rounds that might have been played in the game.

Input

The first line contains integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of test cases. Then t t t test cases follow.

The first line of a test case contains the integer number n n n ( 1 ≤ n ≤ 16 1\le n\le 16 1n16).

The second line of a test case contains one character, the trump suit. It is one of “CDHS”.

The third line of a test case contains the description of 2 n 2n 2n cards. Each card is described by a two-character string, the first character is the rank of the card, which is one of “23456789”, and the second one is the suit of the card, which is one of “CDHS”. All cards are different.

Output

For each test case print the answer to it:

  • Print n n n lines. In each line, print the description of two cards, in the same format as in the input: the first card that was played by the first player, and then the card that was used by the second player to beat it.
  • If there is no solution, print a single line “IMPOSSIBLE”.

If there are multiple solutions, print any of them.

Example

input

8
3
S
3C 9S 4C 6D 3S 7S
2
C
3S 5D 9S 6H
1
H
6C 5D
1
S
7S 3S
1
H
9S 9H
1
S
9S 9H
1
C
9D 8H
2
C
9C 9S 6H 8C

output

3C 4C
6D 9S
3S 7S
IMPOSSIBLE
IMPOSSIBLE
3S 7S
9S 9H
9H 9S
IMPOSSIBLE
6H 9C
9S 8C

Tutorial

大模拟,先用非王牌对同花色相互凑,再用王牌与多余的非王牌相互凑,最后两张王牌相互凑,如果最后能凑成,则答案存在,否则输出 IMPOSSIBLE

Solution

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

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

string s = "CDHS";

void solve() {
    int n;
    char king;
    cin >> n >> king;
    vector<string> cards(n * 2);
    map<char, vector<string>> mp;
    for (string &card : cards) {
        cin >> card;
        mp[card[1]].emplace_back(card);
    }
    for (char c : s) {
        ranges::sort(mp[c]);
    }
    vector<string> ans;
    for (char c : s) {
        if (c != king) {
            while (mp[c].size() > 1) {
                string s1 = mp[c].back();
                mp[c].pop_back();
                string s2 = mp[c].back();
                mp[c].pop_back();
                ans.push_back(s2 + " " + s1);
            }
        }
    }
    while (mp[king].size()) {
        string s1 = mp[king].back(), s2 = "";
        for (char c : s) {
            if (c != king and mp[c].size()) {
                s2 = mp[c].back();
                mp[c].pop_back();
                break;
            }
        }
        if (s2 == "") {
            break;
        }
        mp[king].pop_back();
        ans.push_back(s2 + " " + s1);
    }
    while (mp[king].size() >= 2) {
        string s1 = mp[king].back();
        mp[king].pop_back();
        string s2 = mp[king].back();
        mp[king].pop_back();
        ans.push_back(s2 + " " + s1);
    }
    for (char c : s) {
        if (mp[c].size()) {
            cout << "IMPOSSIBLE" << endl;
            return;
        }
    }
    for (string si : ans) {
        cout << si << endl;
    }
}

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

E. Final Countdown

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

You are in a nuclear laboratory that is about to explode and destroy the Earth. You must save the Earth before the final countdown reaches zero.

The countdown consists of n n n ( 1 ≤ n ≤ 4 ⋅ 1 0 5 1 \le n \le 4 \cdot 10^5 1n4105) mechanical indicators, each showing one decimal digit. You noticed that when the countdown changes its state from x x x to x − 1 x-1 x1, it doesn’t happen in one move. Instead, each change of a single digit takes one second.

So, for example, if the countdown shows 42, then it will change to 41 in one second, because only one digit is changed, but if the countdown shows 2300, then it will change to 2299 in three seconds, because the three last digits are changed.

Find out how much time is left before the countdown reaches zero.

Input

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

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 4 ⋅ 1 0 5 1\le n\le 4\cdot 10^5 1n4105).

The second line contains a string of n n n digits, the current state of the countdown. It is guaranteed that at least one digit is not zero.

The sum of n n n for all tests does not exceed 4 ⋅ 1 0 5 4\cdot 10^5 4105.

Output

For each test case, print a single integer without leading zeroes, the number of seconds left before the countdown reaches zero. Note that this number may be huge.

Example

input

5
2
42
5
12345
2
99
4
0005
27
456480697259671309012631002

output

46
13715
108
5
507200774732968121125145546

Note

In the first example, there are four changes that take 2 seconds: 40 to 39, 30 to 29, 20 to 19, and 10 to 09, other changes take 1 second each. So the total time is 2 ⋅ 4 + 1 ⋅ ( 42 − 4 ) = 46 2\cdot 4 + 1\cdot(42-4) = 46 24+1(424)=46.

Tutorial

从最低位看起,个位数需要改变 n n n 次,十位需要改变 ⌊ n 10 ⌋ \lfloor {n \over 10} \rfloor 10n,则从右往左数第第 i i i 位的变化次数是 ⌊ n 1 0 i − 1 ⌋ \lfloor {n \over 10^{i - 1}} \rfloor 10i1n 次,每位数字都会在它右侧的位置产生贡献,则可以利用前缀和优化,相当于用数组来模拟数字(此处优化为一个变量 c n t cnt cnt

Solution

for _ in range(int(input())):
    n = int(input())
    s = input().strip()
    ans = []
    mid = 0
    cnt = sum(int(c) for c in s) # 每一位的和
    for c in s[::-1]:
        ans.append(str((cnt + mid) % 10))
        mid = (cnt + mid) // 10 # 后一位给的进位
        cnt -= int(c)
    if mid:
        ans.append(str(mid)[::-1])
    ans = ''.join(ans).rstrip('0') # 去掉前缀零
    print(ans[::-1])

F. Feed Cats

time limit per test: 3 second
memory limit per test: 512 megabytes
input: standard input
output: standard output

There is a fun game where you need to feed cats that come and go. The level of the game consists of n n n steps. There are m m m cats; the cat i i i is present in steps from l i l_i li to r i r_i ri, inclusive. In each step, you can feed all the cats that are currently present or do nothing.

If you feed the same cat more than once, it will overeat, and you will immediately lose the game. Your goal is to feed as many cats as possible without causing any cat to overeat.

Find the maximum number of cats you can feed.

Formally, you need to select several integer points from the segment from 1 1 1 to n n n in such a way that among given segments, none covers two or more of the selected points, and as many segments as possible cover one of the selected points.

Input

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

The first line of each test case contains two integers n n n and m m m ( 1 ≤ n ≤ 1 0 6 1 \le n \le 10^6 1n106, 1 ≤ m ≤ 2 ⋅ 1 0 5 1 \le m\le 2\cdot 10^5 1m2105).

The i i i-th of the next m m m lines contains a pair of integers l i l_i li and r i r_i ri ( 1 ≤ l i ≤ r i ≤ n 1 \le l_i \le r_i \le n 1lirin).

The sum of n n n for all tests does not exceed 1 0 6 10^6 106, the sum of m m m for all tests does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

Output

For each test case, print a single integer, the maximum number of cats you can feed.

Example

input

3
15 6
2 10
3 5
2 4
7 7
8 12
11 11
1000 1
1 1000
5 10
1 2
3 4
3 4
3 4
3 4
1 1
1 2
3 3
3 4
3 4

output

5
1
10

Note

In the first example, one of the ways to feed five cats is to feed at steps 4 4 4 and 11 11 11.

  • At step 4 4 4, cats 1 1 1, 2 2 2, and 3 3 3 will be fed.
  • At step 11 11 11, cats 5 5 5 and 6 6 6 will be fed.

Tutorial

本题可以用动态规划的思想来解决,可以用差分来记录每个位置上有多少只小猫,对于 d p i dp_i dpi,其表示为在第 i i i 个点最多可以喂多少只猫,由于每一只猫只能喂一次,则对于点 i i i,只能由点 i i i 涉及到的猫的范围的最左侧转移过来,设 c n t i cnt_i cnti 表示 i i i 这个点上最多可以喂多少只猫, r i r_i ri 表示点 i i i 当前位置所有的猫的最左边界,所以可以得到递推公式 d p i = max ⁡ ( d p i − 1 , c n t i + d p r i − 1 ) dp_i = \max(dp_{i - 1},cnt_i + dp_{r_i - 1}) dpi=max(dpi1,cnti+dpri1),最后答案即为 d p n dp_n dpn

Solution

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

#define endl '\n'
#define int long longvoid solve() {
    int n, m;
    cin >> n >> m;
    vector<int> r(n + 1, n), cnt(n + 2), dp(n + 1);
    for (int i = 0; i < m; ++i) {
        int left, right;
        cin >> left >> right;
        r[right] = min(r[right], left);
        ++cnt[left];
        --cnt[right + 1];
    }
    for (int i = 1; i <= n; ++i) { // 记录每个位置有多少只猫
        cnt[i] += cnt[i - 1];
    }
    for (int i = n - 1; i >= 0; --i) { // 每个点最多管多大的范围
        r[i] = min(r[i], r[i + 1]);
    }
    // dp[i] 表示在第 i 个点最多可以喂多少只猫
    for (int i = 1; i <= n; ++i) {
        dp[i] += cnt[i];
        if (r[i] > 0) { // 不能重复喂,只能由点 i 涉及到的猫的范围的左边转移过来
            dp[i] += dp[r[i] - 1];
        }
        if (i) {
            dp[i] = max(dp[i], dp[i - 1]);
        }
    }
    cout << dp[n] << endl;
}

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

  • 28
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值