CMX1P03 - Code To Get Her - Code Mates x1 Editorial

def past(arr,W,i):arr[i - 1] += W;return arr
def present(arr,L,R): print(sum(arr[L - 1:R]))
N = int(input()); arr = list(map(int , input().split()))
for _ in range(int(input())):
	input_ = list(map(str , input().split()))
	if(input_[0] == "past"): past(arr,int(input_[1]),int(input_[2]))
	else: present(arr,int(input_[1]),int(input_[2])) #sample testcases passed, TLE

even this code got TLE for the ‘dark’ problem

In this function, the calculations are being done on large numbers, which is taking considerable amount of time. Hence, resulting in TLE.
You’ll have to use modular arithmetic. Please read the editorial.

1 Like

okay thank you

For Dark, the expected time complexity was O(Q*LogN). Editorial for Dark is available here.

1 Like

lol :joy:

1 Like

Can anybody help me why my code is wrong?
Link : https://www.codechef.com/viewsolution/37341857
I have tried to follow the same logic as editorial but can’t seem to find my mistake

editorial for https://www.codechef.com/COMX2020/problems/CMX1P01 ?

@sb_kmb Editorial for Code Together (CMX1P01): CMX1P01 - Code Together - Code Mates x1 Editorial

1 Like

@mayureshpatle can you please point out the mistake in my code :
https://www.codechef.com/viewsolution/37343577

I have used the same C++ code given at geeksforgeeks with some changes :

may be i am doing mistake with applying the mod…please help…any help is greatly appreciated

@nalingoyal In function binomialCoefficient, the values of n and k are large, so there will be an overflow during calculation. Use the method mentioned in the editorial.

1 Like

@mayureshpatle i did as u suggested…i checked for overflow in binomial cofficient function…but i didnt get AC …plz have a look.
https://www.codechef.com/viewsolution/37370795

@nalingoyal \frac{A}{B} \mod M \neq \frac{A \mod M}{B \mod M}.

\frac{A}{B} \mod M = ((A \mod M)*B^{-1}) \mod M, here B^{-1} is the modular multiplicative inverse of B under modulo M.

To use this, you will have to calculate \text{numerator} \mod M and \text{denominator} \mod M differently. Also, since M=998244353 is prime, you can use Fermat’s Little Theorem to calculate the modular multiplicative inverse of B under modulo M, which will be equal to B^{M-2} \mod M.

Hence, the result will be:

((\text{numerator} \mod M) * ((\text{denominator} \mod M)^{M-2}\mod M)) \mod M

Have a look at the ncr() function in Python 3 Solution provided in the editorial, it uses the same approach that you are trying to implement.

Also, you can read more about Modular multiplicative inverse here.

1 Like

it is not resolved yet.plz help
lli power(lli x, unsigned long long int y, unsigned long long int mod)
{
if (y == `0)
return 1;
long long int p = power(x, y/2, mod) % mod;
p = (p * p) % mod;

return (y%2 == 0)? p : (x * p) % mod; 

}
// Returns value of Binomial
// Coefficient C(n, k)
lli binomialCoeff(lli n, lli k)
{
lli res = 1,den=1;

// Since C(n, k) = C(n, n-k) 
if (k > n - k) 
	k = n - k; 

// Calculate value of 
// [n * (n-1) *---* (n-k+1)] / 
// [k * (k-1) *----* 1] 
for (lli i = 0; i < k; ++i) { 
	res *= (n - i);
            res %= mod;
	den*=(i+1);
	       den %=mod;
lli inv=power(den,mod-2,mod)%mod;
return (res*inv)%mod;
} 	

}

my solution: https://www.codechef.com/viewsolution/37372360

@redindian I had to spend a lot of time on debugging your code. And, finally found the bug. The problem is with this statement:

result = (result*(fact[x[i]]*binaryExp((fact[c[i]]*fact[x[i]-c[i]])%mod,mod-2))%mod)%mod;

Let’s make it look a little simpler,

Let, a = fact[x[i]]
b = (fact[c[i]]*fact[x[i]-c[i]])%mod
c = binaryExp(b, mod-1)

Now, put these symbols in your statement

result = (result * (a * c) % mod)% mod;

Now you can guess the error. :relieved:

result * (a * c) % mod has same execution sequence as (result * a * c) % mod, and each of result, a & c can have values upto 998244353, hence, this will result in long long limit overflow.

The correct statement should be of the following form:

result = (result * ( (a * c) % mod ) )% mod;

Just, replace the statement, with the following one, and your code will be accepted.

result = (result*((fact[x[i]]*binaryExp((fact[c[i]]*fact[x[i]-c[i]])%mod,mod-2))%mod))%mod;

You see, the problem was just because of a pair of parentheses, and it was quite tough to spot the error, so I would suggest, rather than writing a large expression in a single statement, you should always evaluate it by dividing into simpler forms. This will make your code more readable and easy to debug. :slightly_smiling_face:

1 Like

You’re violating this condition

print the name having maximum score, if there are more than one such names, then print the one which appeared first in the list.

P.S. There’s one more error in your code,

lli inv=power(den,mod-2,mod)%mod;
return (res*inv)%mod;

These two statements should be placed after the closing bracket, i.e outside the loop’s body, you’ve written them inside the loop’s body.

1 Like

thanks man… i got my mistake…actually i thought Unorderd_map keeps the thing in the order they are inserted…but that was not correct…thanks for helping me…
people like you are the reason why i like codechef discussion forum so much :smiling_face_with_three_hearts:

1 Like

Thank you very much :smile:

please help me in finding reason of WA

In power((fac[arr[i]-arr2[i]]*fac[arr2[i]]),M-2)), you are passing no = product of factorials directly, and you will end up squaring this value in power() which will lead to overflow and might give -ve result.
Just pass product \mod 998244353 instead of directly passing the product. Rest of the code seems to be fine.

When are the certificates getting distributed?