🚩 What I Learned by Correcting ChatGPT: (a + b)^2 = year and the Importance of Critical Thinking
Today I want to share a very valuable anecdote for anyone who practices programming in general, and competitive programming in particular. While solving the “A. Square Year” problem from Codeforces, I not only reviewed quadratic techniques and pair searching, but also confirmed something fundamental: reading and analyzing the actual statement is the key to avoiding mistakes, even when the solution looks “trivial” or when, after you’ve solved the problem, the AI (ChatGPT) suggests another approach while ignoring the correct one you gave it.
🔍 The Problem
I faced a problem where, given a year in 4-digit format (with possible leading zeros), I had to find two non-negative numbers a and b such that:
And if no such numbers existed, I had to answer -1.
The statement did NOT impose any restriction on how many digits each number should have, nor did it ask for the partition to be in any special form.
It only required the pair to be valid.
🤖 What ChatGPT suggested to me (after I solved the problem)
At first, ChatGPT suggested I look for another way to solve the problem, since its assessment was that my solution didn’t correctly cover all cases. Even after I showed it the correct answer along with the statement, it insisted a couple of times that it wasn’t enough. After this exchange, I understood two key things:
-
The AI probably didn’t generalize correctly what it learned from previous Codeforces problems, since this question is quite recent (about two months old), which made it harder for it to find the most direct and efficient solution.
-
A big part of the real reason may have been that the AI didn’t have enough context from the start. If I had started a new conversation giving it just the problem to solve, something different would have come out, but even with my prompt explicitly adding something like: “This is my solution for the following problem: SOLUTION + Problem statement: Statement of question 2114A Square Year”, it didn’t perform as optimally as possible. ChatGPT usually handles classic, well-known questions without trouble — especially those it was trained on more extensively — but it can be less accurate with new problems or rarely seen statements.
😅 The AI’s approach
- The AI’s first approach (Wrong):
void solve() {
string s;
cin >> s;
int year = stoi(s);
// Try the two possible ways of splitting the string
for (int i = 1; i <= 3; i++) { // i: number of digits for 'a'
int a = stoi(s.substr(0, i));
int b = stoi(s.substr(i));
int sum = a + b;
if (sum * sum == year) {
cout << a << " " << b << '\n';
return;
}
}
cout << -1 << '\n';
}
- The AI’s second approach (Works, but somewhat convoluted):
void solve() {
string s;
cin >> s;
int year = stoi(s);
int root = sqrt(year);
if (root * root != year) {
cout << -1 << '\n';
return;
}
// Only ONE pair (a, b) with a + b = root, 0 ≤ a, b ≤ 9999 is needed
for (int a = 0; a <= root; ++a) {
int b = root - a;
if (b >= 0 && b <= 9999) {
cout << a << " " << b << '\n';
return;
}
}
}
💡 My Approach
For my part, I simply decided to:
- Check whether the year is a perfect square.
- If it is, print a valid pair like (√year, 0), since it always satisfies the condition.
- If it’s not a perfect square, answer -1.
I checked with the judge and all test cases were accepted.
The statement only required one valid pair (not a special one).
✨ Example of accepted code
C++ code:
#include <bits/stdc++.h>
using namespace std;
void solve() {
int year;
cin >> year;
int r = sqrt(year);
if (r * r != year) {
cout << -1 << '\n';
return;
}
cout << r << " " << 0 << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while (t--) solve();
}
Python code:
import math
def solve():
year = int(input())
r = int(math.isqrt(year)) # isqrt returns the integer root and avoids decimals
if r * r != year:
print(-1)
return
print(f"{r} 0")
t = int(input())
for _ in range(t):
solve()
📚 What I learned
-
Trust your critical thinking: Building your own logic based on what you understand (not on what “seems right”) and testing it directly on the judge can save you hours of looking for a more complex solution than necessary — even more complex than the one the AI would propose.
-
Even AI can make mistakes, so always double-check yourself. No matter how good the tool is, no AI replaces human intuition, understanding, and validation. If something doesn’t fit, question it and verify.
-
Never assume unwritten constraints. It’s easy to fall into the trap of assuming conditions out of habit — that’s what happened to the AI with this new problem — especially when you’ve solved similar problems before. But every statement is unique, and projecting what you think it should say can lead you straight into error. Going back to the real statement, without prejudice, is key.
🚀 Final reflection
This episode confirmed to me that even the best AI cannot be perfect all the time, not even with problems that look simple at first glance. Practicing programming logic remains fundamental, because artificial intelligence is still far from replacing the intuition and analysis we develop as programmers. There is still a lot to learn, both for us and for AI, especially in its ability to generalize when facing new or uncommon problems.
That’s why it’s key to read the statement carefully, trust our reasoning, and validate our solutions directly on the judge. In the end, the combination of critical thinking and constant practice remains irreplaceable in competitive programming — and in programming in general.