ahnaf09's blog

By ahnaf09, history, 6 months ago, In English

I’ve heard from many people that regularly reading about math — especially Number Theory, Combinatorics, and Geometry — can really help in competitive programming.

I’m mainly focused on improving at CP, not studying deep academic mathematics, but I still want to spend some time each day building my math skills.

Could you share some good articles, websites, or resources that are helpful for learning math specifically for competitive programming?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By ahnaf09, history, 7 months ago, In English

I am new to Ubuntu and trying to precompile the bits header for faster C++ compilation. On my system, the path to the header files is:

/usr/include/x86_64-linux-gnu/c++/11/bits

Listing the directory contents after navigating to the path shows the following: Directory Listing


Current Sublime Text Build System

Here is my current Sublime Text build configuration:

{
  "shell_cmd": "g++ -std=c++20 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wno-unused-result -DLOCAL -include ~/.stdc++.pch -o '$$${file_path}/$$${file_base_name}' '$$${file}' && timeout 10s '$$${file_path}/$$${file_base_name}'  \lt  '$$${file_path}/in.txt' > '$$${file_path}/out.txt' 2 \gt  '$$${file_path}/err.txt'",
  "file_regex": "^(...*?):([0-9]+):([0-9]+): (.*)$",
  "working_dir": "${file_path}",
  "selector": "source.c++, source.cpp",
  "variants": [
    {
      "name": "Run Only",
      "shell_cmd": "timeout 10s '$$${file_path}/$$${file_base_name}' < '$$${file_path}/in.txt'  \gt  '$$${file_path}/out.txt' 2> '${file_path}/err.txt'"
    },
    {
      "name": "Compile Only",
      "shell_cmd": "g++ -std=c++20 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wno-unused-result -DLOCAL -include ~/.stdc++.pch -o '$$${file_path}/$$${file_base_name}' '${file}'"
    }
  ]
}

Errors Encountered

After using this build system, I received compilation errors as shown in the screenshot: Compilation Error


Notes

  • The build attempts to include a precompiled header ~/.stdc++.pch.
  • Compilation fails, possibly due to incorrect precompiled header setup or incompatible paths.
  • I am looking for a proper way to precompile the bits header in Ubuntu and integrate it with Sublime Text.
  • Code Link where i error

Full text and comments »

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

By ahnaf09, history, 7 months ago, In English

Hello everyone,

I’m currently stuck on a Codeforces problem — Problem 2137B. While reading the official editorial, I got confused about one particular transformation step:

Each element is replaced by n + 1 - p[i].

I understand the use of n + 1, since we’re working with adjacent elements and need a length of n + 1. However, I don’t quite understand why we subtract p[i].

Also, I noticed that the editorial mentions a condition like: G C D ( p i + q i , p i + 1 + q i + 1 ) ≥ 3

I’m wondering if this relates to the property gcd(a, b) = gcd(a - b, b) where (a > b). Is that property being applied here?

And finally, does the condition hold simply because (n > 3) (since (n + 1 > 3)) — or is there a deeper reason behind it?

// C++ Version of the logic
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        vector<int> a(n), b(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int i = 0; i < n; i++) b[i] = n - a[i] + 1; // ??
        for (int i = 0; i < n; i++) cout << b[i] << " ";
        cout << endl;
    }

    return 0;
}

I’d really appreciate it if someone could explain this step more clearly. I’m still a beginner, so a simple explanation would be very helpful.

Thank you in advance!

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By ahnaf09, history, 7 months ago, In English

Hello Everyone, Hope you all are doing well!

Problem: 2149A - Be Positive I was trying to solve this problem but got completely stuck. Then I looked at other people's submissions.

Submission: 340371416 I found this solution, and according to my understanding, it runs in 31ms. Most other solutions follow a similar idea, but I’m a bit confused about one part:

cout << z + ((on % 2) * 2) << endl;

I understand that z counts the number of zeros in the array and on counts the number of negative numbers. What I don’t understand is how this formula calculates the minimum number of positive 1s needed to make the product of all array elements positive.

Could someone please explain the logic behind z + ((on % 2) * 2) in simple words? It would help me a lot.

Thanks a lot for your time and help! Have a great day!

Full text and comments »

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

By ahnaf09, history, 7 months ago, In English

Hello everyone,

I am planning to participate in my National Olympiad in Informatics, which will likely be held around mid-February. I want to use the coming months wisely to prepare.

Could you please suggest:

  1. The most important topics to focus on (data structures, algorithms, math, etc.)
  2. How to balance theory and problem-solving practice
  3. Useful resources for Olympiad-level preparation
  4. Tips for building confidence before the contest

I have some basics in programming, but I want to get stronger step by step. Any advice would be greatly appreciated.

Thank you!

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it

By ahnaf09, history, 9 months ago, In English

Hey everyone, salam! I’m trying to understand a line from the editorial for 2120B - Square Pool.

Here’s the part of the code:

for (int i = 0; i < n; i++) {
    cin >> dxi >> dyi >> xi >> yi;
    if (dxi == dyi) ans += (xi == yi);
    else ans += (xi + yi == s);
}

I understand the first ifif dxi == dyi, the ball is moving along the main diagonal. If xi == yi, it’s already on that diagonal, so it will go straight to the pocket.

But I’m a bit confused about the second part:

else ans += (xi + yi == s);

Why are we checking xi + yi == s here? Is this because it represents the other diagonal of the square? And is it correct that collisions don’t affect this count because balls just swap directions, so the number heading to pockets stays the same?

Thanks in advance for the help! Really appreciate it :)

Full text and comments »

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

By ahnaf09, history, 9 months ago, In English

Salam everyone!

I’m a total newbie here on Codeforces :) and I’m trying my best to level up. I wanna ask the community — how do I get out of newbie?

Like...

  • Which topics should I learn first? (I’m kinda confused between learning random stuff vs. having a proper roadmap)
  • What should my practice strategy be so I actually improve and not just grind aimlessly :)
  • Any good problem lists or contests you recommend for my level?
  • Also, how much estimated rating do I need if I ever dream of participating in IOI in the future? :)

Right now my practice strategy is kinda simple — I try to solve 100 problems of each rating (step by step), give all CF contests, and if I get stuck for too long I just read the editorial and learn from it :)

Any tips, experience, memes, motivation — I’ll take all of it :) If you have advice, please drop it in the comments and upvote so more people can see :)

thanks in advance :)

Full text and comments »

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

By ahnaf09, history, 10 months ago, In English

Hey Codeforces! Today I started learning the precomputation technique of prefix sum, covering both 1D and 2D arrays, as it’s a powerful tool for speeding up range queries. Since I’m new to this topic, I followed some lectures and made rough handwritten notes to better understand the concept—check them out here. Before moving on to other important topics like precomputation + hashing, basic modular arithmetic, and related number theory tricks, I want to practice 5–10 problems focused on prefix sums (range sum queries in 1D and submatrix sums in 2D). If you know any beginner to intermediate-level problems that helped you grasp prefix sums better, please drop them in the comments. Let’s keep learning and leveling up together! – algorider

Full text and comments »

  • Vote: I like it
  • -18
  • Vote: I do not like it

By ahnaf09, history, 10 months ago, In English

Hi everyone,

While solving Problem 2107A - LRC и VIP, I got stuck and decided to read the editorial. In the editorial solution, I came across the following line:

for (int i = 0; i < n; ++i)
    cout << (1 + (a[i] == mx)) << " \n"[i + 1 == n];

I have a basic understanding that the expression (a[i] == mx) returns a boolean value, and when it's true, it becomes 1, so the full expression would be 1 + 1 = 2; otherwise, 1 + 0 = 1.

However, I'm not fully sure about the use of " \n"[i + 1 == n]; — I assume it’s a trick to conditionally print either a space or a newline depending on whether it’s the last element in the loop.

Can someone please explain this line step-by-step? I'd really appreciate any clarification.

Thanks in advance.

Full text and comments »

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

By ahnaf09, history, 10 months ago, In English

Hey, I'm ahnaf09, a newbie who just started doing CP. Recently I learned C++ and some basic STL.

A few days ago, I faced binary search for the first time in a problem. To learn it better, I searched "binary search tutorials" on Google. I found some articles and eventually got to Errichto's binary search lecture. It helped, but I still feel I need more time to fully understand and be comfortable with this algorithm.

Right now my practice method is very simple:

  • I filter 800–900 rated problems
  • Try to solve 5 problems a day
  • I mostly pick from latest contests, attempt them seriously, think for 30 minutes, and if I can't solve them, I read the editorial and learn from it.

I do CP along with my academic studies and I get around 6–7 hours a day to study + practice.

Now I have a question. Should I pick a fixed day like Friday only for learning specific topics (like binary search, two pointers, prefix sums, etc.) and practicing them deeply? Or should I just learn topics when I face them in problems, and continue solving random problems every day?

Would love to hear advice from experienced people here. :)

Thanks in advance!

ahnaf09

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it