I would like you to read the full story and give me some feedback on it, Thank You.
Today I was solving this amazing 715A - Плюс и квадратный корень problem. After finding the idea and implementing it in C++, I submitted it in C++17 and later in C++20.
But I was getting the wrong answers in the main test 5. At first, I thought my idea was wrong and I tried to prove my idea. Thinking about the problem the whole day I got nothing wrong with my approach and implementation.
By not finding any other way I opened my submission and then I saw the reasons for the wrong answer 198441320. But in theory and my implementation method, there could not have errors such as this.
After that, I got experimental and tested the code, and changed certain bits. But whatever I did it didn't change the verdict.
The main part of my code was:
ll lcm(ll a, ll b){
ll LCM = (a * b);
return LCM;
}
void solve(){
ll n;
scl(n); // scan in long long
ll LCM = 2;
for(int i = 1; i <= n; i++){
ll templcm = lcm(i, i + 1);
ll res = (templcm - LCM) / i + (templcm / i) * (templcm - 1);
cout << res << endl;
LCM = templcm;
}
return;
}
But after that, I submitted the same code with the change of variable type of i (loop Variable) that got AC 198513530.
// Previous codes are unchanged
for(ll i = 1; i <= n; i++){
ll templcm = lcm(i, i + 1);
ll res = (templcm - LCM) / i + (templcm / i) * (templcm - 1);
cout << res << endl;
LCM = templcm;
}
Here I should not have got this verdict because in whatever the type of i (loop Variable) be, it should be converted to long long.
Now I was having fun, in another version: I have declared another variable j, and did this and that also got an AC:
for(int i = 1; i <= n; i++){
ll templcm = lcm(i, (i + 1) ), templcm2 = lcm(j, j + 1);
templcm = templcm2;
ll res = (templcm - LCM) / i + (templcm / i) * (templcm - 1);
if(templcm != templcm2){
cout << templcm << " " << templcm2 << endl;
}
cout << res << endl;
LCM = templcm;
j++;
}
Finally, I submitted my first submission (Where i in int type) 198505674 in C++14 and with the help of magic it got AC That same code that got WA in C++ 17 and C++20 got AC in C++14.
Here are two codes that differ only in variable names, one for AC and the other for WA ON 5, so I guess the name caused the conflict. When we test the following code within customtest, it gives an output of 15 without Hello, and when using a lower compiler version, it says hello to me.
So the result is Codeforces built int lcm(int, int) into the C++17 or higher compiler, so it grabbed your lcm function, causing a signed integer overflow.
What You said was correct, I change the function name and after that it got AC. Thank You.