ohhh f*ck yes.
when I prepared the PPT I knew this fact so in the video there is written complexity LogN , then after some days when I started recording video during recording I was like hmmm there 2 nested loop each running roughly logN time so complexity must be LogN^2.
sir, after watching the lecture, i submitted kth prime number(TDKPRIME) problem. Then i went for the problem KPRIMES2(hard) on SPOJ itself, it is giving runtime error at the last testcase.
please help me to optimize the code.
in lecture 15 how come (n! / c! * r! ) % m = (n! % m) * ( inverse( c! %m ) %m * ( inverse( r! %m ) %m
inverse(c!)%m is fine but how come inverse(c!%m)%m because in factorial funtction we are always taking %m when multiplying.
so according to this inv(b) = inv (b%m) where b is any number and m is for modulo???
I recently watched your video on July circuits hacker earth , where you used a line in the code for x y z path queries problem ,
invFact[i] = (invFact[i-1] * power(i , mod - 2)) % mod;
I have been trying to figure out how to calculate inverse modulo in dp since then and haven’t figured it out yet . It would be really helpful if you could explain it here or make a video on " how to calculate modulo inverse of all positive integers up to n".
Ok , so we have invFact[i] = ( Fact[i-1]*i ) ^ m-2 and Fact[i-1] ^ m-2 is invFact[i-1] so we have written invFact[i] = invFact[i-1] * (i ^m-2) . Is that right ?
Also the time complexity of this approach is O(nlog(n)) , ( if I’m not wrong ) but there’s a O(n) solution to calculate inverse modulo for a range of (1,n) , can you please explain that as well ?
plz HElp … this question is “Fire Escape Routes”
link: FIRESC Problem - CodeChef
sample test case is passing but while submitting it says Wrong ans …i couldn’t figure this out …
i using same method as @waqar_ahmad224 - YouTube
code is in PYTHON 3.6
submittion id : 37325098
user name: akankshaerror
aj=0
def dfs( node):
global aj
aj=aj+1
vis[node]=1
ln= len(li[node])
for child in range(1 , ln):
if vis[li[node][child]]==0:
dfs( li[node][child])
xx=10**5
li= [0 for i in range( xx + 1) ]
vis=[ 0 for i in range( xx + 1)]
try:
for _ in range(int(input())):
n,m = map(int, input().split(" "))
li= [[i] for i in range(n+1) ]
vis=[ 0 for i in range(n+1)]
for i in range(m):
a,b= map(int, input().split(" "))
li[a].append(b)
c=0
res=1
for i in range(1 , n+1):
if vis[i]==0:
aj=0
dfs(i)
c=c+1
res= (res % (10**9+7) * aj% (10**9+7) )% (10**9+7)
print(c , res)