Блог пользователя flamestorm

Автор flamestorm, 2 года назад, По-английски

We hope you enjoyed the contest!

1950A - Stair, Peak, or Neither?

Idea: SlavicG

Tutorial
Solution

1950B - Upscaling

Idea: flamestorm

Tutorial
Solution

1950C - Clock Conversion

Idea: mesanu

Tutorial
Solution

1950D - Product of Binary Decimals

Idea: flamestorm

Tutorial
Solution

1950E - Nearly Shortest Repeating Substring

Idea: mesanu

Tutorial
Solution

1950F - 0, 1, 2, Tree!

Idea: flamestorm

Tutorial
Solution

1950G - Shuffling Songs

Idea: SlavicG

Tutorial
Solution
Разбор задач Codeforces Round 937 (Div. 4)
  • Проголосовать: нравится
  • +28
  • Проголосовать: не нравится

»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится +6 Проголосовать: не нравится

thanks for the fast editorial

although they are all empty :(

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

A fun little exercise for G: Shuffling Songs. Consider this code that finds the longest hamiltonian path of a subset of vertices. What is the time complexity of this code? Is it $$$O(N^3 \cdot 2^N)$$$ or $$$O(N^2 \cdot 2^N)$$$ or $$$O(N \cdot 2^N)$$$? (With proper proof/arguments).

»
2 года назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

F can be solved in O(1), so why a + b + c <= 3*10^5??

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

thanks you for the quick editorial and good round!

»
2 года назад, скрыть # |
 
Проголосовать: нравится -17 Проголосовать: не нравится

Hello I am gokula kannan siva ji

»
2 года назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Hey! I am solving G using DFS to find the component of largest size. Why is it giving WA on test 5. Please look at my Submission

Detailed approach:

create a graph with vertices as songs from 1 to n. Two vertices are connected if they have a common artist or genre. Using dfs, find the largest component. Is my approach Wrong??

  • »
    »
    2 года назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится -7 Проголосовать: не нравится

    Consider

        a
        |
    b-c-d-e-f-g
      |___|
    

    Is this graph connected? Can you find a way to arrange all vertices in order?

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится +4 Проголосовать: не нравится

      so how to approach this? i read about hamiltonian path! but i didnt get it! please explain

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
        Rev. 2  
        Проголосовать: нравится +6 Проголосовать: не нравится

        [EDIT: This solution is incorrect, check end of this comment]

        I also did with DFS and DSUs. The trick is do DFS from a node and find the length of the longest "path" from each of them. You can do this by returning (max path length + 1) at each node.

        ll dfs(ll node, vector<vll> &nb) {
            dfsvis[node] = true;
            ll plen = 0; 
            for (auto &n : nb[node]) {
                if (dfsvis[n]) continue;
                plen = max(plen, dfs(n, nb));
            }
            dfsvis[node] = false;
            return plen+1;
        }
        

        You will notice that I'm setting dfsvis[node] = false; while exiting the node. This ensures that every path is tried and non-optimal results aren't returned. (Think what if a node that is part of the longest path is visited before we can check for it, that's why we unvisit it)

        However only this solution will give a TLE when all 16 songs have same genre and writer coz you check every possible path. To counter this, I checked if the max length that I'm getting is the max possible. If it is no point in keeping checking.

        Now what is the max possible length? The size of the connected component! I found it using DSU, you can use other methods. To check, at each branch, I add the max length of that branch and the number of visited node and see its it's equal to the size of the connected component.

        Submission: 253849577 [HACKED]

        EDIT: This submission has been hacked and I discovered that my code has n! worst case time complexity.

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Let's suppose vertex a and b both have first string(genre) same as first string of c then we can arrange like a-b-c-d-e-f-g.

      consider other case like a,d both have second string(writer) same as second string of c then we can arrange like b-c-a-d-e-f-g.

      so trying all possible case i guess we always find some arrangement

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        You don't need to suppose anything. The graph I shared explicitly means that there is nothing common between vertex $$$a$$$ and any other vertex except vertex $$$d$$$. How would you find an arrangement then? Hint : It's not possible.

        But if you stick to your strategy of finding maximal connected component size, you'd get an incorrect answer.

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      This isn't a good counter-example since this graph cannot exist (a must have an edge with at least one of c/e, or c must have an edge to e). Add an edge between c and e, and that suffices for a graph with no Hamiltonian path under the constraints of this problem.

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Fair enough. The OP had an impression that their algorithm must be true for general graphs as well, hence I randomly constructed a counter example.

        But you're correct, given the context of this problem, there should be an edge between c and e. Fixed.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

what would be the rating for problem E? just wondering

»
2 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

I loved the problem G. The constraints were a subtle hint already but I tried to build a graph between connected components (an edge between song $$$i$$$ and $$$j$$$ iff genre or artist is same). After looking at the graph, it was pretty evident it is a Hamiltonian Path bitmask DP. Figuring it out was very fun! Great problemset overall for Div4 users.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why is my submission for E wrong tho i tested them all? ;-; here's my submission: 253815920

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain the reasoning behind the Time complexity Anaylsis given for D? I didnt get the idea or how those cubic/quantic equations got formed.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone explain solution to problem E

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I was a little disappointed with the pretest in task G. It was clearly my fault for underestimating the problem. But I was disappointed that out of 38 pretests, not a single one made simple DFS got Time Limit Exceeded in a task where you intended dp using bitfield as the correct answer.

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Explain why it is impossible to find a component in G with the maximum number of vertices. Essentially, vertices have two types of edges, those connected by author and those connected by genre. If there are > 2 edges at a vertex, then some vertices will form a cycle, where the order is no longer important.

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For problem D why cant we just factorize the given number and if the prime factorization contains any digit which are not binary decimal then we will multiply them together lets call it as Product and the numbers which are binary decimal we will leave them as it is. Now if the Product is binary decimal then answer is yes else no ?

if the number is already a binary decimal we will simply return yes

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please try and hack my E solution. Submission Link

»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится +12 Проголосовать: не нравится

My solutions to problems A

code

My solutions to problems B

code

My solutions to problems C

code

My solutions to problems D

code

My solutions to problems E

code

F sadly I didn’t solve it, but I had the logic for creating a binary tree, I just didn’t have time to implement it))

My solutions to problems G

code
»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

решение за O(log(a)):

Для начала построим полное бинарное дерево наибольшей высоты, используя вершинки первого типа

Как это сделать: найдем наибольшее n такое, что 2 ^ n — 1 <= a, тогда n — будет высотой этого дерева, а свободных ребер(ребер, к которым надо привязать вершинку) будет 2^n

Тогда есть два случая: либо вершинок первого типа не останется, либо их останется a — 2^n — 1 штук, чего не хватит на новый слой. Если вершинок первого типа не осталось, то привязывая к свободным ребрам вершинки второго типа, мы не увеличим количество этих свободных ребер. Тогда ответ — сeil(b / кол-во свободных ребер)

Если же вершинки первого типа остались, то привяжем их на новый слой. Каждая привязанная вершинка даст +1 к количеству свободных ребер. Так как на новом слое еще остались места, нужно заполнить его вершинками второго типа. Если количество свободных мест после расстановки всех вершинок первого типа больше ил равно количества вершинок второго типа, то ответ — n + 1. Иначе вершинок второго типа слишком много, тогда дозаполним новый слой ими, а дальше начнем заполнять новые слои, пока вершины второго типа еще есть. В этом случае ответ n + 1 + ceil(кол-во оставшихся вершин второго типа после заполнения n+1 го слоя / кол-во свободных ребер)

код: 253819555

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How can this solution for D have tle,its constant time

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In solution of E why are we only checking for prefix or sufix of length l and not and string of length from the middlee?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

F can be solved in O(1) if you know the property of complete binary trees (number of nodes at each level increases by a factor of 2) and a little bit of maths.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

253824980

WHY IS THIS WRONG QUES 4

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Why didnt Dp worked for the solution D. Its shows TLE. Here is my solution : https://mirror.codeforces.com/contest/1950/submission/253751070. PLEASE HELP

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

after knowing that c == a+1 in F the problem became very easy and solved it in 3 lines submission. the leaves are what bothered me when solving it in the first place

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

253841056

My solution in O(1) using maths...

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится +5 Проголосовать: не нравится

I believe on question E, instead of trying for all n, we can first compute the divisors in O(sqrt(n)) with the following: go until i * i <= n and, if n % i == 0, add to the divisors array not only i, but also n / i.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

2nd question was very bad question.I hate such type of questions

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Another weak pretests day? What is it with codeforces and weak pretests.... (problem E)

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

nvm, my assumption was wrong

»
2 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Adding my live coding + commentary here: https://www.youtube.com/watch?v=kQOWiTvgah0&ab_channel=DecipherAlgorithmswithSaptarshi

I'd be glad if somebody finds it beneficial!

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

i just want to know that for e question,i submitted my code in c++17 and it got wrong answer on test2,then after contest i checked that test and it was giving right answer on my visualstudiocode compiler and then i submitted that same code in c++20 and it got accepted,now i want to know why is it so and am i responsible for this mistake of choosing compiler or is it a error from codeforces;s side

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem G was great

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Wrote G for 80 minutes, 3KB of non-template code, only to find I went in the wrong direction. I tried to build a bipartite,one side is Genre and the other is Writer, and check for a Euler path, but it doesn't work on example 4 When can I AK Div4......

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is my intuition for G correct, because i am getting wrong answer in test 5, testcase 168. approach is to create a graph, and each category of artists and genre represent an edge between two adjacent nodes, ie.if two nodes have same genre or same artist then there will be edge between them. so after we create a graph we can just find the size of largest connected component and print n-c.(n is total number of nodes)

»
2 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

name of problem D was a hint about its tag.

Spoiler
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Ive seen many solutions for G problem using dp + bitmask. Could someone explain why only using bitmask and trying to take all the vertices you can doesnt work? Here my solution . I just used bitmask + bfs but got wa on test 5

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anybody explain how this code gets AC? https://mirror.codeforces.com/contest/1950/submission/253676853

»
2 года назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

I want to use DSU to solve G but failed ,who could tell me why,I want to find the biggest set f[k] and return n-size[k] thank you

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anybody tell what's wrong with (G)this submission, I am getting WA on test case 3 , can't find the mistake

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

O(logn) solution for problem f

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help me with this solution, how is this different from the one in tutorial?

https://mirror.codeforces.com/contest/1950/submission/253833340

»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

can't g be solved by optimizing the code for finding the longest path in a graph problem

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I have a question. yesterday I sent in and was accepted for four problems in the Division 4 contest while now it turns out that I only sent in one. Why?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Be patient, system testing is currently in progress. Your three others are in the line for rejudging.

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      what do you mean by rejudging? sorry i am new here

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Normally in a Codeforces contest, in-contest submissions are judged by a preliminary testset (or more commonly called pretests) to reduce server load. After the contest ends (and with an extra delay for preparation and such), all AC submissions will be rejudged in a phase called "system test" with the full testset available.

        For Div.3, Div.4 and Educational however, the in-contest testset is complete, yet after the contest ends, a 12-hour open hacking phase is presented for community to hack any AC submissions they felt incorrect but the original testset missed. After the hacking phase, those hacks are added to the testset, and after some more delay for preparation, system test will kick in.

»
2 года назад, скрыть # |
Rev. 5  
Проголосовать: нравится 0 Проголосовать: не нравится

Thanks for the complexity analysis in D. Note that most of the 30 numbers are IGNORED so the complexity is far lower than n^0.587.

If you do precomputation then its about $$$O(N^{1.4})$$$ (not a tight bound just a bigger bound than $$$O(N^{1+(log(2)/log(10))}$$$)).

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why 2 segments are sufficient for problem E

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is G NP-complete? G can be reduced into a longest path problem, but there are additional constraints.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

A brute solution for D

code
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in Ques. E, why do we need to iterate in reverse also?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    "**acabab**"

    look at the above testcase: Possible length of subarray is 1,2,3 or 6.

    If we only consider the subarray starting from beginning, then the answer would have been 6.

    But considering from last, we can achieve an optimal answer of 2, where we concatenate ab 3 times to get ababab which satisfies the given condition.

    This condition wouldn't have satisfied if we had considered it from the beginning where we would have got acacac which differs from the given string in 2 places and won't be considered for solution. Hope that u have gotten my point.

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Sometimes, i feel that i don't think correct to solve the problems like D.Product of Binary Decimals, i didn't think correct and i don't know DP to think about the solution to this problem. How can i fix this problem?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Where s the tutorial for G?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is it possible to solve G using bitmask dynamic programming?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

i just want to know that for e question,i submitted my code in c++17 and it got wrong answer on test2,then after contest i checked that test and it was giving right answer on my visualstudiocode compiler and then i submitted that same code in c++20 and it got accepted,now i want to know why is it so and am i responsible for this mistake of choosing compiler or is it a error from codeforces's side,also i dont think i used anything in my code which would not have worked in c++17 according to me.someone please help

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    pow is a dangerous function in C++ since its precision is really bad, combining with the rounding-down nature of integer casting it might corrupt the prime check procedure. Probably 64-bit processing in CF's C++20 made it a bit more precise so you passed, but C++17 32-bit didn't.

    A safer / more stable way for square root alone should be sqrt. You can also try checking for i * i <= n, but careful for overflow (normally if the step is just i++ I'm still confident that such overflow won't happen).

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help me? I use dsu(Disjoint set union) to solve problem G, but I don’t know why it always goes Wrong answer on test 5. I spent a long time thinking about it, but I had no idea why.Here is my code:253955348

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For question g, is it wrong to use the length of the longest path in the graph?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

F за $$$O(log(a))$$$ корм.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Here is an elaganto solution with T(N) = O(log(N)), S(N) = O(1) for 1950F — 0, 1, 2, Tree!

void solve()
{
    int a, b, c;
    cin>>a>>b>>c;
 
    int height = -1;
    if(a > 0){
        // a will have a + 1 slots or children
        int slots = a + 1;
        if(slots == c){
            // Calculating a's height
            int as_height = 0;
            for(int a_copy = a; a_copy; a_copy>>=1, as_height++);

            if(b == 0){
                height = as_height;
            }
            // If putting a's results in a complete tree
            else if((slots & (slots - 1)) == 0){
                int bs_height = ceil((double)b/slots) - 1;
                height = as_height + bs_height + 1;
            }else{
                // If putting a's is not producing a complete tree
                // Then how many slots are there above the base as_children
                /*
                      a
                     / \
                    a   _ <==== This is left slots
                   /\
 
                    Here, 1 is the number of empty slot
                */
                int left_slots = (1<<(int)(log2(slots) + 1)) - slots;
                if(b > left_slots){
                    int b_left = b - left_slots;
                    int bs_height = ceil((double)b_left/slots) - 1;
                    height = as_height + bs_height + 1;
                }else if(b <= left_slots){
                    height = as_height;
                }
            }
        }
    }else{
        if(c == 1){
            height = b;
        }
    }
 
    // print(a, b, c, height);
    cout<<height<<nline;
    return;
}
»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I have a beautiful recursive approach for Question D https://mirror.codeforces.com/contest/1950/submission/253808823

Let F(n)=b1*b2*b3*..*bk , where b1,b2,..bk are binary decimal numbers . Then F(n)=b1*F(n/b1)

Thus if we find a binary decimal factor of n then if F(n/b1) is a product of binary decimal numbers then n also is .

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

It's been 4 days, still no tutorial for G?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

https://mirror.codeforces.com/contest/1950/submission/254107386 could anyone help me with this (G question) , my logic is that i have made a graph with vertices having edge on having same genre or same writer or both , and then find the longest path in the graph , but it is giving wrong answer in testcase 3 at 163rd token :/

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

what is the difference between (a<b && b<c) and if(a<b){ if(b<c){ cout<<"STAIR"<<endl; }else{ cout<<"PEAK"<<endl; }else cout<<"NONE"<<endl;

»
16 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

is there any method for the following problem,,,,given a graph and some elements(which are part of the graph),, find whether its possible to obtain a straight chain of the given elements,,, i tried doing this for the shuffling song problem,,but couldnt figure it out

»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't understand why DSU will not work for G, its like grouping the songs with either one pair same? Submission: 322571467

»
7 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Post a G question written using DFS.

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

void LICDAK() {
    int n;
    cin >> n;
    unordered_map<string, vector<int>> mg;
    unordered_map<string, vector<int>> mw;
    for (int i = 0; i < n; i++) {
        string g, w;
        cin >> g >> w;
        mg[g].push_back(i);
        mw[w].push_back(i);
    }
    vector<set<int>> gs(n);
    for (auto& [_, v] : mg) {
        for (int x : v) {
            for (int y : v) {
                if (x != y) gs[x].insert(y);
            }
        }
    }
    for (auto& [_, v] : mw) {
        for (int x : v) {
            for (int y : v) {
                if (x != y) gs[x].insert(y);
            }
        }
    }
    vector<vector<int>> gt;
    for (set<int>& s : gs) {
        gt.push_back(vector<int>(s.begin(), s.end()));
    }
    // for (int i = 0; i < n; i++) {
    //     cout << 'I' << i << ' ';
    //     for (int x : gt[i]) cout << ' ' << x;
    //     cout << endl;
    // }
    vector<vector<int>> memo(16, vector<int>(1 << 16, -1));
    auto dfs = [&](auto&& dfs, int x, int is) -> int {
        int& res = memo[x][is];
        if (res != -1) return res;
        res = 0;
        for (int& y : gt[x]) {
            if ((is >> y) & 1) continue;
            res = max(dfs(dfs, y, (is | (1 << y))), res);
        }  
        return res + 1;
    };
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans = max(ans, dfs(dfs, i, (1 << i)));
    }
    cout << n - ans << endl;
    return;

}

int32_t main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        LICDAK();
    }
    return 0;
}