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

Naaz's blog

By Naaz, history, 15 months ago, In English

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

  • Vote: I like it
  • -2
  • Vote: I do not like it

»
15 months ago, # |
  Vote: I like it +8 Vote: I do not like it

The problem is that you are trying to find $$$^0P_{\ 1}$$$, which leads you to get inv[-1]. Also, it would be better if you shared your entire code (which failed) instead of just your BINO struct. We never know where bugs are hiding after all :)

Fix
  • »
    »
    15 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Also, I think why your AC code got accepted was because you made the array global. I don't know much but when you declare an array global the elements are automatically set to $$$0$$$, but in case of non-global arrays garbage value are stored in each place instead.

  • »
    »
    15 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    And it did get accepted. 211427952 Your assumption was correct as it was trying to calculate inv[-1]. Thanks Big help.