We will hold Panasonic Programming Contest (AtCoder Beginner Contest 186).

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

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

We are looking forward to your participation!

Are there any plans to hold an ABC on Sunday as well? Considering next week is goodbye rng_58 and many would like to jump that 2000 barrier to participate in them.

If anyone is interested, I'm currently planning to do a post-contest stream (twitch.tv/AnandOza).

Uploaded to Youtube: https://www.youtube.com/watch?v=3njFJqbY5D0

Is it a hiring contest ?

Why don't you show start time like cf announcements? Or is there any reason you give just a link ?

This is primarily because the time may vary from place to place, as per their time zone. timeanddate.com URLs can help with directly showing time converted in the local timezone, thus saves the confusion and hassle of converting them manually.

I think only CF contests can have time-zone adjustable displays of event time. And to avoid the confusion harshitkumargupta mentioned, they are doing this.

I wish all event times can be correctly displayed in CF blogs soon tho.

What is special about this contest？

The contest is sponsored by Panasonic (https://na.panasonic.com/us) and there is hiring advertisement on Japanese version of the contest page.

very nice contest(especially problem D)

btw, how to solve F??

https://atcoder.jp/contests/abc186/submissions/18889820

it was sweep line basically, first assume line extending until it can for both $$$x$$$ and $$$y$$$ directions , now you must have noticed that you have done over count, so you have to remove that over count, so question reduces to given $$$n$$$ and $$$m$$$, horizontal and vertical lines respectively, find total number of intersections of them.

https://www.hackerearth.com/practice/math/geometry/line-intersection-using-bentley-ottmann-algorithm/tutorial/

You could read more about here, you could use any range sum data structure, to calculate intersection in current sweeper line. also update them.

`(k.x + s) % n = 0`

given, n, s, and k, how to find the smallest x which satisfies this equation? I didn't try brute force as it will give TLE for sure.

Even, if there was a brute force approach, it won't work for the sample test case (I tried for 10^7 iterations) and one of the testcases was giving the wrong answer.

Could you please show me that which sample test case you didn't get the right answer through a brute force approach?

What I did was to iterate from 1 to 10^8+ 7, it gave me answer -1, instead of 249561088 for 998244353 897581057 595591169. (correction, it was a silly bug :|). But still, I don't think it will work.

It won't because you need 148897793 approx(1.5 * 10^8)itterations and you are just doing 10^8+7. There is no constraint on no of iterations

This is what I did.

(k.x)%n=(-s)%n

Let g=gcd(k,n).

We know that (k.x)%n takes all (i*g)%n values.

If ((-s)%n)%g is non zero than answer is -1.

For other case just divide k, n, and (-s)%n by g.

Now the equation changes to (a*b)%m=c where gcd(b,m) is 1.

It has a unique soln for a in range [0,m). Its a=(inv(b)*c)%m. You can use extended gcd to find that inverse.

Yeah, modulo arithmetic does the work I guess.

this is better, I did it by finding $$$x0, y0$$$ using euclids algorithm, and then checking sign and adjusting. Actually $$$s < n$$$ so no need of modulo $$$n$$$.

According to cp-algorithm if we convert ax = b (mod m) to a/g x = b/g (mod m/g), this new equation's unique solution x' is not the only solution of original equation, there are exactly g solutions: x = x' + i m/g (mod m) for i in [0,g). How to prove that x' is the minimum solution?

I tried to implement that, but no success. Can you shortly tell which of the input numbers n,k,s are the variables a,b,n of cp-algorithm?

Not working codeCheck this AC submission with comments.

Thanks, I see that pow(a, m-2) does not work here :/

Yes, pow(a,m-2) comes from Fermat's little theorem (pow(a,m-1)%m=1) which works only if m is prime.

why is c = n — s instead of c = s (line 68) in the mentioned solution?

Takahashi is initially sitting on the chair that is S chairs away from the throne in the

clockwise direction.Move: Go to the chair that is K chairs away from the chair he is currently sitting on in the

clockwise direction.Draw some cases on paper and simulate if you are still unclear.

All those g solns have same value mod (m/g). Just take the one with i=0 which is returned in above soln.

Use extended euclid's algo to just find any solution to k.x-n.y=-s, then use shifting to find for finding minimum x>0

how to solve E,F?

E:https://cp-algorithms.com/algebra/linear_congruence_equation.html

Here is how I solved problem E.

So assume that position of the throne is at position n and we are at position s.

So to reach the throne following equation can be formed —

s + x*k = y*n where(x is the lowest positive integer)

So this equation can interpreted as ax + by = c which is standard linear dopamine equation.(There are many good resources for this on internet).

I was able to do until this point but can you tell how you solved this equation

So we are having this equation right x*(-k) + y*n = s

You can learn about solving such equation over here

Now after reading that we have all the possible values of x that are in form of x = x0 — m*(n/g), and we need to find the a value of m for which x is the lowest possible equation which can easily be solved by basic math.

Considering the process to calculate the greatest common divisor $$$d$$$ of $$$a$$$ and $$$b$$$. Let's try to get a pair $$$(x,y)$$$ such that $$$ax+by=d$$$. When $$$b=0$$$ , $$$d=a$$$ so that we can set $$$x=1$$$ and $$$y=0$$$. Assume that we have already known $$$bx+(a\%b)y=d$$$. Then $$$d=bx+(a-(a/b)*b)y=ay+b(x-(a/b))$$$ so we can change $$$x$$$ into $$$y$$$ and change $$$y$$$ into $$$x-(a/b)$$$ at the same time. Repeat the operation above so that you can get a pair $$$(x_0,y_0)$$$ such that $$$ax_0+by_0=d$$$. All of the pairs $$$(x,y)$$$ satisfy the condition if and only if $$$x=x_0+k(b/d)$$$ and $$$y=y_0+k(a/d)$$$ ($$$k$$$ can be any integer).

You can have a look at the following function to understand better.

I have a solution for F involving policy-based DS(indexed set).

First, you calculate the number of cells reachable by using steps

`down`

and then`right`

.Then you need to calculate (for every column) how many rows are there such that their minimum y is less than the current column number. Add that number to the answer. This can be done using a Policy-based DS.

Can you elaborate the part

Then you need to calculate (for every column) how many rows are there such that their minimum y is less than the current column number. Add that number to the answer?One solution for F is as follow :

Let us find 2 arrays, call them R[H], C[W] and initialize R[i] = {w+1}, and C[i] = {h+1} for each valid i. So, the information we store in R[i] = first column which has a block in ith row, similarly , C[i] = first row which has a block in ith column.

Now in first move we can go to anything from [1,R[1]] and [1,C[1]] and then we also have stored value, of how many blocks we can visit in front of each of them . So, we just add them up.

Are we considering all the blocks which can be visited ? Yes. But, we also need to now remove the blocks which can be visited from both paths, going right then down or going down and then right.

You can use pbds, segment tree or anything you like to answer these queries.

Code

did some one use BIT TREE TO solve F ? if yes share ur code plz

You can have a look at my code.

I tried using merge sort tree and policy based trees to but code failed for 15 test cases and they dont share test cases I don't know what is the error now shifting on BIT lets see the results.

I used segment tree with two operations: point update and (sum of range, number of zeroes in a range).

Link to code

How to solve C for larger constraints? Preferably,$$$N<=$$$ 10^18

we can use DIGIT-DP for modulo 8 and modulo 10 separately, but i am not sure about how to calculate intersection of both the sets.

This soln is wrongLet D be the set of lucky nos in [0,40). Then a no is lucky if and only if its digits in base 40 belong to set D. Find all the lucky nos in [0,40) and use these as digits in base 40 to find answers upto 1e18.

How to solve E? I was able to deduce that if throne positon is not divible by gcd of n and k answer is -1. But i couldnt figure out final answer without doing brute force.

I think you have transfromed the problem into get the minimal positive integer $$$y$$$ that $$$N \cdot x + K \cdot y = N - S \text{ mod } N$$$ stands.

The next step of my solution is to use EXGCD to get the exact value of $$$y$$$, which is a classic use of EXGCD.

you can refer to Linear Diophantine Equation or 线性同余方程.

Thanks a lot!

Has any of you tried persistent segment tree for the last problem? Thanks! I want to know how my solution went wrong: https://ideone.com/AH50pe

Actually, I solved it. The correct solution is here: https://ideone.com/SH52qH

Could someone please explain an efficient solution for D? My (brute force) attempt timed out, but I'm unable to figure out the reasoning behind other contestants' accepted answers.

This is a really good contest. Although I didn't do as well as I want,(I got WA 3 times during the contest) it shows me that I have something unfamiliar with and I must be more careful to make my solutions correct.

At the same time, I won't be able to join in AtCoder Grand Contest 050 (Good Bye rng_58) as a rated contest. So I hope that there is going to be another contest before the AGC contest for people like me.

So what's the editorial of F?

I think about it for a long time, but just can't solve it.

Maybe you can have a look at my code first, I am trying to write the editorial for F now.

First, calculate $$$a[i]$$$ which means the number of squares in the $$$i$$$-th row that you can reach (from $$$(i,0)$$$ in one move) and $$$b[i]$$$ for the $$$i$$$-th column, respectively.

Set $$$c=a[1]$$$ and $$$d=b[1]$$$, so that you should only consider the first $$$d$$$ rows and the first $$$c$$$ columns. The only problem we need to solve now is to calculate the number of sqaures $$$(x,y)$$$ such that $$$1 \le x \le d$$$, $$$1 \le y \le c$$$, $$$y \le a[x]$$$ and $$$x \le b[y]$$$.

We solve this problem in the ascending order of $$$b$$$. When we consider $$$i$$$, we need to put the first $$$b[i]$$$ elements of $$$a$$$ in BIT so that we can only calculate the number of elements which are not less than $$$i$$$ in BIT, which is called $$$p[i]$$$.

Finally, the answer is $$$\sum_{i=1}^c(b[i]-p[i])+\sum_{i=1}^da[i]$$$.

thx！

Problem E

We start at position S, and need to count the number of steps to get to position 0. One step goes K positions. So

$$$x\cdot k=n-s$$$ |mod n

Implementing this according to https://cp-algorithms.com/algebra/linear_congruence_equation.html produces nonsense results for me.

Can somebody explain?

Code, not workingEdit: I am aware that there are basically 3 possible solutions (maybe some more): Linear Congruence Equation which is based on the inverse. The Diophantene equations which are based on the extended euclediean algo. And we can also solve this using the chinese remainder theorem (with only one equation).

While I am not aware of the Linear Congruence Equation method — I am reasonably sure it should be

`if(b%g!=0)`

in your`lce`

method.Thanks, that was wrong in the code.

But the more basic error is stated in the editorial know: The inverse modulo M can be calculated with extended Euclidean algorithm. (Note that $$$A^{M-2}$$$ is not necessarily the inverse of A if M is not a prime)

So I did wrong in calculating the inverse always with pow(a, m-2).

Can you plz confirm that

`(b%g != 0)`

comes from the fact that for any linear congruence`a = b(modm)`

this must satisfy -> m | (a-b). As we have already taken the gcd of a and m. So, remaining b must be divisible by m in order the satisfy the condition? Correct me if i am wrong! And one more thing if gcd is not 1 then, inverse shouldn't exist. But here we are still checking the condition`(b%g != 0)`

and after that we are finding the inverse. CODEYes, your reasoning for the

`(b%g != 0)`

condition is correct. Regarding the second part of your comment, spend some time reading https://cp-algorithms.com/algebra/linear_congruence_equation.html — it describes how you can get a new congruence equation by dividing both sides by the GCD (dividing by GCD won't work if`b%g!=0`

and in that case no solution)thanx!

(b%g!=0) is one, also i was getting garbage values until i realised that modular exponentiation can only be used to find inverse if the given "mod" is a prime. Else it'll give wrong answers. i applied the same to your code and it gets right answers.I also changed MOD=n just in case.

AC

Video Editorial

Source code and simple explanation

thanks!!

can someone suggest me the problem simillar to E for practice?

Problem E was kinda similar to this codechef problem CVDRUN. Although constraints are smaller on this.

For problem E, In the editorial, its mentioned the answer is (Ainverse * B) for AX ≡ B mod M .. suppose if A = 3 , B = 6 , M = 10.. then (Ainverse * B) = 7*6 = 42 which satisfies the equation but isn't the minimum X , the minimum X is 2. How can we find the minimum X ?

You should take the product modulo M as well, and $$$42 \equiv 2 \pmod {10}$$$.

Problem F : why wrong answer https://atcoder.jp/contests/abc186/submissions/18948383

I get it https://atcoder.jp/contests/abc186/submissions/18959209 thanks

Problem D is explained in gfg

[user:kyopro_friends], chokudai, Kmcode

hey,can you please add the test cases of this contest, I looked for it in https://www.dropbox.com/sh/arnpe0ef5wds8cv/AAAk_SECQ2Nc6SVGii3rHX6Fa?dl=0 but there isn't for this contest, there is some Panasonic 2020 but that contest isn't this one.

Thanks.

Never mind ,I found the bug