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?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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?
Название |
---|
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.