We will hold AtCoder Beginner Contest 179.

- Contest URL: https://atcoder.jp/contests/abc179
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20200919T2100&p1=248
- Duration: 100 minutes
- Number of Tasks: 6
- Writer: beet, kyopro_friends, satashun
- Rated range: ~ 1999

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

Front row!

"We are looking forward to your participation!"

We are looking for quick editorials.

P.S. I love ABC's.

same to you...looking for quick editorials

What are the ALC contests of Atcoder?

Edit: It's is described in this blog

(-_-)

No, it is me after solving ABCDE

Though it seems F was not particularly difficult this time

I tried a DP solution for D. Somehow getting TLE in 6 TCs. I tried to optimize it as much as I could. Am I missing something?

Link to Submission

post qns only after contest is over.

It is over.

U posted few minutes before it was over.

I could have posted the comment barely moments before the contest got over. But anyways, you can't open the submission link before the contest gets over anyways. Don't be a cop. If you can help then you are welcome.

direct DP is O (n^2). U need to use either prefix array or seg tree to solve in O(n logn)

I tried d with segment tree but in runtime error in some test cases. Can you help me? https://atcoder.jp/contests/abc179/submissions/16889464

i used fenwick tree to optimize it, note that k <= 10, your solution fails because ranges can be very big, so your solution is n^2

But the sum of ranges won't be more than O(n) I suppose. Because the segments are nonintersecting.

Yes, the sum of ranges is $$$O(n)$$$, but you also have $$$O(n)$$$ states and for each, you iterate through all of them. In total it is $$$O(n^2)$$$.

I used a presum array to find the sum of dp in previous ranges.

you can try it in O(n*k). Refer to below link for reference.

https://www.geeksforgeeks.org/constant-time-range-add-operation-array/

My Submission

your code runs in O(K*N) where K is the number of possible jumps, theoretically in worst case it would reach O(n^2) time complexity which could defenitely get TLE. In this problem i used dynamic programming with fenwick tree, you can look at my code here

For d, you have to combine the dp you are talking about, with the range covering problem (it is easier than segment tree I leave you this video in which that guy explains that problem https://youtu.be/Zze-O2oxoEo?t=219 ) and then you just have to be careful about modulo operations (you might be doing them with negatives)

I hope this was useful

Thanks for this. I checked out the video. Isn't what he is talking about, called the Fenwick tree?

No, Fenwick tree is a little more complex than that, it is about having sum until last significant bit and when doing a query going up from last significant bit to most significant bit (with a complexity of logn) and when updating is almost the same, but instead of getting an answer, updating; that is why it is also called Binary Indexed Tree

My code is similar with yours and I wonder how to optimize it too. [submission:#17148205]

Loved solving E !

How to solve ?

Its running a while loop until you face one among three situations,

i) when x becomes 0 (once x becomes 0, the following numbers will be 0 too)

ii) when count==n (it means you no more needed to continue evaluation)

and the third observation is probably the challenging one

iii) once you see any x repeating then come out of the loop, because you know the following numbers repeat too

i used the same thing can u plz tell whats wrong with my solution `string solve(){ ll n,x,m; cin>>n>>x>>m;

}`

First time in ABC I was able to solve all problems. Here are short explenations.

FUse two segment tree with lazy propagation to maintain the first blocking position per row/col. Submission

EThe sequence repeats after at most M elements. Find the size of the loop and the sum of the loop, then "jump" to n. Submission

DUse segment tree with lazy propagation to maintain the dp-array. This makes it possible to quickly update the intervals. Submission

CBrute force count the number of pairs $$$i*j<n$$$ Submission

B+ASubmission B Submission A

Funfact, I had no WA, all AC on first submission.

For d instead of segment tree + dp you can use prefix array + dp.

please share your code

Submission

plz give a brief explanation also. thnx

Basicdp[i] represntes number of ways to reach ith index.

suppose you can make jump of value a1,a2,a3.

then

`dp[i] = dp[i - a1] + dp[i - a2] + dp[i - a3]`

Solutionconsider a segment (L1,R1)

so

`dp[i] = dp[i - L1] + dp[i - (L1 + 1)] + ... + dp[i - R1]`

So for this, we can use the prefix array.

`dp[i] = prefix[i - L1] - prefix[i - R1 - 1]`

We have to repeat this process for all segments.

Submission

thnx a lot:) Helped a lot

You don't really need a segment tree for D

I just used prefix sums

please share your code

Your text to link here...

D and E should have been swapped ,I didn't even attempt E after continuous TLE's in D .

D is more difficult in terms of the idea I guess

E is heavier in terms of implementation

even i think so

You don't need segment tree for F either.

Codecan you explain your approach for F

F: you can use std::map for the same query (lower_bound to find closest) Submission

D: you can use only partial sums of your dp and fill dp[i] with sum of previous values Submission

Can you explain your approach for F?

I also thought of using segtree with lazy prop on F, but I had no segment tree template with lazy propagation :P and I had only 35mins left. So I quit. Missed my chance of solving all problems in ABC for the first time.

I'm taking your template now XD.

I also got idea of two lazy segment tree immediately but I haven't implemented even a basic lazy segment tree before so I gave up. Now I am seeing there are other solutions too

I had to fix a bug in that template in function rangeInc(). Not sure if everything works fine.

Oh,thanks for informing. I'll test it properly after customizing it for myself. :)

In AtCoders ACL library there is a segment tree, too. I would like to switch to that one, but need time to get used to it.

ACL lib

I got tle in this, could you plz tell me why?

You have asymptotic of $$$O(n \sqrt{n})$$$ which is too big. You can enumerate only the smallest number of $$$A, B$$$ which would be $$$\sqrt{n}$$$ and calculate how many multipliers bigger than your number you can have such that $$$A \times B$$$ is smaller than $$$N$$$.

n is 1e6, so the inner loop runs up to 1e3 times... that are aprox 5e8 iterations.

spookywooky — Love the way you add inspiring quotes to your code.

Link : https://atcoder.jp/contests/abc179/submissions/16871292

Can you please elaborate solution of D a little?

Me too for the first time solved set in any contest lol

every time i used to fight myself to solve at least 3. But u guys are solving all.

Can you please further explain how did segment tree help with this problem? I know segment tree but I cannot utilize it to solve this problem.

I assume problem D.

If we would do standard knapsack we would need to loop over all the values in the K ranges, which is O(n).

With the segment tree we are able to do these updates in O(log n).

Note that the solution with the partial sums do these updates in virtually O(1). see here

Thanks a lot:) you always provide useful insights

How to solve problem D?

Спойлерyou can just use range query datastructure like fenwick tree and update every index

I solved it without segment tree

I used difference array. Time complexity of $$$O(N*K)$$$

basically let

`dp[i]`

be no of ways to each i.Then you increment, $$$i$$$+$$$L_j$$$ to $$$i$$$+$$$R_j$$$ with

`dp[i]`

, increment using difference array method, and keep summing as you move toward right.The answer would be

`dp[n]`

Submission

Hello , I thought D problem is same as Coin Combinations I as expected get TLE for that.Anyway to improve my solution? My submission is here

please can you tell where am i doing wrong

I solved it maintaining a prefix sum array and using dp.

dp[i] = sum of dp[j] for all j, i is reachable. As the range was contiguous it was easy to get the sum of dp[j] using prefix sum.

My Sumbission

could u plz explain more

Here

`dp[i]`

is the number of ways to reach`i`

.`dp[i]`

was calculated by the sum of`dp[j]`

for all`j`

from where`i`

is reachable. Assume`i = 4`

and`i`

is reachable from 2 and 3. If the`number of ways`

to reach 2 is 1 aka`dp[2] = 1`

and the`number of ways`

to reach 3 is 2,`dp[3] = 2`

then`dp[4] = dp[2] + dp[3]`

which is`dp[4] = 3`

(rule or sum)And for any

`i`

corresponding`j`

s were calculated from the ranges and their sum was calculated from the the prefix sum array. For any position`i`

and a range L, R`i`

is reachable from all valid i-R, i-L.Finally the answer is

`dp[n]`

.Complexity O(N*K)

Tx a lot

I got runtime error in sometest cases in prob D with segtree can somebody help me? https://atcoder.jp/contests/abc179/submissions/16889464

what WA in my D https://atcoder.jp/contests/abc179/submissions/16888327[my sol link](http://https://atcoder.jp/contests/abc179/submissions/16888327)

they want you to union the segments [Li,Ri] i mean {Li,1+Li,2+Li,3+Li,....,Ri} not only values {Li,Ri}

** what is wrong with this code for the problem E **

It`s the first time I AK abc! LOL

I tried to solve D by repeating knapsack dp approach with time complexity O(N*m).But TLE destroyed my today's contest.Can anyone help me with that?

D can be done in O(n*k).

Use range update operation on array, it will take O(n) for each range.

My submission

Can you elaborate this part?

if(i+range[j].F<=n) temp[i+range[j].F]=(temp[i+range[j].F]+dp[i])%M; if(i+range[j].S+1<=n) temp[i+range[j].S+1]=(temp[i+range[j].S+1]-dp[i]+M)%M;

Suppose you have array 1 2 3 4 5 You want to add 2 from position 2 to 4 [1 based-indexing) You can take a temp array and make temp[2]=2 and temp[5]=-2

Now you can iterate over array and take sum+=temp[i]

And add sum to a[i] . You can get the required array after update in O(n) this way.

now I get it,thanks....

This is for C: Here we have simply calculate number of factors of (n-c) where 1<=c<n a*b + c=n =>a*b=n-c;

Eagerly waiting for geothermal's editorial.

Solution of C : https://ideone.com/gpZjgX

server not found :(

Hello in D no I used the similar pattern of dp like CSES-Coin Combinations I. but get TLE from that.Isn't it the similar pattern problem.

My solution is here

Thats' so because the time complexity of your solution is O(n*n).

I thought it's like Coin Combinations I problem.My bad.How can I solve it using dp ?

Well, usually i solve 4-5 problems in ABC contest but today i was able to solve only the first three!

Could anyone please explain the 1 testcase this is failing on? I have tried to find a cycle and then adding to the sum the sum of that cycle and then adding the rest seperately.Submission

nvm, fixed it, was not solving correctly for when the cycle would end without it ever repeating. fixed code

Your code here...`` ~~~~~

how to solve D? someone with good explanation?

You can use this dp: dp[i] means how many ways do you have to go to the i-th cell.

Let's get an array of long long dp[2n]. First we fill it with zeros. Denote the sums[2n] as prefix sums array of dp.

dp[n] = 1, sums[n] = 1

for each cell from n+1 to 2*n and each segment (l[j], r[j]) we calculate:

`dp[i] = (mod + dp[i] + sums[i - l[j]] - sums[i - r[j] - 1]) % mod`

You can understand that part as: to get how many ways I have to go to the i-th cell, I should sum the ways from all dp[the previous one, from which you can get into this].

The value

`sums[i - l[j]] - sums[i - r[j] - 1]`

gets us sum:`dp[i - r] + dp[i - r + 1] + ... + dp[i - l]`

Why are you taking dp[2*n] and not dp[n] or dp[n+1]?

Just because I don't want to care about bounds

Thanks I implemented it and got AC. if anyone wants to know what he means Here is my clear cut solution with more explanation. https://ide.geeksforgeeks.org/lpwZlbNN8D

My Unofficial Editorial.

And the Chinese version.

how do you solve c? I got TLE my solution is only O(n^2)

$$$O(N^2)$$$ is too large for $$$N=10^6$$$.

How can i know that it's large or not?

You just put N into the time complexity expression and evaluate it. Generally speaking, $$$10^7$$$ is totally acceptable, while $$$10^8$$$ can be a bit dangerous (on some OJs it cannot pass), and $$$10^9$$$ is almost impossible to pass the time limits.

However, sometimes we also need to consider the constant, which is omitted in the time complexity expression.

can somebody please explain the logic for c.

Since N is fixed, we don't need to enumerate all possible triplet of (A, B, C), we only need to enumerate (A, B) and compute C accordingly. Such method can be improved by enumerating only A and find the upperbound and lowerbound of B. All the values between lowerbound and upperbound are valid B. Note that B should be integer.

$$$0 \lt A \cdot B = N - C \lt N \implies 0 < B < N/A$$$

yeah.. got the logic thanks.

You can solve C in O(n) or O(nlogn) time complexity.Both of them will pass pretty convincingly.However the nlogn version would require a bit of precomputation You can refer to my O(nlogn) solution (I use Java)

https://atcoder.jp/contests/abc179/submissions/16892756

I believe F can be solved using monotonic set

Can any one help me figure out how to solve D using recursive dp? I do not quite understand the iterative dp solution.

I have written an unofficial English editorial.you can find it here.

UPD: Added editorial for problem F.

In the explanation for C : Could you please explain how the number of ways of choosing is N/A ?

I have tried to do it on paper but couldnot come up with the intution.

It should be $$$\frac{N - 1}{A}$$$ fixed it now, thanks for notifying me.

The reason for that is for every $$$A$$$ there can be $$$\frac{N}{A}$$$ numbers for be such that $$$ A \times B \le N$$$, however we should subtract $$$1$$$ because $$$C$$$ can't be equal to zero.

what is the range for c? is it 1<=c<=n-1?

For problem C i counted the number of factors of

`n-c`

In order to precompute number of factors for numbers in range [1,1e6]. I tweaked seive like this:But it may accidentally have worked for first two cases but didn't work for

`n=1e6`

(The last sample case) please help me find where did it went wrong?Thank You in advance

My solution was quite similar to yours.You can check my solution for reference(I use Java) https://atcoder.jp/contests/abc179/submissions/16892756

Can you link your submission for that problem

I couldn't debug this so i didn't submit :(

You can check my solution.

Nice solution. orz

Can you please explain the while(num>1) part in your solution?

Thank You

if num<=1 then the number has no prime factor.So as long as it greater than 1 the loop will run.

So you're using this formula am I right?

Let n = (p1^a1)*(p2^a2)*...(pn^an) where p1,p2,..,on are prime numbers And a1,a2,..,an are powers of primes Then number of factors of n = (a1+1)*(a2+1)*(a3+1)*...*(an+1)

Yes I am using this formula to count the number of distinct divisors and add to the count.

Yes Thank You very much for helping me.

I didn't want do it this way it's tedious to code and prone to error because I am bad at programming(I knew the formula but couldn't have implemented it the way you did it). So I tried to go around in order not to implement it and couldn't solve it.

Welcome.You can follow CodenCode NumberTheory lectures to get better at solving these type of problems.I am learning from there.

Thank You

UPD: Corrected ...at the time of init of seg tree i increased size by(n+2)

Problem D just failing one testcase...can anyone help me out ?? I applied segment tree in this code..

Editorial for D

Problem D was quite interesting.

Can E be solved with Matrix exponentiation??

I tried to solve E,but for some unkown reason,I got two RE.

Can you help me?

CodeI couldn't even understand D,

Please correct me if I am wrong in my Interpretation:

We are given K ranges. After union of all those segments, we will have distinct distances.

So we need to find number of ways to reach from 1 to N using these distances.

Please help me

Editorial D

How to solve in O(nlogn) when the transitions cannot be written as a sum of small number of segments?

Wondering the same thing, please let me know if you find the answer.

[ignore this, too much complex]

let $$$J$$$ be the set of possible jump lengths, now $$$ J = \cup[l_i,r_i] $$$

Now we can use generating function $$$ G $$$ to define that set.

$$$G = \sum_{i=0}^N c_i x^i $$$ where $$$ c_i = 1$$$ if $$${i \in J}$$$ else $$$c_i = 0$$$.

Now, number of ways to reach from $$$1$$$ to $$$N$$$ using exactly $$$k$$$ jumps = $$$[x^{N-1}] G^k$$$.

As there is no restriction of jumps,so we sum it over all possible $$$k$$$.

so, finally what we need is $$$[x^{N-1}]\sum_{k>=0} G^k = [x^{N-1}]\frac{1}{1-G}$$$

now coefficient of $$$ x^{N-1} $$$ in the polynomial $$$\frac{1}{1-G}$$$ can be easily calculated by calculating inverse of polynomial $$$ 1 - G $$$ restricted to max degree $$$N$$$.

Overall complexity is $$$O(N\log{}N)$$$

For calculating inverse of polynomial you can refer Operations on polynomials and series

Editorial F

Could anyone help me understand the approach. I am not able to get it. Thanks!

I am getting stuck in mod operations. Getting WA for few cases. Can someone take a look at the mod operation and let me know if you find the error.

Link to my submission: https://atcoder.jp/contests/abc179/submissions/16925676

Thanks!

ABC turning into AGC :/

How to apply binary search to D?

In problem C, why is this #b =(n-1)/2. Precisely, why we have to subtract 1?