Please read the new rule regarding the restriction on the use of AI tools. ×

Unusual behaviour of G++ 20 compiler on the code.

Revision en1, by Naaz, 2023-06-29 12:43:06

i recently submitted a code to the task 1436C - Binary Search but one submission where i used the modular inverse to calculate nPr went straight passed without any hassle.so then i decided to create a struct which would create all the factorial and inverse and would contain the correlated function for calculating the nPr and nCr under a certain modulo.

Here is the code for it.


struct BINO { ll N; vector<ll> fact, inv; BINO(ll n) { N = n; fact = vector<ll>(N + 10); inv = vector<ll>(N + 10); fact[0] = fact[1] = 1; FOR(i, 2, N) fact[i] = (fact[i - 1] * i) % MOD; FOR(i, 0, N) inv[i] = bin_pow(fact[i], MOD - 2); } ll bin_pow(ll b , ll e) { ll res = 1; while ( e) { if ( e & 1) res = (res * b) % MOD; b = (b * b) % MOD; e /= 2; } return res % MOD; } ll fac(ll k ) { return fact[k] % MOD;} ll nPR(ll n , ll r) { return ( fact[n] * inv[n - r]) % MOD; } ll nCR(ll n , ll r) { return (((fact[n] * inv[n - r]) % MOD) * inv[r]) % MOD; } };

fair enough i thought to check the correctness of the code by submitting to the same question once again. But it failed at a certain test case

2 2 0 the answer to this task is 0 which my local compiler is also giving. But somehow the codeforces compiler is giving wrong answer. i have checked for integers overflows but cant find any , but when used custom test on codeforces with CLANG 20 diagnostics. it showed some overflow.

Please help me to find the integer overflow. Or is it some default behaviour of gcc which I don't know about. Any help would be totally beneficial.

Here is my accepted submission. 211400470

Tags int overflow, help, compiler, undefined behavior

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Naaz 2023-06-29 12:43:06 1710 Initial revision (published)