Блог пользователя Asakire

Автор Asakire, история, 10 месяцев назад, По-английски

Recently i went through few modulo and integer arithmetic section from a website

I am writing this as recap for myself but it might help beginners who are unfamiliar with Modular arithmetic

A few basic observations

If a mod b = 0, then a is divisible by b. This single condition appears in an enormous number of problems, from basic number theory to constraints checking.

Another common use is determining parity. If n mod 2 = 0, the number is even; if n mod 2 = 1, it is odd. Whenever a problem involves alternating behavior, parity, or splitting elements into two groups, modulo 2 is usually the first thing worth checking.

Modulo and cyclic behavior

One important property of modulo is that it naturally creates cycles. The values of n mod 5, for instance, repeat as 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, ....

This is extremely useful in practice. Circular arrays, rotating turns, and repeating patterns can all be handled cleanly with modulo. If an array has size n and an index i goes out of bounds, using i mod n wraps it back into the valid range.

for a real life example we can take the days of week : they repeat every 7 days , so its exactly modular arithmetic (N%7 would give us the day even if the N value is more than 7)

Modulo properties and large numbers

Two properties of modulo are used constantly in competitive programming:

(a + b) mod m = ((a mod m) + (b mod m)) mod m (a × b) mod m = ((a mod m) × (b mod m)) mod m

These identities allow us to reduce numbers at every step instead of working with extremely large values. This is crucial when problems ask for answers modulo a large number such as 10^9 + 7. Multiplying first and taking modulo at the end often causes overflow, while applying modulo during intermediate steps keeps everything safe.

consider computing a product of two very large numbers modulo 10^9 + 7. Direct multiplication may overflow even 64-bit integers. By reducing both numbers modulo m first, then multiplying, and finally applying modulo again, the result remains correct while staying within safe limits.

For example

    import math
    tc=int(input())
    mod=10**9+7
    for i in range(tc):
        m=int(input())
        p=(m-1)**2
        r=pow(2,p)
        print(r%mod)

The above code return ML (Memory limit error due to overflow while doing the pow)

Which can be rectified using mod in intermediate steps:

    tc = int(input())
    mod = 10**9 + 7
    for I in range(tc):
        m = int(input())
        p = (m - 1) * (m - 1)
        print(pow(2, p, mod))

Negative numbers and modulo

modulo with negative numbers. Mathematically, -7 mod 3 equals 2, but programming languages handle this differently. Python returns 2, while C++ and Java return -1.

To avoid surprises, a commonly used safe expression is (a mod m + m) mod m. This guarantees a value in the range [0, m−1], regardless of the language.

Digit extraction using modulo

Modulo is also heavily used in digit manipulation problems. The last digit of a number can be obtained using n mod 10, and removing the last digit is done via integer division by 10.

For example, repeatedly applying these operations to 12345 extracts digits from right to left. This pattern appears in problems involving digit sums, palindromes;

Integer division vs modulo

Integer division and modulo are complementary. Integer division tells us how many full groups can be formed, while modulo tells us what remains. Many problems rely on using both together, such as grouping elements evenly or stripping digits from a number.

Understanding when to use each makes solutions much cleaner.

Common mistakes

A frequent mistake is confusing division with modulo, or mixing them incorrectly in expressions without proper parentheses. Another common issue is ignoring negative modulo behavior, which can silently break solutions in some languages. Finally, applying modulo only at the end of a long calculation often leads to overflow and incorrect results.

Some easy practice problems Happy Number Add digits Fizz Buzz Power of two

This post was written while revisiting the modulo and integer arithmetic sections on REPOVIVE . Thanks to Shayan for building and maintaining such a useful resource.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -2
  • Проголосовать: не нравится

Автор Asakire, история, 10 месяцев назад, По-английски

Today's D had more submissions than usual It's obvious a lot used AI and also solutions were leaked via telegram Why can't codeforces ban such people? What are the possible ways to reduce such cheatings? Is CP dying or is it dead :( cheating The above image is of the one who shared solutions via telegram and I think the codeforces ID is :!

https://codeforces.me/profile/Ananya_codenova

I think this is the person because the submission status is mostly matching with this ID and in the telegram grp only solutions upto A-F1 and G was given but F2 was not given(this is also didn't solve F2)

Is there any way to stop such things???

Полный текст и комментарии »

  • Проголосовать: нравится
  • +51
  • Проголосовать: не нравится

Автор Asakire, история, 10 месяцев назад, По-английски

I’m practicing competitive programming, but maths and especially combinatorics are my weak points. I’m okay with implementation and standard logic, but I struggle with counting problems, combinatorial observations, and math based reasoning.

I want to improve maths/combinatorics only from a CP perspective focusing on patterns, intuition, and common techniques rather than heavy theory or memorization.

I’d appreciate advice on:

**Where to start if maths basics are weak**
 **Which maths/combinatorics topics matter most for CP (priority-wise)**
 **Any beginner-friendly resources (blogs, books, playlists, CF problem sets)**

If anyone here had similar issues earlier and managed to improve, please share what worked for you.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится