We will hold AtCoder Beginner Contest 177.
- Contest URL: https://atcoder.jp/contests/abc177
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20200829T2100&p1=248
- Duration: 100 minutes
- Number of Tasks: 6
- Writer: kyopro_friends, YoshikaMiyafuji
- Rated range: ~ 1999
The point values will be 100-200-300-400-500-600.
We are looking forward to your participation!
Atcoder beginner at 5:30, codechef lunchtime at 7:30, tomorrow cf Div 2 666. Good happy weekends for cp lovers :>
:)
Could you please suggest some good
cp website
to practice. I foundbinarysearch.io
quite interesting. Please suggest some. ThanksAlso worst weekend for getting brutally killed when problems are tough ..
I think atcoder admins are not at all interested in begineer contest now-a-days.
problems are also repeating more from some time. why?
ABC-175 was really good especially problems D and E but ABC-174 was really bad.
How to solve problem F?
Store an array $$$a$$$ where $$$a[i]$$$ denotes the minimum number of rightwards moves to get to a column $$$i$$$ at row $$$r$$$. We can simply add the fixed number of downward moves at the end. We update this at each row when we go from row $$$r$$$ to $$$r+1$$$. So in the beginning it is $$$a = {0, 0, \dots, 0}$$$. Note that when we try to move to the next row, and are forbidden from going downwards from columns $$$[L, R]$$$, we have that for $$$i\in [L, R]$$$, $$$a[i] = a[L-1] + i - (L - 1)$$$. The rest of the values in $$$a$$$ stay the same. If $$$L = 0$$$, then $$$a[i] = INF$$$ for $$$i\in [L, R]$$$, where $$$INF$$$ denotes impossibility to get to that location, formally stored as a large number. We can store the array as blocks of consecutive numbers. For example, the array $$${1, 2, 3, 2, 3, 6}$$$ could be compressed into $$${ ((0, 2), 1), ((3, 4), 2), ((5, 5), 6)}$$$, where these denote an interval and the starting number of the block of consecutive numbers. Then updates can be done in $$$\mathcal O(\log W)$$$ using a set. We then simply query for the minimum value of any block of consecutive numbers, and add the current row number for the vertical moves. If this is $$$INF$$$, then we know we can't get to this row. Overall, this runs in $$$\mathcal O(H\log W + W\log W)$$$.
Submission
Can you elaborate on the blocks of consecutive numbers and the compression of arrays?
To actually do this, I use an interval union data structure (not an official name, just the name of my template). What it does is it stores a bunch of intervals (currently, it doesn't handle repeated intervals, but it's not hard to change to multiset to handle it). You can then query the intervals that intersect the a given interval in the data structure in $$$\mathcal O(X + \log I)$$$, where $$$I$$$ is the number of intervals in the structure and $$$X$$$ is the number of intervals intersected. You can also add and remove intervals in $$$\mathcal O(\log I)$$$ time. So to change a block of intervals, I simply query the intervals intersected by $$$[L, R]$$$, and remove them and update the endpoints. See my code for more details on implementation.
If you want other problems that can use something like an interval union data structure, see Facebook Hacker Cup 2020 Round 1 problems A2 and A3, although they are a little terrible to implement.
How to solve F?
It is segment tree with a clever range update. I was not able to make a working implementation, but others did.
It seems that it involves some trick!, I will try solving using segment trees hint! Thanks!
Exactly
What is wrong with my solution for E?
const int N = 1e3+1;
I think N shall be 1e6 but you chose 1e3.
My idea was to check if a prime doesn't divide a number from the array more than once.Since, any composite number > 1e3 must be divisible by a prime less than 1e3, I kept N to be 1e3+1.A prime > 1e3 will be alone and won't divide any other element from the array anyway.
What if all the numbers are same prime number >1e3.
DAMN!! Thank you.
How to solve D ?
size of the largest connected component
need to used bfs or dfs ?
both will work.
i used a dsu
create a graph and find the size of largest connected component
Oh shit..thanks everybody
We can use DSU to find size of biggest component, which is ans.
Size-based merging of dsu. https://atcoder.jp/contests/abc177/submissions/16334890
This approach is $$$O(n \alpha (n))$$$.
Main observation is that if in a group, all of them are friends with each other then it is must to distribute all of them in different teams. So You have to find such group of the maximum size.
I learnt DFS a few days ago. So today I solved this problem with a simple DFS application.
Participated in Beginner contest for the first time. Missed the announcement, started 40 minutes late, but managed to solve 3 problems. Could you guys post the announcement 24 hours earlier, if that's not too inconvenient for you?
How to do C? I tried it using prefix sums and got right answers for sample inputs. Still WA.
Suffix sum would be a better option.
Doesn't really matter if used correctly
yeah I tried both. Same answers
Have you put the mod(10^9+7) correctly everywhere?
Yeah
can u elaborate more about your approach??
For every i, I added (a[i] * (pref[n-1]-pref[i])%mod)%mod to the answer(pref is the prefix sum)
pref[i] %= mod for every i maybe
and (pref[n — 1] — pref[i] + mod) % mod
Maybe .. a common mistake in such in incorrect implementatation of modulos ? Link you code maybe ?
(a + b + c)^2 = (a^2 + b^2 + c^2) + 2 * (ab + bc + ca) You have to find (ab + bc + ca).
.
You can find it here.
Compute the times all integers within the given range occurred as a prime factor, then
pairwise coprime
,setwise coprime
,not coprime
https://atcoder.jp/contests/abc177/submissions/16343274
Even if one prime factor occurs more than once, it enough to say it will be either setwise or not distinct depending upon the whole gcd.
It is enough to create the set of primefactors foreach a[i] and check if any primefactor is in any others a[j] set.
Additionally calculate the gcd of all a[i].
submission
I used the same approach but it failed.Can you please tell me what's wrong with my code? https://atcoder.jp/contests/abc177/submissions/16376967
You do not create a prime factorization, instead list all divisors. But I do not see how that could make it notwork. I dont know.
[user:spookywooky]Can you point out what's wrong in my code ?
https://atcoder.jp/contests/abc177/submissions/16766579
I am not sure if I understand your code. It seems that you do not divide all 2 in the first loop of the factorization.
There is an flag 'ok'. What is this good for?
That is for counting each prime factor in a given number only once
Ah, ok.
You flip flag^=1 whenever you find a double. But if you find two doubles, you flip it back, that is not good.
Instead just set the flag to 1 if an error is found.
This code passed from 25 test and failed in only one test.
Can you tell me what's this test""
Sorry, I dont know.
logic
* notprime if gcd(a1,a2,---,an)>1
* setwise prime if any two elements ai,aj have a common divisor, for this just memories the divisors which we have encountered
* other wire pairwise prime
spookywooky What will be the complexity of your solution?
Yes.
but what if all numbers are prime then the fact function will have to check till $$$\sqrt(N)$$$ for all numbers ?
Dude, I changed my answer, previously i thought he was storing the smallest prime factor for each number using Sieve, but i was mistaken, you're right. Though most optimized solution of this problem is of complexity Nlog2(N)
then why his code is not giving TLE verdict?
Because, N and MAXN ranges are 10^6, now to bottleneck the solution you have to find 10^6 such numbers which are distinct because as soon as it will encounter any two numbers which happen to be the same or have gcd > 1, it will terminate instantly and thus, code never reached 10^9 iterations. 1 sec of C++ can accommodate about 2 * 10^8 iterations which i also suppose is very hard to reach, which by the ac time you can see its even less than half of them.
Ok got it so the method of implementation is optimizing the solution because as soon as we are factorizing we are checking if the prime factors already exist.
approach for E :
for setwise coprime : i used the recursive property of gcd
for pairwise coprime : is it possible to make a set(size > 1) from given numbers s.t if we take any two element from the set whose gcd is atleast x.
if it is not possible for x>1 then paiwise coprime
submision
For problem E can someone tell me what is wrong in my code
I got WA for 5 test cases
I guess interger overflow for large testcases in ans*=x line!
Ok, Thank you
Solution of problem E(Coprime) :
Firstly we will calculate the smallest prime factor of all the numbers(due to the given constraints).
Secondly, we will store the different prime numbers in the prime factorization of a number in a set for each number in the array. We will do this for all the elements.
Lastly we will iterate over the different prime numbers of the prime factorization of all the numbers in the array, and maintain an hash array for the prime numbers found.
If we don't find any prime number which occurs in more than 1 number, then our ans will be pairwise co-prime, else, if the gcd of all numbers is 1 then the ans is setwise coprime otherwise it is not coprime
If anything is not clear, please ask.
And if anyone got a solution with better complexity, do share it.
can you please provide link to your submission.
It's in a very bad shape now Link, but I will make a new submission which will be a clear.
UPD: Here it is.
Can someone tell me what is wrong with my submission for problem C?
https://atcoder.jp/contests/abc177/submissions/16372833
(vec[i]*pre[i-1]%MOD
will lead to overflow. Replace it with((vec[i]%MOD)*(pre[i-1]%MOD))%MOD
.try when store pre array also use mod
Can anyone please tell me the what is the wrong in my code. here is my solution
size of visited and adj it should be 2*10^5+1
How to solve F?
I used Segment tree + BS, so complexity is nlog^2n.
Link to solution
Why are ABCs always unbalanced, how can mid-level participants use atcoder to improve?
Ya there shall be more ARCs!
They just could have used some tougher E. I took some time at B, then got a WA in E and that costed me.
Rarely are some atcoders good for practice. Mostly are easy till E then out of the world F. After giving contests on codechef, atcoder and codeforces, I think only codeforces is the one which could help us improve, there is always something to upsolve.
Well, you forgot the old atcoder problems. There are a lot of interesting E's and F's there including this F to solve. Atcoder problems are better in my opinion but they are making unbalanced rounds recently.
Hi, someone can help me with problem E?
for pairwise prime , you can store the smallest prime factor of a number using sieve. which helps to compute prime factors of the number in log(n) time. Then use a hashmap to check if any two number share any prime factors, if they do the set can't be pairwise coprime. For setwise co prime we just take the gcd while iterating the whole array. Here's my code ~~~~~ https://atcoder.jp/contests/abc177/submissions/163592881. — ~~~~~
https://atcoder.jp/contests/abc177/submissions/16359288
can someone explain the question F?
After each of the H rows we have foreach starting position a "shift" to right if we start at that position. The starting position with the min shift is optimal to start (because it produces the shortest path).
So we need to calculate/maintain these shifts efficiently. This is possible with a segment tree and lazy range updates, but the implementation is tricky. Because every position is not simply updated. The Update-Function is something like:
newVal=max(oldVal, position-b[i])
if anyone is getting WA on just one test case in problem E ..try this:
3
1 1 1
answer should be "pairwise coprime"
Why is this approach for problem C wrong??????
You have done a mistake. Consider sum1 is 1e9+8 we are at an array element 2 . Now you take addition of 2%(1e9+7) and sum1 becomes 1e9+10. Same way you have done mistakes with sum and total.
If anyone need short explanation & sample implementation from A-E Here
Can anyone explain this in problem $$$F$$$. I didn't understand this — $$$you \,cannot\, move \,down \,from \,the$$$ $$$A_i th, (A_i+1)th,...B_i th$$$ $$$squares\, from \,the \,left \,in \,the$$$ $$$i^{th}$$$ $$$row \,from \,the \,top$$$
In first row you cannot go down at positions in interval (a[1],b[1]), in second row you cannot go down at positions (a[2],b[2]) etc.
I think 'at' should be replace with 'from'. But still then what does this remaining part signifies $$$"from\,the\,left\,in\,the \, i^{th} \,row\,from\,the\,top"$$$. Because what you are saying is clear without this part also. Does this part trying to say something extra?
Well, it is the A[i]th col from the left, not from the right.
Can someone tell me why answer of C is wrong in some cases when mod=1e9+7(float) but 10**9+7(int) is right. Thank you!
Floating point operations may cause precision errors.
Is it true that set of distinct integers {a,b,⋯z} is pairwise coprime if its product is equal to its least common multiple? can we solve problem E using this?
you can't use lcm bcz if all numbers are prime, you will end up the lcm to be greater than the range of int as well as long long.
lol during the contest on E, I was printing "set coprime" instead of "setwise coprime" and I wasn't able to debug it.
That's why I copy-paste the magical strings from the problem statements. Too easy to make a mistake
Personal editorial for this contest:
Could you explain what does the "lo" and "lh" means in the code of problem F?
Could you please explain update [L,R] to f(i)=f(L-1)+i-L+1(since we must go right from L−1)
You can take it as a DP solution, consider dp[i][j] means: the minimum moves to reach the position (i,j). So dp[1][j] is always 0.
Now, suppose there is an example with W = 9 and the first row is like:
O,O,O,X,X,X,X,O,O
(X for blocked grid)
Then, you can see that the dp[2][-] should be:
1,1,1,2,3,4,5,1,1
Based on the observation above,we can use formula to conclude it:
a. if (i-1,j) isn't blocked, then dp[i][j] = dp[i-1][j]+1.
b. if (i-1,j) is blocked, then dp[i][j] = dp[i-1][x]+1+(j-x), where x is the maximum number (rightmost) with (i-1,x) isn't blocked. The formula "f(i)=f(L-1)+i-L+1(since we must go right from L−1)" you mentioned is about this one.
You may see the example above for better understanding.
Now, the question is how to optimize this solution and it's natural to think of segment tree and that's what I don't understand lol
Your explanation is great.
"lo" means minimum, "lh" means the current value of the left endpoint.
Got it! thx
By the way, the lazy tag for segment addition is actually unnecessary since we can just add the current row to answer lol
Wow. It was helpful. I bookmarked that site.
I have added some comments to the code of Problem F, hope they will help.
Can you explain how to make the second update and find minimum then?
We store the value of the left endpoint and the minimum in our segment tree nodes.
For the second update, we just update the left endpoint according to the value of $$$l-1$$$, and when pushing down, we can just use the information stored in the parent node. Also, since the value will increase from left to right, the minimum will be set to the value of the left endpoint after the update.
For the range minimum query, we just choose the minimum of left child and right child.
Thanks. Got it
For E, why
if(n>80000) {cout<<"setwise coprime\n";return 0;}
is not correct? In my view,$$$80,000$$$ is larger than $$$\pi(1000000)=78,498$$$ ,so there must exist $$$(i,j)$$$ which have same prime factor. my submittionElements can repeat.
if repeated then the ans is
setwise coprime
wasn't it?what if the array is all 1s?
Right,thanks!
I got it ! All of them could be $$$1$$$,thanks
plz explain correct logic behind F or give a link of easy to understand submission
my WA submission
EDIT-> it is wrong because we can start from any column
Logic-> for every row we need to find out the lowest column we can visit
Just a little thing here .. why have they stopped tagging this blog for each contest on the atcoder page for few time now ?
Hey! Can Someone help me figure out what am doing wrong here, getting WA on a few testcases.
Thanks!
Try the case with all elements as 1.
Can some tell whats wrong with my submission? Was not able to pass 2 tests:(
https://atcoder.jp/contests/abc177/submissions/16373270
The logic is to check setwise coprime, just find gcd of whole array and check if it 1. For pairwise i counted the number of numbers that are divisible by some number. If for any number, there are more than 1 numbers that are divisible by it, its not pairwise coprime.
int main() { optimize
}
Please.
Try the case with all elements as 1.
Its giving correctly, pairwise coprime.
Ok your mistake is like if we have a prime number. Let's say 13. You are checking it till sqrt(13). 13 as a prime isn't being counted. Like if we have two elements 13 and 65. we have a common factor 13 between them . But your code won't show. If you want, I can help with correcting the code further.
logic is correct but implimentation is wrong.
Can someone tell me what is wrong in my approach for E. Link
For input
your program outputs "setwise coprime", while it should be "pairwise coprime".
thank u.... :)
For problem C, we can write $$$\sum_{i =1} ^{N - 1} \sum_{j = i+ 1}^{N} A_iA_j = \frac{((\sum_{i = 1}^{N}A_i)^2 - \sum_{i = 1}^{N}A_i^2)}{2}$$$. I used this, but got WA.What is wrong with my solution?
In the end of your solution you are dividing the answer by 2, but you can't do it by simply using operator "/" in modular arithmetic, you can read an article about it here. Also, I would say that your solution is too complicated, to get the answer you just need to calculate how many times will every number in the array be added, you can do that using prefix sums, see this solution.
Your formula is fine, but remember that you work with MOD, so you can't just divide your answer by 2. If you'll take modular multiplicative inverse, you'll get AC. Your modified solution: link.
For example: you should print answer $$$\frac{200}{2} \mod 109$$$. You're taking $$$\frac{200 \mod 109}{2} = 45$$$, that's wrong. Correct solution is: $$$200 * 2^{-1} \mod 109 \equiv 200 * 55 \mod 109 = 100$$$.
extra caution: in the example above, $$$gcd(200, 109) = 1$$$
if $$$gcd(200, mod) \neq 1$$$ then you can't use inverse multiplication for division
Yeah, sure, that's why all MOD's are prime: $$$10^9 + 7, 998244353$$$ and so on. That's important note, thanks!
In problem E I checked the condition of pair-wise coprime by using the fact that if $$$a_{1} a_{2} a_{3} ..... a_{n}$$$ are pair-wise coprime then their product = their LCM and for setwise coprime I checked that gcd over all numbers should be 1.My code is failing on some test cases.my submission
lcm grows very quickly so you can't store it in even long long.
Consider 2 primes very close to 10^6 their lcm can be as large as 10^12. So if we have n distinct primes lcm can be 10^(6*n) although that's a very light upper bound value of distinct prime also decrease but still you get the idea you can't store lcm.
Is there any possible way to incorporate modulo operator with the lcm finding?
Your LCM will overflow, and I think your solution might not be logically correct. Let's say the lcm of the array comes out to be
x
and the product the array comes out to beMod + x
then in this case your solution will outputpairwise coprime
whereas the correct output could be any of the 3 outputs. For example let the elements of the array be[Mod, Mod, Mod, Mod]
then thelcm = Mod
&product = Mod ^ 4
, this will lead to the answernot coprime
but your solution will givepairwise coprime
. This example does not fit into the constraints but there will surely be one such testcase that will fit.Can someone hack my solution for problem E because I think there are weak-test cases. Here my another solution get accepted even though this fails for this simple testcase
[1, 1, 6, 1]
.Share a solution without segtree for problem F.
The main idea : use a set to store the nearest place in the row i which can be reached from column "j".Suppose it's d[j] for the j-th column,and use another set to store "d[j]-j" for finding the smallest "d[j]-j".
Either my English is poor or the statement of problem F is not clear. Please help me to understand restricted moves.
For all $$$(i, j)$$$ such that $$$1 \leq i \leq H$$$ and $$$A_i \leq j \leq B_i$$$, it is prohibited to make moves of $$$(i, j) \to (i+1, j)$$$.
Can anyone please explain why I am getting TLE in problem C?
You used 2 nested for loops, so you exceeded the time limit. Find a better approach with a better time complexity.
It's a basic thing to notice you can't run 2 nested for loops like that. Do you know how to estimate time complexity?
I think the max size of the connected component is the answer.Is this concept wrong?What's wrong with my code?Can anyone teach me to adjust?thanks you very much. code is below (by python3)
x, y = par[x], par[y]
in 2nd line ofunion
Thank you very very very much,I get accept now~~~.
for this problem : https://atcoder.jp/contests/abc177/tasks/abc177_c
why this code get AC : https://atcoder.jp/contests/abc177/submissions/34638882
and this code get WA : https://atcoder.jp/contests/abc177/submissions/34639087
Although they are same ?
Add 2 characters to WA code and it passes: https://atcoder.jp/contests/abc177/submissions/34640706
The issue is $$$x = m[n-1]-m[i]$$$ might be a negative value if you apply $$$m[i]$$$ mod $$$d$$$(which isn't necessary to do so in this case). In some languages like C++, modding negative values is a bit weird, so you make it non-negative by adding $$$d$$$ to $$$x$$$ (as $$$|m[n-1]-m[i]|<d$$$ in the WA code) before modding by $$$d$$$.
in problem E
this code passed from 25 test and failed in only one test
can someone explain what's the wrong ?
Corrected here.
I debug your code in order to improve my debugging ability. You miss a case where there is no prime at all, i.e., all $$$a[i]=1$$$. So if(mx == 1) should be if(mx <= 1).
Besides, potential integer multiplication overflow exists in your code.