_LeMur_'s blog

By _LeMur_, 2 years ago, In English

1917A - Least Product

Author: zidder
Preparation: _LeMur_
Editorial: zidder
Official solution: 238752794

Hint 1
Hint 2
Hint 3
Solution

1917B - Erase First or Second Letter

Author: _LeMur_
Preparation: _LeMur_
Editorial: zidder
Official solution: 238752759

Hint 1
Hint 2
Hint 3
Hint 4
Solution

1917C - Watering an Array

Author: IgorI
Preparation: IgorI
Editorial: IgorI
Official solution: 238743155

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
Bonus 1
Bonus 2

1917D - Yet Another Inversions Problem

Author: IgorI
Preparation: IgorI
Editorial: IgorI
Official solution: 238743552

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Hint 6
Hint 7
Hint 8
Hint 9
Solution
Bonus 1
Bonus 2

1917E - Construct Matrix

Author: _LeMur_
Preparation: _LeMur_
Editorial: _LeMur_
Official solution: 238752669

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
Bonus

1917F - Construct Tree

Author: _LeMur_
Preparation: _LeMur_
Editorial: _LeMur_
Official solution: 238752543

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Hint 6
Solution
  • Vote: I like it
  • +192
  • Vote: I do not like it

| Write comment?
»
2 years ago, hide # |
 
Vote: I like it -14 Vote: I do not like it

Auto comment: topic has been updated by _LeMur_ (previous revision, new revision, compare).

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Just Missed C by implementation,and i thought i can solve problems that are implementation based easily...C proved i was wrong.

»
2 years ago, hide # |
 
Vote: I like it +41 Vote: I do not like it

Thank you for the fast editorial! Also between this and Pinely round I have a huge Christmas backlog to upsolve.

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Thanks for the fast editorial

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

problem E is wangba.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

why bitsets

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Oh it was bitsets. I was thinking how to get the two sets in better that $$$n d^2$$$ complexity.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

C was really frustraing. Anyways, nice idea and observation.

»
2 years ago, hide # |
 
Vote: I like it +19 Vote: I do not like it

bitset in the author's solution,

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it -18 Vote: I do not like it

    I really don’t understand why after EVERY ROUND with a bitset in author’s solution, someone has to complain. Bitset is a very good optimization by at least a factor of 32, which is huge both in competitive programming and real life job programming: imagine google pages or apps loading 32 times slower! Bitset is also something quite easy to learn, a lot less advanced than (for instance) a segtree, so it appearing in a div2F or E should really not represent a problem. So why not use it when it is available and force contestants to know it as well?

»
2 years ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

C's pretests are really weak, I did same thing with k instead of 2 * n +1; how this case couldn't create? Respect to authors but these are really unnecessary things for FST :(

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

fst round lets gooo

»
2 years ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

Overkilled and missed D by a few minutes.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

any countertest for C 238728699 please?

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +9 Vote: I do not like it
    1
    8 10 10
    8 1 2 3 4 5 6 7
    1 1 1 1 1 1 1 1 8 1
    

    you can do 1st type of opearation on 1st 9 days, then 2nd type operation on 10th day. ans will be 7 in this case.

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

who solve B with dp or something different from guide?

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

A more intuitive solution for B is to iterate over string and find number of distinct characters from 2nd position onwards. At every index, number of distinct strings of length (n-i+1) that can be made is number of distinct characters. Time complexity will be O(N) as set will have constant time complexity(max size is 26).

»
2 years ago, hide # |
Rev. 3  
Vote: I like it +32 Vote: I do not like it

E is nice.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Let see if Specialist is in sight!!!

»
2 years ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Can anyone explain why this works for B?

    set<char> c;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      c.insert(s[i]);
      ans += (int) c.size();
    }
    cout << ans << '\n';
  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    Read the editorial, because the problem reduces to for each $$$j$$$ count all possible $$$s_i s_j s_{j+1} \ldots s_n$$$ where $$$i \lt j$$$

    and choices for $$$s_i$$$ are exactly the number of distinct characters in the prefix $$$s_1 \ldots s_{j-1}$$$

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    For a string of remaning suffix length l, you can take remove any (n-l-1) elements from (n-l) elements, so only one character is left , thus (l+1) length string with number of distinct character at starting

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    First let us suppose all characters are distinct eg. abc then possible strings will be abc, bc, ac, c, b, a. See we have 1 occurrence of length 3, 2 occurrence of length 2 and 3 occurrence of length 3. So what we are doing is we are selecting the Substring from [0,i) and deleting it except one character which gives one occurrence. There are total i ways to select one character between [0,i) substring. Therefore, we need the count of distinct characters at every position of i and add it to our answer.

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Rare rare contest where system test cases are tricky. I can't remember the last time so many people got their submissions rejected. Unfortunately Problem $$$C$$$ test case $$$26$$$.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

https://mirror.codeforces.com/contest/1917/submission/238696258 Can anyone tell why my sol gives wrong answer

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Look at the constraints !! It says $$$a_{i} \leq 10^9$$$. Suppose all $$$a_{i}$$$ have that upper bound value. Their multiplication will be $$$10^{900}$$$ which can't be represented as $$$long$$$. There will be overflow leading to unexpected behavior.

»
2 years ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

I guess C was the toughest problem.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

solved D for the transpose matrix instead, it was a cool problem too :D

Spoiler
  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can you give intuition behind it? i was thinking same in contest.

    • »
      »
      »
      2 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      consider two rows, if the difference between the q values of both of them is more than 20, the one with the higher q value will have all of it's element greater than the other. now for each difference from 1 to 20, you pre-calculate the number of inversions the cases when the lower q is ahead of the higher one and vice versa. rest can be handled using segment/fenwick trees

»
2 years ago, hide # |
 
Vote: I like it +32 Vote: I do not like it

The submissions of problems A,B,E and F aren't puplic, Only C and D are (I guess it could have been submitted in the manager mode or something). _LeMur_

Nice contest, sadly I was busy so I managed only to attend the last hour :", (that is why I though of doing it in reverse).

I liked the problems F,E,D (in order)

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Div.2 E

Why for n, k:
6 6
solution:
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

wrong?

»
2 years ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

For B, all final strings will be of the form of a single character and a suffix to the right, or just a suffix by itself. For the suffix case, just add n to your answer since there are n suffixes. For the other case, iterate over the suffix and count the #of characters to its left that aren't the same as the one immediately to its left and update the answer by that much to make sure we only consider unique strings.

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    My approach is a little bit different.

    Assuming the answer will always have at least two letters. Then it will be one character and a suffix to the right.

    So the answer would be for each suffix, add the number of distinct characters to its left.

    Lastly, we add the number of distinct letters for answer that only has one letter.

»
2 years ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Not being able to understand why 2*n works in Problem C.


What would happen in this case? when do we perform the first operation of type 2?: 4 10 10 1 1 2 3 1 1 1 1 1 1 1 1 1 4
»
2 years ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

actually crying about C, I had the entire solution up until the 2n+1 part which should be so obvious :(

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone please explain why checking till the $$$(2*n + 1)$$$ th day is sufficient in Problem C?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can somebody explain me, why we are performing first type2 operation in min(d , 2 * n) days?? why 2*n?

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +6 Vote: I do not like it

    Doing the first type2 operation can get at most n points. By doing opration(1,2) n times(2n times operations), you can get n points, too. So if you doing the first type2 operation on (2n+1)th day, why not doing 1+n*(1,2) instead?

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

FST on C ..... [sad]

»
2 years ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

in C problem if we take t=10^3 and n=2^103 then the total time complexity will be tc->10^3 * 2*10^3 * 2*10^3 ie O(t*n*(min(d,2*n))) ie finally tc->4*10^9 so how it is not giving tle as according to me 2sec->2*1e8 operations

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    "It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2000$$$"

    You can't have $$$1000$$$ test cases each with $$$n = 2000$$$ as the sum of $$$n$$$ over all test cases would be greater than $$$2000$$$.

»
2 years ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

For the bonus part of problem E (n is odd). We can solve the problem as follows := First we note that if we have found a construction for k , then by inverting every bit we get another valid construction for k'= n ^ 2 — k.Let us handle odd k , for k >= n and k <= 2n — 1 . we can easily find solution for k = n , for k = n + 2 , we can place 1s at (1,1) ; (1,2) ; (1,3) ; (2,1) ; (3,1) and place rest of them on the diagonal i.e. a[i][i] = 1 for i >= 4. Now by filling 2 × 2 squares , we can get the solution for all odd k in [n, 2n — 1]. For even k <= (n-1)^2 , the problem reduces to the original problem E. Also note we can't have even k > n * (n — 1). For k in [(n-1)^2 + 1 , n * (n — 1)] we can reduce the problem to odd k' = n^2 — k. and we have already solved that. Similarly odds > 2n , can be solved by interting bits for k' = n^2 — k.

»
2 years ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

My screencast & editorial. Only problems A, B, C, but it's more than nothing.

»
2 years ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

D is hard to write code

»
2 years ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Can someone help with C. 238738762

I know that can be solved by $$$ O(n^2) $$$, but can be there other solutions than authors?

I used $$$ dp[i] $$$ like when $$$ a[i]==i $$$ and $$$ dp2[i] $$$ when $$$ (a[i]==i+1) $$$.

My solution is $$$ O(n*log2(k)+k*log2(k)+n^2) $$$

»
2 years ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Can anyone give an example where min(d,n+2) fails and min(d,2*n+2) will pass and proof for 2*n

238785353

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    here you go
    8 11 12
    2 1 2 3 4 5 6 7
    1 1 1 1 1 1 1 1 1 1 8

    correct answer should be 7 but if you will iterate only till n+2, then you will get 5 as result which is wrong.

»
2 years ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

My solution for problem D is exactly $$$\mathcal O(n \log n \log V)$$$, which is the same as the tutorial. But it gets TLE on test 6, can anyone tell me why? thanks!

238788200

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone explain in C why it is required to iterate up to 2*n???

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Because after 2*n , We can't gain more than value of 'n' from type one operation . But we can gain more than 'n' from type two and one alternatively . So its optimal to not choose operation 1 after 2*n onwards . So we stop when it reaches 2*n .

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    see we know that after doing our first op2(if it is optimal),we need to repeat op1 and op2 for the rest of the days. But our score can be atmost n for the first op2.This is because we have n integers,so n positions can have ai=i atmost. So, n can be the max score from out first op2.Now,we know after doing our first op2 we will add (d-i)/2 to our score.if 2*n days have occured,this means if we directly start from repeating op1 and op2 we will get our score as 2*n/2=n and dont need to check for first op2 anymore.As after 2*n days we will always get an answer greater than n which cant be acheived anyhow by the most optimal score of first op2.so after 2*n days our most optimal approach will be to repeat 1st and 2nd operation .

    • »
      »
      »
      2 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      I read many comments till now and yours was the one that finally made me understand why 2*n. Thank you for the wonderful explanation.

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

For bonus part of C, you can refer to my solution. Time Complexity: $$$O(n\;log(d)\;log(k) + n\;log(n))$$$.

https://mirror.codeforces.com/contest/1917/submission/238793642

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I was getting (WA) on test case 26 for Problem C

»
2 years ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

I solved C with going till $$$max(min(n,d),k)$$$ and it passed. You can try to hack, submission id: 238742923

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem B. Erase First or Second Letter ***** in second pretest ababa i am getting 10 strings please correct if i am wrong-->

ababa,baba,aaba,bba,aba,ba,aa,ab,b,a

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

"This array is non-increasing and adding 1 to any of its suffixes keeps it non-increasing ". Isn't performing the first operation will increase its prefixes rather than its suffixes?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

A Silly mistake in C ruined my contest

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Great contest, in problem C when I try n times and get WA on test 3, I have a lucky guess 2*n and get AC :)

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

I have a 2n proof for C.

The min ans if we do reset at the begin is floor((d-1)/2).

The max ans if we wait after 2n op is n+floor((d-2n-1)/2)<=n+floor((d-1)/2)-n=floor((d-1)/2).

So we only try waiting at most 2n op.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can some explain why number of inversion in $$$[x, 2x, 4x, ..., 2^mx, y, 2y, 4y, ..., 2^my]$$$ depends on $$$\log_{2}\frac{y}{x}$$$?

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Is it something like $$$x \gt y$$$ -> $$$x \lt = 2^m * y$$$ -> $$$m \gt = \log_{2}\frac{y}{x}$$$ but how does it affect the entire array ?

»
2 years ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

I did D as given in the tutorial, but I counted inversions using merge sort logic and it gave me TLE. Whereas it passed when I counted using fenwick tree logic. Both methods are O(nlogn) right?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

_LeMur_

In the Solution of "1917D — Yet Another Inversions Problem", there are some mistakes:

  • Incorrect : "By multiplying this number by k" (line 3)
  • Correct : "By multiplying this number by n"

  • Incorrect : if 2x<y<4x, ... = m(m+1)/2 − (m−1) inversions
  • Correct : if 2x<y<4x, ... = m(m+1)/2 − (m) inversions

Similarly, correction needed in "if 4x<y<8x"

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

hello everyone, is there a site that tells you how many greedy problems and how many DFS problems have you solved?

»
2 years ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

Could someone provide a comprehensive breakdown of the DP formulation used to address 1917F - Construct Tree?

While I managed to reach a similar conclusion as outlined in the editorial, focusing on finding subsets whose sums and their complements sum to specific values, particularly ignoring the largest element, the critical aspect of formulating the DP remains elusive to me. In my opinion, the editorial lacks sufficient detail in explaining it.

Upon examining tourist's solution 238702895, $$$dp[i][j]$$$ seems to represent whether there exists a subset of sum $$$i$$$ such that ignoring it allows obtaining a sum of $$$j$$$ (the maximum element obviously is never being considered). However, the intuition and the exact process behind filling the DP table are unclear to me.

  • »
    »
    2 years ago, hide # ^ |
    Rev. 4  
    Vote: I like it +5 Vote: I do not like it

    Let consider diameter consisting of the vertices $$$v_1$$$, $$$v_2$$$, $$$\ldots$$$, $$$v_k$$$ (let's consider that $$$v_i$$$ and $$$v_{i + 1}$$$ are connected for each $$$1 \leq i \leq k - 1$$$) and some vertex in the diameter (let's denote it $$$v'$$$). Now the state of dp will be the following two lengths: $$$dist(v_1, v')$$$ and $$$dist(v_k, v')$$$, we want to find all possible pairs $$$(x, y)$$$ such that we can construct diameter which will have some vertex $$$v'$$$ on it, such that $$$dist(v_1, v') = x$$$ and $$$dist(v_k, v') = y$$$.

    In tourist's solution, the same thing is done. So he tries to find all pairs $$$(x, y)$$$ without using the length of the largest one. And then just checking if there exists one pair $$$(x', y')$$$ such that both $$$x' + l_n \leq d$$$ and $$$y' + l_n \leq d$$$ (because only in this case we can construct tree, we will create edges for all remaining lengths incident to the same vertex $$$v'$$$ in our case).

    I hope, it's already more clear for you :)

    • »
      »
      »
      2 years ago, hide # ^ |
       
      Vote: I like it +5 Vote: I do not like it

      Got it! Your clarification was indeed helpful. The phrase "taking a diameter of length $$$k$$$ and listing its nodes" in the editorial momentarily made me question the solution's direction.

      Framing the DP thinking about any partition of size two for any diameter could potentially simplify comprehension.

      Specifically, identifying the node in between those two partitioned subsets as the one where we connect all non-used edges could add clarity to the explanation. To elaborate further, your example of length $$$k$$$.

      Hence, in understanding the solution, $$$dp(x, y)$$$ represents the possibility of forming two disjoint subsets with specific sums $$$x$$$ and $$$y$$$, utilizing the given set elements (excluding the greatest one).

»
2 years ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

I ended up writing a solution to C that runs in O(nlogk + klogk) if anyone is curious (Bonus 2). Could also be modified to solve Bonus 1 as well.

Solution: https://mirror.codeforces.com/contest/1917/submission/238896629

»
2 years ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

The problem C is so great, love from fst :)

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Somehow during the contest I thought I must do better than O(n^2) in C, and so I missed it despite figuring out the solution a long time ago. But can someone help me with solving it in better than O(n^2)?

»
2 years ago, hide # |
 
Vote: I like it -11 Vote: I do not like it

_LeMur_

F solution without bitset

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

problem D is very cool and educational

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem D is terrible. I just modified the inversion counting algorithm and passed. I can't see any reason to use a segment tree.

»
20 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone please give me the DP solution of Problem B

  • »
    »
    14 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    # include <bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    #define infor(a, b) for (ll i = (a); i < (b); i++)                // in using i
    #define injfor(a, b) for (ll j = (a); j < (b); j++)               // in using j
    #define inkfor(a, b) for (ll k = (a); k < (b); k++)               // in using k
    #define inlfor(a, b) for (ll l = (a); l < (b); l++)               // in using l
    #define inrfor(a, b) for(ll i = (a-1); i >= (b); i--)                // in reverse
    #define inrjfor(a, b) for(ll j = (a-1); j >= (b); j--)                // in reverse
    #define inrkfor(a, b) for(ll k = (a-1); k >= (b); k--)                // in reverse
    #define inrlfor(a, b) for(ll l = (a-1); l >= (b); l--)                // in reverse
    #define tt ll tcs; cin>>tcs; while(tcs--)                       // include test cases
    #define ina(arr,n) ll arr[(n)]={0}; infor(0,n) cin>>arr[i]       // input arr of n elements
    #define inv(vec,n) vector<ll> vec(n);  infor(0,n) cin>>vec[i]; // input vector of n elements
    #define pb push_back
    #define fr first
    #define sc second
    #define sz size()
    #define yes cout<<"YES"<<endl
    #define no cout<<"NO"<<endl
    #define lt length()
    #define lb lower_bound
    #define ub upper_bound
    #define pll pair <ll,ll>
    #define vpll vector <pll>
    #define sll set <ll>
    #define msll multiset <ll>
    #define vll vector<ll>
    #define vvll vector<vector<ll>>
    #define mll map <ll,ll>
    #define all(x) x.begin(), x.end()
    #define pref(p,a,n) vll p(n);p[0]=a[0]; infor(1,n) p[i]=p[i-1]+a[i];
    #define vec2(a,n,m,i) vvll a(n,vll(m,i))
    #define vec3(a,l,m,n,i) vector<vvll>a(l,vvll(m,vll(n,i)));
    const ll INF = 1000000000;
    const ll MOD = 1000000007;
    ll n;
    string s;
    vvll dp;
    ll func(ll i,char c){
         if(dp[i][c-'a']!=-1 ) return -1;
         if(i==n) return dp[i][c-'a']=0;
         ll ans=0;
         if(s[i]==c){ans=1+func(i+1,c);}
         else{ans=2+func(i+1,c)+func(i+1,s[i]);}
         return dp[i][c-'a']=ans;
         
         
    }
    int main()
    {
    tt{
    cin>>n;
    cin>>s;
    dp=vvll(n+1,vll(27,-1));
    cout<<1+func(1,s[0])<<endl;
    }
         return 0;
    }