divyamsingal01's blog

By divyamsingal01, 13 months ago, In English

So I did not find a tutorial for Maths section of CSES on codeforces, so thought of writing one.

I have put all my codes on https://github.com/div5252/CSES-Problem-Set.

This tutorial covers 29 questions of Mathematics Section of CSES.

1. Josephus Queries

Tutorial
Code

2. Exponentiation

Tutorial
Code

3. Exponentiation II

Tutorial
Code

4. Counting Divisors

Tutorial
Code

5. Common Divisors

Tutorial
Code

6. Sum of divisors

Tutorial
Code

7. Divisor Analysis

Tutorial
Code

8. Prime Multiples

Tutorial
Code

9. Counting Coprime Pairs

Tutorial
Code

10. Binomial Coefficients

Tutorial
Code

11. Creating Strings II

Tutorial
Code

12. Distributing Apples

Tutorial
Code

13. Christmas Party

Tutorial
Code

14. Bracket Sequences I

Tutorial
Code

15. Bracket Sequences II

Tutorial
Code

16. Counting Necklaces

Tutorial
Code

17. Counting Grids

18. Fibonacci Numbers

Tutorial
Code

19. Throwing Dice

Tutorial
Code

20. Graph Paths I

Tutorial
Code

21. Graph Paths II

Tutorial
Code

22. Dice Probability

Tutorial
Code

23. Moving Robots

Tutorial
Code

24. Candy Lottery

Tutorial
Code

25. Inversion Probability

Tutorial
Code

26. Stick Game

Tutorial
Code

27. Nim Game I

Tutorial
Code

28. Nim Game II

Tutorial
Code

29. Stair Game

Tutorial
Code

30. Grundy's Game

Tutorial
Code

31. Another Game

UPD: I have also added tutorials for the newly added problems in Maths section. I am yet to do two problems — Counting Grids and Another Game. It would be helpful if someone could come up with tutorials for these.

 
 
 
 
  • Vote: I like it
  • +92
  • Vote: I do not like it

»
13 months ago, # |
  Vote: I like it +5 Vote: I do not like it

Thanks man...

»
13 months ago, # |
  Vote: I like it +5 Vote: I do not like it

Thanks a lot!

»
13 months ago, # |
  Vote: I like it +3 Vote: I do not like it

UPD: The tutorial is now complete. I have put the explanations and codes for all 21 problems.

If you find any typo or any improvement in the explanation, please comment below.

I haven't put the complete code(template portion) in the tutorial above, so as to prevent people for directly copying. Still if you wish to see the complete code, I have to the link to my github which contains AC solutions.

»
13 months ago, # |
  Vote: I like it +1 Vote: I do not like it

I still do not get sum of divisors :(

»
13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

THanks bro!

»
13 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

divyamsingal01 can you share the links to the editorials of other sections if you have found them?

»
13 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

can you please verify if i understood moving robots correctly —

for each robot we first get what is the probability for it to be on some i, j cell after k iterations.

then for each cell what is the probability that it was empty after k iterns? — probability that robot 1 isnt there * probability that robot 2 isnt there and so on.... so we multiply the 1 — dp[i1][j1]

now expectation — sum(P(xi)*xi) we calculated P(xi) and xi is 1?

»
13 months ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

For 21, here's a fairly straightforward proof:

This game is essentially equivalent to Nim with a restricted number of additions permitted, with the piles being on the even indices, and the permitted additions being precisely the ones which can be done due to the shift from the pile on the immediate right (if it exists), with the only other possible move being removal of one element from an even pile at most a finite number of times, and thus this game is equivalent to vanilla Nim (proof here: https://cp-algorithms.com/game_theory/sprague-grundy-nim.html#toc-tgt-5), from where it is obvious.

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

divyamsingal01 can you pleaes elaborate on second problem Exponentiation II. I don't know much about Fermat's theorem.

  • »
    »
    12 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    fermat's theorm says that if a,m are coprime and m is prime. then pow(a,m-1)%m = 1.

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Thankyou so much for the contribution

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

dardev cdes graph and yours math are the best!!

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Would you care to explain the question Exponentiation II again? I tried to solve it using simple Binary exponentiation but got WA. I see you're using Fermat there what is the reason behind it. can we not solve it using simple Binary Exponentiation?

»
11 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

divyamsingal01

In Graph Paths II, how did you decide the value of INF?

I took INF=7e18 and it was giving WA, but on changing it to 4e18, it got Accepted!

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You might be having long long overflow due to 7e18. Initialize, it sufficiently large so that it serves its purpose and encounters no overflow scenario.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Where can I find the proof of the recurrence relation in the "Throwing dice" editorial?

»
9 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

some additional explanation for Q-2 ,

so we have a ^ b ^ c

also note that m is a prime !

from Fermat's little theorem we know a^ m-1 = 1 mod(m) for every prime m

b^c = q*(m-1) + r where q and r are quotients and remainder resp ,

a^(b^c) --> a^(q*(m-1)+r) --> a^q(m-1) * a^r = (1^q) * (a^r) mod(m)

so our ans for a^b^c = a^r(mod m) where b^c = r mod(m-1)
Hope it helps :-)

»
9 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Mathematical Proof for Q21. It is a Staircase Nim Problem and can be easily proven that only even position value affects our answer. For the proof click here

»
7 months ago, # |
  Vote: I like it +6 Vote: I do not like it

Counting Necklaces How to get the inclusion/exclusion right?

Can sombody explain with example n=12?

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks a lot!

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

anyone solved grundy's game ?

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Several weeks age CSES problem set presented some interesting new problems, including "Josephus Queries", https://cses.fi/problemset/task/2164 , and I'm looking forward to editorials for these new problems:)

»
5 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Great Tutorial. I had a doubt in CSES Prime Multiples.

This is my code

Reference From CP-Algo

This is more related to C++ data types. So when I use long long for variable mult the output is wrong and when I use double the output is correct. What is the reason behind this ?

One Example of Output input

1000000000000000000 20

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71

correct output 872202319624080142

user output 872202319624079358

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

ans*=1e6 ans=round(ans); ans/=1e6;

My question is , why did you use this rounding code for Dice probability question and not for other questions like Inversion Probability , Moving Robots etc

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

In exponentiation II , why we use fermat's theorem ? Can't we just find x = power(b,c) and then power(a,x) ? I tried but it didn't work. So I want to know why? Note : I am not from Maths background so if you will share any resources it will be very useful.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    That's because $$$b^c$$$ will overflow (doesn't fit in long long), so we used Fermat's Theorem.

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks a lot!!

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi In COUNTING DIVISORS (problem-4), I understand that an array of 0s is created and then each 0 in the array is incremented in the for-loops.

I want to know the rationale behind this. Why this works?

»
2 months ago, # |
  Vote: I like it +3 Vote: I do not like it

For Another Game, the first player wins iff there is at least one heap with an odd number of coins.

If all heaps have an even number of coins, the second player can win, by taking coins from the same set of piles as the first player on the previous turn. This ensures that all heaps have an even number of piles at the end of the second player's turn, so the strategy can continue.

If at least one heap has an odd number of coins, the first player can win by taking one coin from each pile with an odd number of coins, reducing the game to the first scenario.

Proof of Stair Game is as follows:

Consider the nim game on the even-numbered piles. If the first player wins this game, they can win the entire game. A valid strategy:

  • In the first move, play in the even-numbered piles with an optimal nim strategy.

In subsequent moves:

  • If the second player moves coins from an odd-numbered pile $$$p$$$ to pile $$$p-1$$$, the first player should move the same number of coins from $$$p-1$$$ to $$$p-2$$$. Note that the nim game on the even-numbered piles is unaffected, and it is impossible for the first player to lose during this.

  • If the second player moves coins from an even-numbered pile, they have effectively made a move in the nim game, so the first player's next move should be to play an optimal move in the nim game.

After all the moves in the nim game have been exhausted, we know that the second player has the next move and that all coins are in odd-numbered piles, which means the first player ultimately wins using this strategy.

If the second player wins the nim game, use a similar strategy as the one above (i.e. if first player plays on odd-numbered pile, mirror their move; if they play on even-numbered pile, play nim game).

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

17. Counting Grids

Tutorial
Code

(Sorry for the Necropost)

  • »
    »
    9 days ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    in case someone is wondering how to get $$$250000002$$$ without using online calculator

    Code
»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain the inverse function in editorial?