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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
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.