I recently solved the problem of Codeforces round 146.
This is the problem statement
C++17 submission: AC submission
C++14 submission: WA submission
Both the codes are exactly same but the verdict is different.
Can anyone explain why this happens?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
I recently solved the problem of Codeforces round 146.
This is the problem statement
C++17 submission: AC submission
C++14 submission: WA submission
Both the codes are exactly same but the verdict is different.
Can anyone explain why this happens?
Name |
---|
Your code C++14 submission uses LCM(int, int) and passes 2 long long arguments. Integer overflow may cause the WA verdict.
Your C++17 submission uses std::LCM(long long, long long), a template instantiation of std::LCM, which is only available since C++17. The built-in LCM function accepts two long long arguments, which does not cause integer overflow.
UPD: AC submission.
Replacing
int lcm(int a, int b){return (a*b)/__gcd(a, b);}
with
ll lcm(ll a, ll b){return (a*b)/__gcd(a, b);}
fixes the problem in C++14.
In C++14 your code calls on your int based lcm, which causes UB.
Not only does it use int, it is also written in such that it can easily overflow. Switching it to
gives AC 81694498.
As for why C++17 survives. In C++17 they added lcm to the standard library, so your call to lcm actually calls std::lcm.