Common Board#include<iostream> #include<cmath> #include<cstring> long long int n; int main(){ const int k = 256; std::string g = "GOOD"; std::string b = "BAD"; std::string str; std::cin>>str; std::cin>>n; if( n == 0){ std::cout<<"0"<<"\n"; return 0; } int arr2[4]; int bit = 1; int arr[4]; long long int l = 0; memset(arr,0,sizeof(arr)); memset(arr2,0,sizeof(arr2)); for(int i = 3;i >=0 ; i--){ long long int j = pow(k,i); for(int C = 1;C<=255;C++){ long long int h = C*j; if( n >= h){ l = n % h; if(l==0 && i!=0){ arr[i] = 1; bit = 1; n = n-bit*h; break; } else if( i == 0 && n <255){ arr[i] = n; break; } else { arr[i] = n/h; bit = n/h; n = n-bit*h; break; }
} else break; } } if(str == g){ long long sum = 0; for(int i = 0,m = 3; i<=3,m>=0;i++,m--){ sum+=pow(k,m)*arr[i]; } std::cout<<sum<<"\n"; } else if( str == b){ long long int sum = 0; for(int i = 0,m = 3; i<=3,m >= 0;i++,m--) sum+=pow(k,m)*arr[i]; std::cout<<sum<<"\n"; } else; return 0; } dude, 14 lines is enough to solve this problem if u have wa 44 just remove one in the cyclic factorization, if there is one. <=sqrt() + 1 -- bad <=sqrt() -- good Can anybody help? Easy solution without optimization efforts. Use Matrix A= [0 1 0 0 0 0] [0 0 1 0 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [c1 c2 c3 c4 c5 c6] Answer=[[A^(X-N)]*K][N] Thus we must calculate pow A^(X-N) Use famous O(lg) pow in groop of matrix __int64 in inner calculations and int (rez)%Y as result My such solution ran for 2.9 seconds with 3 for TL. So it's still not enogh for stable solution. I had to differentiate between M^2 and M*A matrix multiplications (the latter can be implemeted via int, +, - and >=), then it ran for 1.5 sec :) My such realisation works in 0.609. This time even without optimizations on +- for fast modulus, and not using fact that matrix is sparse in those cases (so multiplication can be done at O(N^2)) - it's 0.078 sec. With these optimizations - 0.046 sec. Edited by author 17.08.2022 11:56 This was overall a great problems set! Can anyone give a hint on the solution for "J. Turning Turtles"? What special property (beside that it's a tree) must one use? What is the complexity of the intended solution? Thanks in advance! This problem can be solved in O(N + Q*logN), where N is the size of the field. Am I right that we build slightly another tree and then use LCA on that? =) And when the problems will be added to the problemset? Actually it is possible to use the given tree. But calculating the answer is not very trivial. I was glad to use successfully LCA with forgotten implementation. It is enough to know what LCA do. It is mean that LCA very adequate tool for tree and cannot be omitted in learning. It is possible to use given tree, but my complexity is O(N*log(N)+Q*log(N)) - 0.25 sec and quite a bunch of RAM. For every node store its 1st, 2nd, 4th, 8th, 16th, ... 2^16th parent. Also store number of turns on that path and final orientation after last move (horizontal/vertical). This information is enough to calculate answers at log(N). 1. Use integer calculation for judging if the answer is -1 or 0. 2. Use integer calculation until the last step for the area (an integer-division). 3. Use 'long long' instead of 'long' or 'int'. Also, here are some cases which might help. 0 0 -1000 0 0 0 1000 1 1000001000.000000 -1000 0 0 1000 0 -1000 1000 -999 2002004004.004004 0 400 1000 1000 -1000 -1000 355 -187 -1 0 400 1000 1000 -1000 -1000 350 -190 0 0 400 1000 1000 -1000 -1000 353 -188 16931680400.000000 -1000 -1000 1000 1000 -1000 -1000 1000 999 31984004000.000000 -1000 -1000 1000 1000 -472 -851 379 1 5800420000.000000 More tricky cases: 0 0 3 0 1 0 1 0 0 The following testcase helps me to beat WA#33. 0 0 3 0 0 1 3 1 -1 Edited by author 27.11.2012 17:54 Edited by author 27.11.2012 17:54 Edited by author 27.11.2012 17:54 This test shouldn't be applied: 0 0 3 0 1 0 1 0 0 because it is said that window lengths are positive. And that's true, I've checked. Or maybe a misprint? -1000 0 0 1000 0 -1000 1000 -999 2002004004.004004 i get 2002004.0040040, but accepted. Same here, also another test is incorrect. All others are fine. -1000 0 0 1000 0 -1000 1000 -999 2002004.00400400 0 400 1000 1000 -1000 -1000 353 -188 16931680400.00000000 Since input numbers are integers, pseudovector product absolute value will be at least 1 if they aren't parallel, so you can compare it to 0.5 to have bigger margin for double precision error. Just get cross-product (dx1*dy2-dx2*dy1). If it's zero - they are parallel. When n>10 is a prime, i am writing n=k*3+l*2, where l=1,2 or 3 depending whether n mod 3 = 1 2 or 0. Then I get lcm=3^k*2^l, which is the maximal with respect to all possible divisions of n. However, my program gives WA6. Am I correct? Thanks! Clearly the lcm of k 3's and l 2's is at most 6 -- you are asked the lcm of them, not the product of them. Head-on solution have complexity O(2^40)=O(10^12). It's very slow. How do it faster? I used 40=30+20 First 20- memorization second 20 as 2^20=10000000- that normaly Could you explain your solution more in detail? "First 20- memorization" What it mean in Russian? I understood that what we do with second 20. But can not understood that what we do with first 20. =) I meant that first 20 also as 2^20 or with brute force aproach, but result of first 20 we place in arr[1..1000000](memorization) sort arr than in second 20 use log-bin search in arr. Thank you very much. You is good man! I got AC. =) It's a bit more complicated than that because you should care about rod not getting off the rails during the process as well, so you can't just pick arbitrary memorized sequence which gives suitable end-result, it also must stay within limits while it performs memorized shiftings. Looks like segment tree algo, not just plain binary search (or tests are very weak). P.S: Got AC with 0.5 sec and 44Mb, probably could've done better. For every 2^20 endings I track range of start values where it is suitable (i.e. fits for its entire internal history), then for every 2^20 beginnings (sorted) I track min/max of currently active segments via two heaps. Edited by author 16.08.2022 11:51 P.P.S: Ah, I'm dumb! Memorizing first 20 leads to much easier algo when last 20 are brute-forced (not vice versa). 0.265 sec, 7 Mb. Yet, coding that monster from previous "P.S" was fun :) Why is the correct answer in the example "uhhdud" and not "uhdudh"? in terms of lexicographic order, this combination is closer to the original data. It's not next in the book, it's before. d < h < u Let a[1], ... , a[n] be the sequence written by the friend and pref[i] be the xor of the prefix a[1...i]. Then a question (l, r, t) (which means the parity of the number of ones on the segment from l to r is t) can be represented as (pref[r] xor pref[l - 1] = t). Let's build a graph with two vertices for each prefix (thus, there will be 2 * (n + 1) vertices). Using compression, we actually need only at most 2 * m vertices (where m is the number of questions). Let the vertices corresponding to the prefix i be f0(i) and f1(i). Now, let's add all edges f0(i) ~ f1(i). For a question (pref[r] xor pref[l - 1] = t) if t = 1, we add two edges f0(l - 1) ~ f0(r) and f1(l - 1) ~ f1(r), else if t = 0, we add edges f0(l - 1) ~ f1(r) and f1(l - 1) ~ f0(r). To check whether a sequence of questions from 1 to x is achievable we just need to check whether our current graph is bipartite. This can be done by simply doing dfs after each question. Nice! Can you please elaborate in a few words? I actually didn't get the significance of having two vertices for each prefix and how it's solving the problem. Though O(N*log(N)) and O(N) both gave me 0.068 timing, it's a good testdata to write such code, would matter if N could be up to 10^6. Also this solution with O(1) stepping may additionally report number of ways to achieve minimal result. The structure is doubly-linked list of available amounts (always strictly ascending), each of which is non-empty doubly-linked list of characters with that amount. This structure allows to query for max and to change amount of single character +-1 at O(1). Could anyone who solved it share the idea? My solution was: iterate over all vertices i. Fix each i start bfs, so you get a tree (rooted in i). Find it's diameter (from the farthest node v from root i start another BFS and find the farthest node u from v. The distance from u to v would be the diameter). Across all the diameters find the minimum, that would be the answer. This solution got WA 8. After random_shuffle() on adjacency lists I've got WA 17. Now repeat this solution 100 times and you get AC. So my question is, what is the solution? 1. Find shortest paths from all vertices with Floyd-Warshall or BFSes 2. Brute force all possible tree centers. Note, that a diameter center can be either a vertex or an edge. Basically, my solution is exactly yours while I believe you've forgotten to check edges that can form a center. My solution is completely deterministic. Edited by author 07.11.2018 17:03 Edited by author 07.11.2018 17:03 I just tried all vertices and all edges (meaning pushing both neighbors in priority before BFS starts), then checked tree diameter across all vertices, not just to these roots (there is O(N) algo for that). I used no Floyd-Warshall for candidates, just tried all pairs v1 <= v2, besides I am not sure Floyd will help here because original graph diameter can be shorter than that of resulting tree (e.g. see WA17 topic). Use this test 6 9 5 6 3 4 2 4 1 5 3 6 1 2 1 6 4 5 Thanks, helped me to get AC, but first line in your test should be 6 8. Correct answer is diameter 3. att Graph is not oriented. Word "direct connection" in the statement means connection w/o intermediate nodes. hi, im trying to solve this problem and would like to get tips in the right direction of solving this problem. would a grapha matching solution work? or perhaps a minimum cost flow solution? or anything else? thanks in advance. My solution is to find really optimal matching with min-cost max-flow. Then compare it to one from input. I solved it with negative cycle searching. My algo have following complexity: (N+M)*(2*N*M + M*M) It can also be a chain ending up in shelter with residue capacity. It is also possible to make it N*M*M + M*M*M by jumping from shelter to shelter through one preselected building which grants maximum yield for that pair of shelters (hence the first N*M*M step), M*M*M is Bellman-Ford. I spent a lot of time on this problem. For those who want to fit the time limit I recommend reading A. Brandstƒadt, D. Kratsch "On partitions of permutations into increasing and decreasing subsequences". Also M.D. Atkinson "Permutations which are the union of an increasing and a decreasing subsequence" is very interesting to read but not so helpful for this particular problem. My algo is O(N) DP which does not rely on input array being a permutation. Input can be anything - even floats or strings and may have repeating elements. Moreover, it's "online" DP, so if you don't need detailed result (just 'Possible' or 'Fail') it can be coded in O(1) memory. -1000 0 0 1 1 My solution do not deal with testcases like this, yet still was accepted. Am I missing something? For those who wonder, my program gives 9079565065540428013 on this testcase lol Simple solution, but try to optimize it! If you want better explanation (myironmistake@gmail.com) Simple recursion from highest weight down to lowest with proper cut-off on maximal reachable weight and caching of results gives AC in 0.015 sec and 1Mb RAM. I write that spoiler here because I suspects that tests are very weak because I have no proof of why this straightforward thing works so fast when problem has 4-sec/256Mb limits. Example - my submission 9959592 (problem 1990 - Podracing). It's not about input or sorting (checked everything), but GCC gives 0.5 sec where VC++ gives 0.171 sec (it was 0.9 sec on the verge of time limit when everything was double). FPU is just a guess because I faced situations before in real projects when GCC used FPU and VC++ used SSE or something else and was way faster, there was some command-line switch like -fdisable-fpu or -fdisable-387 or something like that for gcc (can't remember now), probably it will help. The original version of the statement was unclear, and not all the limitations were described in the input data format. Both English and Russian version of the statement were updated. It's still unclear what to output if Lara has enough time to escape (i.e. there is no reason to hide) - empty output? My AC solution ignores that case like if entrance is locked. |
|