We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Enter your code here. Read input from STDIN. Print output to STDOUTimportsysdefgo():n,k,p=map(int,sys.stdin.readline().split())A=n+1B=k+1res=1f=[1]foriinrange(1,p):f.append(f[-1]*i%p)whileAorB:a,b=A%p,B%pA//= p B//= pifa<b:return0res*=f[a]*pow(f[b]*f[a-b]%p,p-2,p)res%=preturnresfor_inrange(int(sys.stdin.readline())):print(go())
My answer seems to be working right for most of the test cases, but got wrong on nearly half of the tests. I cannot think of any reason why that happens. Based on the editorial the calculation should be very straight forward. Can you guys look at the output and give me some hint where I got it wrong?
I calculate nCr mod P directly by cancelling out the small prime factors in the anominator and denominator of N!/(K!*(N-K)!). It's much more efficient than the way proposed by the editorial. But I still cannot get it right.
Python3 solution
C++ CODE .............. I TRIED AND SUCEED
include
using namespace std;
define MAXP 100000
int p; long long fact[MAXP];
long long get_exp(long long n){ long long ret = 0;
}
int calc(long long n){ if(n == 0) return 1;
}
long long mod_pow(long long a, int b){ long long ret = 1;
}
long long comb(long long n, long long m){ if(get_exp(n) > get_exp(m) + get_exp(n - m)) return 0;
}
int main(){ int T;
}
My answer seems to be working right for most of the test cases, but got wrong on nearly half of the tests. I cannot think of any reason why that happens. Based on the editorial the calculation should be very straight forward. Can you guys look at the output and give me some hint where I got it wrong?
I calculate nCr mod P directly by cancelling out the small prime factors in the anominator and denominator of N!/(K!*(N-K)!). It's much more efficient than the way proposed by the editorial. But I still cannot get it right.
Any hint on how can I reduce the time taken for execution? I am using BigInteger, and it's working well until and unless there isn't any time limit.
In the editorial, I am unable to understant the part where inverse of n is being calculated.
Precisely, this part =>
calculate inverses mod p
for i in xrange(2,p): inv[i] = (p - p/i) * inv[p%i] % p
I know how to calculate inverse using extended Euler's theorem. It would be nice if you could elaborate the logic behind it or share some link.