### awoo's blog

By awoo, history, 8 months ago, translation,

1681A - Game with Cards

Idea: BledDest

Tutorial
Solution (BledDest)

1681B - Card Trick

Idea: BledDest

Tutorial
Solution (awoo)

1681C - Double Sort

Idea: BledDest

Tutorial
Solution (awoo)

1681D - Required Length

Idea: BledDest

Tutorial
Solution (BledDest)

Idea: BledDest

Tutorial
Solution (awoo)

1681F - Unique Occurrences

Idea: BledDest

Tutorial
Solution 1 (awoo)
Solution 2 (awoo)

• +63

 » 8 months ago, # |   0 Why does my submission(158304624) for C give WA ("Arrays are not sorted")? My approach is to store each pair of $a_i$ and $b_i$ for both sorted and original arrays and compare all pairs, since pairs can't change, and then use a simple bubble sort to print swaps. I tried testing it on cfstress but it didnt give me any counterexamples(link).
•  » » 8 months ago, # ^ | ← Rev. 3 →   0 here is your accepted codeyou must do swaps in both arrays as the problem say but you did swaps in only one array. you can compare your previous code with this new code to see the few changes.
•  » » » 8 months ago, # ^ |   +3 ah I see, thanks :)
 » 8 months ago, # |   +6 Through many hacks and further FST's on problem D, my rating is now 1899 :))
 » 8 months ago, # |   0 can any body tell me why this happened? these submissions are shared the same code 158313258: GUN C++ 17 158313168: GUN C++ 14 158313129: GUN C++ 20 (!! TLE !!)
•  » » 8 months ago, # ^ |   +5 The C++20 GUN is NA-45 while the C++17 one is AK-47
•  » » » 8 months ago, # ^ |   0 but why
•  » » 8 months ago, # ^ |   0 https://codeforces.com/blog/entry/62393Because every value you insert is a multiple of the input it's possible to make a test case that blows up unordered_map. I'm just surprised that no-one found a way to hack the older implementation (possibly the primes required have higher digits and don't insert enough values to hit the time limit).
•  » » 8 months ago, # ^ |   0 Don't use unordered maphttps://codeforces.com/contest/1681/submission/158319237
•  » » 8 months ago, # ^ |   0 I've seen something like that before.Check Here.
•  » » 8 months ago, # ^ |   0 I also did the same but don't know what got into my mind I also submitted my solution with map during the contest and during system testing my unordered_map solution gave TLE on TC 176 but map didn't and hence I was saved.
 » 8 months ago, # |   +4 Finally I became a cyan !!
 » 8 months ago, # |   +6 For problem C the name "Double Sort" gives the hint to use Bubble Sort
•  » » 8 months ago, # ^ |   0 Exactly!!! I used Selection Sort in this problem during the contest and unfortunately I got WA in both the submissions, but now when I implemented the exact same logic in Bubble Sort, it got accepted. This indicates that implementation with selection sort has some complications which bubble sort doesn't have.
•  » » » 8 months ago, # ^ |   0 i did selection sort
•  » » » 8 months ago, # ^ |   0 It's easy to see that any comparison based sorting algorithm is entirely valid for this problem. If you're getting WA that means you didn't implement it correctly.
•  » » » » 8 months ago, # ^ |   0 Not sure about that. What we need is not comparison-base but stability. Any stable sort should do
•  » » » » » 8 months ago, # ^ |   +3 If you sort the values as pairs $(a_i, b_i)$, you don't need stability.
•  » » » » » 8 months ago, # ^ |   0 A comparison based sort on both array values doesn't need stability, as all entities in question will already be considered by the comparator. something like this works just fine: bool compare_first(const std::pair &a, const std::pair &b) { if(a.first == b.first) { return a.second < b.second; } return a.first < b.first } //---------- bool compare_second(const std::pair &a, const std::pair &b) { if(a.second == b.second) { return a.first < b.first; } return a.second < b.second } Just make two passes as follows (and track the inversions): any_sort(arr.begin(), arr.end(), compare_first); any_sort(arr.begin(), arr.end(), compare_second); 
•  » » » » » » 8 months ago, # ^ |   0 I got it from Bleddest's response.It's just that my solution relied on stabilityThen again, there is no need for it to be comparison-based, you can implement this with any sorting
 » 8 months ago, # |   +10 Something really unfair happened to me in this contest... I got accused of cheating in this round ... But this is not true. I guess this happened to me because I use Python and problem A and B where really straight forward , for example in problem B , most of the line of the code is for taking input and the actual solution is only of 1 line , which can be easily be similar to someone else. (158180193 my solution of B). This kind of things should be avoided at all cost , because it demotivates the falsely accused person!
 » 8 months ago, # |   0 Can someone explain how we are getting min operations with BFS in problem D. Shouldn't we sort the string before multiplying?
•  » » 8 months ago, # ^ | ← Rev. 2 →   0 Basically, we are using BFS because we don't know the future digits coming into our number after multiplication. Hence we can't pick any particular path before reaching the destination(i.e number of n digits) so we will visit the all the possible numbers(the numbers that we get after multiplying the current number with all distinct digits present in the current number) if we haven't visited them yet. As we are looking for all the possibilities sorting the string won't help.
•  » » » 8 months ago, # ^ |   0 thanks got it :D
 » 8 months ago, # | ← Rev. 3 →   +2 Can anyone explain to me F better? What are the transitions, mentioned in the editorial?
•  » » 8 months ago, # ^ | ← Rev. 2 →   0 I think it is basically building Splay tree and then normal DP . So , if you don't know splay tree just go through it once . You will find a blog and video on it in CF and also on CC .
•  » » 8 months ago, # ^ | ← Rev. 2 →   0 If you like, I can give another solution to you.But it solves the problem from an aspect differ from the tutorial.
•  » » » 8 months ago, # ^ |   0 Yeh Sure, lingfunny !
•  » » » » 8 months ago, # ^ | ← Rev. 2 →   +6 Sorry for the late reply and my poor English.Let's try to calculate the answer from the different weights.For every kind of weight of an edge, try to figure out the value that this kind of edge contributes to the answer.For example, in the following illustration, the contribution of edges weigh $2$ is $3\times1+3\times3=10$.Why? If we cut the every edge weighs $2$(red edges), we will get $3$ new trees.For the edge $(6, 7)$, the new trees it connected are $4,5,6$ and $7$, so answer will add $1\times3$. Because for the paths from $u$ to $v$, where $u=4,5,6$ and $v=7$, the value $2$ will definitly appear exactly once.For the same reason, edge $(4, 1)$ will contribute $3\times 3$ to the answer.Obviously, the edges with different values can't influence each other, so You can calculate the different values of edges partly.Here is how to calculate the answer of edges with same value.Firstly, get the 'bracket order' of the tree. Let $L[u]$ and $R[u]$ be the begining and ending indices of $u$ in the 'bracket order'.It's another kind of Euler Tour of Tree. Instead of output the number as soon as you visit a node, you will output the number only when you first visit the node or finish visiting the node.I don't know how to translate 'bracket order' to English since I'm Chinese and my English is poor. For example, the 'bracket order' of the tree in the illustration is $\texttt{4 5 5 6 7 7 6 1 2 2 3 3 1 4}$, hope you can understand what I want to express from the example. Or maybe you can see my code in the end.Then, for a tree, cut the edges in dfs order and split the subtrees out, and cut the edges in a subtree recursively. function Cut(int CurL, int CurR, int &CurE): for CurE in [CurL, CurR]: cur_real_size -= size[v] append value of Cut(L[v], R[v], CurE) to real_subtree_size for val in real_subtree_size: answer += val * cur_real_size return cur_real_size // edges are sorted in dfs order, so an edge have a dfs order, you can judge whether the edge is in the subtree or not. // CurE means the index of the edge you are visiting When you cut the subtree recursively and update CurE, CurE is increasing and always less than $O(n)$. So the time complexity is $O(n)$.You can see my submission to have a better understanding, I think it's quite short: 158222894Actually I don't know whether it's violation of rules for me to type the solution in the comment. If it is, please tell me and I will delete my comment at once.Apologise again for my poor English.
•  » » » » » 8 months ago, # ^ |   0 May below code can explain what is bracket order. def bracket_order(node): print(node.val) for child in node.children: bracket_order(child) print(node.val) 
•  » » » » » » 8 months ago, # ^ |   0 Yeah, thank you, very clear.
 » 8 months ago, # |   0 What is the solution to the dynamic connectivity problem mentioned in F? Is there a good tutorial or video about it?
•  » » 8 months ago, # ^ |   +3 Codeforces EDU DSU section last lesson where the guy talks about DSU Mo's, offline dynamic connectivity using rollback DSUs. Go check that out.
•  » » » 8 months ago, # ^ |   0 Where is this section?
•  » » » » 8 months ago, # ^ |   +20 Damn I either overestimated the average people’s ability to navigate codeforces or codeforces is not as user-friendly as I thought for people whose English is not their first language, based on this comment. It’s under codeforces EDU DSU step 3 theory. link
 » 8 months ago, # |   -13 Apparently C++14 log10 doesn't work properly for counting digits of numbers like: $10^{k} - 1, (k>=15)$ :(Anyone know why?
•  » » 8 months ago, # ^ |   +4 Precision issue? Doubles and long doubles have limited precision, so the rounding could be off when the value is very close to a power of 10 for large numbers. It’s better to not use doubles/long doubles if possible. Just stick with integers manipulation
 » 8 months ago, # |   0 If someone solved F using HLD, could you please share your approach? Here's the submission SSRS_ made using HLD: 158189118
 » 8 months ago, # |   +3 If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints. A: Game with Cards B: Card Trick C: Double Sort D: Required Length E: Labyrinth Adventures: Under maintenance F: Unique Occurrences If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).
 » 8 months ago, # |   +9 Can someone explain the solution for problem E that uses segment tree, please?
•  » » 8 months ago, # ^ |   0 +1
•  » » 8 months ago, # ^ |   +11 The idea is to maintain a structure in each node (let's say this node represents layers $[i,j]$), that contains the following information: shortest distance starting in front of top door of $i$-th layer and ending in front of top door leading to $(j+1)$-th layer ...3 other similar values for other combinations of top/bottom doors. It is possible to merge the structures for ranges $[i,j]$ and $[j+1,k]$ easily (maybe not, but my code was quite sloppy).To answer queries, if the cells are in the same layer, it will just be the distance between them. Otherwise, we can find the answer for going from $[\text{starting layer},\text{ending layer})$ and then find the time taken to get to top/bottom doors for each layer. Then the answer is just the minimum of the possibilities $+1$ (to take care of crossing the door to the $j$-th layer).By the way, the identity element for the structures (or answer for $[i,i]$) is not all zeroes, it must be $\infty$ for top->bottom or bottom->top to maintain consistency. And some other stuff, implementation requires care I guess...Submission: 158239579
•  » » » 8 months ago, # ^ |   +6 Interesting solution, thank you for sharing!After some time thinking about it, I realized that it's just a waste of time to implement this :(
 » 8 months ago, # | ← Rev. 3 →   0 Can someone please tell me why, for C — Double Sort, the simple bubble sort that swaps every time there is an adjacent inversion for either array A or array B works? For more details, see my submission. I spent some time trying to come up with a proof, but cannot.
•  » » 8 months ago, # ^ | ← Rev. 2 →   0 Ok if at all someone is interested, I claim the following.Claim 1: If there exist indices $i \neq j$ such that $A[i] < A[j]$ and $B[i] > B[j]$, then both arrays are unsortable, i.e., the answer is $-1$.Claim 1 is easy to prove.Claim 2 (The other direction of Claim 1.): If there do not exist indices $i \neq j$ such that $A[i] < A[j]$ and $B[i] > B[j]$, then both arrays are sortable.Claim 2 is also not very hard to prove.Now, we can prove by induction that Bubble sort always keeps sortable pairs of arrays sortable and that after outer iteration number $i$, the $i$-th maximum of both arrays is pushed to the end in its proper place.
 » 8 months ago, # |   0 omg!why my Problem C code always runtime
•  » » 8 months ago, # ^ |   0
•  » » 8 months ago, # ^ |   0 i find an error in the function cmp ,the ">=" should be ">"
 » 8 months ago, # |   +19 In problem F,if you use link cut tree to maintain the size of subtrees to solve this problem,it will be much easier.(https://codeforces.com/contest/1681/submission/158267779
 » 8 months ago, # | ← Rev. 2 →   0 In problem D,you can use IDA* and you don't need to think about the problem.And it's also can solve the problem.
•  » » 8 months ago, # ^ |   +32 Wow,it's fantastic,but why not show your code to let us see?
•  » » » 8 months ago, # ^ |   0
•  » » » » 8 months ago, # ^ |   +7 I wonder why this submission is for problem D. :)
•  » » » » 8 months ago, # ^ |   +7 And anyway, I don't think you passed problem F.because there's no submission for problem F in your submissions until now (May/26 08:14 UTC+8).If I'm wrong, plz point it out:)
•  » » » » » 8 months ago, # ^ |   0 Sorry,I'm wrong.
 » 8 months ago, # |   +10 Can someone explain the O(n^2) solution of F in a more detailed way? What is i in dp(v,i)? How does the transition work? What does the phrase when you consider gluing up paths from different children mean?And also, why are the Tutorials on the challenging problems not elaborative enough? Those are written in a way that is tough to follow for Pupils or Specialists.
•  » » 8 months ago, # ^ |   +49 I think that Pupils or Specialists just won't gain anything from upsolving the problem. You won't develop the intuition for similar problems by retyping the transitions in dp from the editorial. It's probably better to go solve easier tree dp problems first, learn different ways of counting the paths in trees, and then tackle this problem.
•  » » » 8 months ago, # ^ |   +14 Thank you for the reply. As a modest suggestion, I think it would be a good thing to keep some prerequisite sections in the tutorials (or link to a blog[in case of common techniques/Algo/DS] or the same kind of easier problem) to understand the concepts behind the main problems better.Once again, thank you for writing these problems, it's always good to learn new things through solving problems.
 » 8 months ago, # |   0 Problem C: Can Someone Explain me ! Why My Code Failed Wrong Answer on test 2 (test case 65) 158363800
•  » » 8 months ago, # ^ | ← Rev. 3 →   0 It is usually beneficial if you can find a smaller test case where your code fails, and then try to figure out where your code is wrong.Edit — Test case: 1 6 5 4 5 5 4 5 3 2 2 5 2 3 
•  » » » 8 months ago, # ^ |   0 okk thanks i will try to find my bug but well i already solved the problem using different method
 » 8 months ago, # | ← Rev. 2 →   0 Hi, I would really appreciate it if somebody could tell me why my code doesn't work for C. Let me explain my approach. Bubble sort $a$ and for every $i$, $j$ that is swapped in $a$, swap it in $b$ as well. Store all pairs $(i,j)$. After this bubble sort, $a$ is guaranteed to be sorted If $b$ is also sorted then we are done and dump out the swapping indices stored in step 1 If $b$ is not sorted, we can still sort it if all the swaps necessary to make $b$ sorted, have the property that $a_i = a_j$. Run bubble sort again on $b$ and check if a swap has been made for which $a_i \neq a_j$, if so, report $-1$, else continue with the sort If our algorithm has still not terminated it is guaranteed that $b$ is sorted and $a$ is also sorted, so it is possible and we dump out all the indices we had stored. The judge says my algorithm produces a case in which $b$ is apparently not correct. My code is here. If you're able to come up with a counter-example, I'd really appreciate it if you can tell me what motivated it, I'm still improving my ability to think of counter-examples.
•  » » 8 months ago, # ^ | ← Rev. 3 →   +3 The following approach usually helps me in finding out a counter example:First of all, try to go through the approach and at each step, ask if what you are doing is correct. Is there some case where your assumption is failing? Is there some case where your expected result will not hold.Once you are confident that your approach is correct (on paper), try to go through the code and check if each block is behaving exactly how you want it to behave. You can usually check by taking some small examples.The above process usually helps me finding out the blocks where my approach might be wrong, and gradually helps in getting a counter case.Try with the above approach once for some time. If you are not able to find some counter case after enough efforts, this test case generated by CF Stress might help.
•  » » » 8 months ago, # ^ |   0 Will try this out thanks a lot!
•  » » 8 months ago, # ^ |   0 I have solved the problem with exactly the same logic: 158188911So, algorithm is ok, check your code.
•  » » » 8 months ago, # ^ |   0 thanks a lot!
 » 8 months ago, # |   0 Has anyone solved D using DP?
 » 8 months ago, # |   0 D is one of those rare question where thinking about the brute force solution is not at all tricky/difficult but computing its time- complexity is ! i thought that at each node(in bfs) we can have atmost 8 choices to multiply with (2,3,4....,9) and in worst case scenario we need to multiply x 64 times....(2^64 is of order of 1e19) ..hence leading to (8^64) operations which i though will surely TLE,MLE , hence didn't even try the approach:( Lovely question tho , to know whether u know time complexity well or not ... :) Can any one link out similar question ? looks like i am bad at computing Time complexity!!!!
 » 8 months ago, # |   +3 In editorial of D: I don't understand that is sufficient to say time limit won't exceed. We have 1.5 million states in total. But they can be called multiple times. Can somebody explain its time complexity please?
•  » » 8 months ago, # ^ | ← Rev. 2 →   0 Each 1.5 million states can have at most 9 outgoing edges because you could multiply that state with 1,2,3,...9So number of edges in the graph is upper bounded by 1.5*9
 » 8 months ago, # | ← Rev. 2 →   0 It seems that tourist solved F — Unique Occurences with rollback DSU (submission). Can somebody explain this method? As a side note, it seems that all top participants did not use the intended solution for F.
 » 8 months ago, # |   +26 I've got a significantly shorter $O(n)$ solution for F.I'm really bad at explaining, but let me try. The basic idea is similar to the second solution in the editorial. If we split the graph by each edge weight at a time, the answer is the sum of products of sizes of each pair of components that were formerly connected by an edge (neighbour components from now on). This can be calculated in a single DFS. Now, when we are processing vertex $u$, which is connected with it's parent with an edge of type $x$, we're gonna calculate the answer for the pairs of components (the component that contains $u$, it's "son" components aka all components it neighbours except the one that contains its parent) when split by edges of type $x$. This value can be calculated pretty simply — the size of the component with $u$ is the size of $u$'s subtree — the sum of sizes of subtrees of descendants of $u$ whose parent edges are of type $x$. The neighbour components are similar, just shifted by one more level.In the end we need to explicitly handle the root as if it had all types of edges going upwards.Hopefully reading the code should clear it up: 158219114
 » 8 months ago, # |   0 I got the logic behind the problem D, but, how to get the idea in the contest that we can traverse to all the states possible than applying the greedy approach of going with the maximum possible, as mentioned in the editorial 'optimal locally' ? Is there any constraint below which we could always apply BFS?
 » 8 months ago, # | ← Rev. 2 →   0 code why am i getting RE for problem B
 » 8 months ago, # |   0 Can anyone give me a hint on how can we minimize the number of swaps on Problem C? thanks
•  » » 6 months ago, # ^ |   0 If you use bubble sort you will do at most n*(n-1)/2 swaps. If you construct your algorithm by using bubble sort twice, at most it will do n*(n-1) swaps. For n=100 and a threshold of 10^4, it's more than enough.
 » 7 months ago, # | ← Rev. 2 →   0 .
 » 6 months ago, # |   0 In code for problem D. Why we are not updating the dist[w] with dist[k] + 1 when w exists in the map?
•  » » 5 months ago, # ^ |   0 As we only need the shortest distances, if we have already visited a node, we won't visit it again.
 » 6 months ago, # |   0 Can anyone share their DP code for D?
 » 5 months ago, # |   0 Hi! For the Problem D, could anyone tell why the first one gives WA, but not the second? The first uses a !map[x], while second uses !map.count(x). Why do these behave differently? WA#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef long double ld; typedef pair p32; typedef pair p64; typedef pair pdd; typedef vector v64; typedef vector v32; typedef vector > vv32; typedef vector > vv64; typedef vector > vvp64; typedef vector vp64; typedef vector vp32; double eps = 1e-12; #define forn(i,e) for(ll i = 0; i < e; i++) #define forsn(i,s,e) for(ll i = s; i < e; i++) #define rforn(i,s) for(ll i = s; i >= 0; i--) #define rforsn(i,s,e) for(ll i = s; i >= e; i--) #define ln "\n" #define dbg(x) cout<<#x<<" = "< void swp(T&a,T&b) { T temp=a; a=b; b=temp; } // Useful Funcs // smallest prime divisor // int smp[100001]={0}; // void calcPrimes(){ // forn(i,100001){ // smp[i]=i; // } // ll ct=2; // while(ct*ct<100001){ // if(smp[ct]==ct){ // for(ll j=ct*ct;j<100001;j+=ct){smp[j]=ct;}} // ct+=1; // } // } ll ceil_div(ll a, ll b) {return a % b == 0 ? a / b : a / b + 1;} // Recursive function to calculate gcd long long gcd(long long a, long long b){ if(b==0) return a; return gcd(b,a%b); } /* Iterative Function to calculate (x^y)%p in O(log y) */ long long modex(long long x, unsigned int y, long long p) { long long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res; } int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle // itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not // present in array return -1; } // STL binary search functions // binary_search(start_ptr, end_ptr, num) // returns true if element present, // otherwise returns false. // upper_bound(start_ptr, end_ptr, num) // returns the first index having value // greater than num. // If there is no such index, then returns // length of array. // lower_bound(start_ptr, end_ptr, num) // returns the fiirst index having value // not less than num // Subtract v.begin() or a(in case of array) to get index. // Pointers // for array-> a, a+x // for vector-> v.begin(), v.begin()+x, v.end() const ll MOD = 998244353; const ll mod = 1e9+7; void solve(){ ll n,x; cin>>n>>x; map dist; queue q; q.push(x); dist[x]=0; while(!q.empty()){ ll t = q.front(); q.pop(); string s = to_string(t); if(s.size()==n){ cout<> t; // for(int it=1;it<=t;it++) { solve(); // } return 0; }  AC#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef long double ld; typedef pair p32; typedef pair p64; typedef pair pdd; typedef vector v64; typedef vector v32; typedef vector > vv32; typedef vector > vv64; typedef vector > vvp64; typedef vector vp64; typedef vector vp32; double eps = 1e-12; #define forn(i,e) for(ll i = 0; i < e; i++) #define forsn(i,s,e) for(ll i = s; i < e; i++) #define rforn(i,s) for(ll i = s; i >= 0; i--) #define rforsn(i,s,e) for(ll i = s; i >= e; i--) #define ln "\n" #define dbg(x) cout<<#x<<" = "< void swp(T&a,T&b) { T temp=a; a=b; b=temp; } // Useful Funcs // smallest prime divisor // int smp[100001]={0}; // void calcPrimes(){ // forn(i,100001){ // smp[i]=i; // } // ll ct=2; // while(ct*ct<100001){ // if(smp[ct]==ct){ // for(ll j=ct*ct;j<100001;j+=ct){smp[j]=ct;}} // ct+=1; // } // } ll ceil_div(ll a, ll b) {return a % b == 0 ? a / b : a / b + 1;} // Recursive function to calculate gcd long long gcd(long long a, long long b){ if(b==0) return a; return gcd(b,a%b); } /* Iterative Function to calculate (x^y)%p in O(log y) */ long long modex(long long x, unsigned int y, long long p) { long long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res; } int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the middle // itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not // present in array return -1; } // STL binary search functions // binary_search(start_ptr, end_ptr, num) // returns true if element present, // otherwise returns false. // upper_bound(start_ptr, end_ptr, num) // returns the first index having value // greater than num. // If there is no such index, then returns // length of array. // lower_bound(start_ptr, end_ptr, num) // returns the fiirst index having value // not less than num // Subtract v.begin() or a(in case of array) to get index. // Pointers // for array-> a, a+x // for vector-> v.begin(), v.begin()+x, v.end() const ll MOD = 998244353; const ll mod = 1e9+7; void solve(){ ll n,x; cin>>n>>x; map dist; queue q; q.push(x); dist[x]=0; while(!q.empty()){ ll t = q.front(); q.pop(); string s = to_string(t); if(s.size()==n){ cout<> t; // for(int it=1;it<=t;it++) { solve(); // } return 0; } 
•  » » 5 months ago, # ^ |   0 It's a subtle bug. Ticket 16129 from CF Stress should help you figure out the issue.When people first learn STL, they are taught that the correct way to check the existence of an element in a container is map.find(key) != map.end(). So, when asked to write a function to compute the frequency, their code looks like: if(freq.find(key) != freq.end()) { freq[key] += 1 } else { freq[key] = 0 } But then, they see other people using a fancy implementations which do not check the existence first, such as freq[key] += 1 However, there's a catch. If you access a value not present in the container, it's set to its default value. But, most of the time, it doesn't matter, since we only deal with frequency. A lot of people do not know this, until they solve a problem which uses map with a code fragment like map freq; for(char ch = 'a'; ch <= 'z'; ch++) { if(!freq[ch]) { cout << ch << " is not present" << endl; } } cout << "total unique characters is: " << freq.size() << " oops";