TCS 2020 MOCKVITA

@zoso_floyd, Dear sir, hope your solution passed all test cases. Now, here is my question. When the input is “2 100”, the answer will not fit in a 32-bit Integer, not even 64 bit-Integer. How did your code pass all the test cases, when you didn’t print the result without applying any modulo.(example 10**9+7). Sorry if I blamed for this reason, but my solution in python gave wrong answer. I am seeking for explanation why it gave wrong answer.
Here is the solution.
https://github.com/ChitturiSaiSuman/TCS_Mockvita/blob/master/p2.py

Same bro, I tried same, using sqrt is giving wrong answer. I didn’t hope to try using (x^2+y^2)/v^2. Now, I regret for that :pensive:

Hi, @anon68452476, first of all, I am no sir and if I am not wrong then there was a constraint in a question which was imposed on the result of |n2-n1| and if I am not wrong it is because of this constraint that the answer will be well within the bounds. Kindly find the question and read the constraints and also @abhiarrathore wrote a solution, in the same manner, have a look at it also. Thanks :slightly_smiling_face:

Can we discuss about it bit detailed. According to my solution, when the input is “2 100”( within the constraints), the result will not fit even in an unsigned 64 bit integer. And it wasn’t mentioned to apply modulo on the result. And such a good solution in Python is giving WA. I couldn’t debug the error. Detailed explanation of the question and solution is preferable. Thanks :slight_smile:

@anon68452476 In the question it was mentioned if I am not wrong that n2-n1<=35 and because of this constraint the answer will be within the bounds.

P.S. Please have a look at the question once again I may be wrong about the correct constraint.

Normally in mathematics distance d = sqrt(x^2 + y^2) and then time t would be t = d/v. But taking sqrt of something causes precision error.
So there is a neat get around to this situation take square of both sides i.e t^2 = (x^2 + y^2)/v^2.
Why this works because we have to find the number of cars at orgin at some instant and not time of cars.

Try solving this problem to get clear understanding of why we take square of time, as it also involves a similar trick in finding slope.

2 Likes

Ok, I will see about it, thanks for your concern. :slight_smile:

yep got it. My bad. Thanks mate.

1 Like

But what if we want ‘t’ and not t^2. How do we get back ‘t’ without losing precision?

Shouldn’t we square both sides and have t^2 = (x^2 + y^2)/v^2.
You just took t = (x^2 + y^2)/v^2.
So we don’t have to square both sides in math?

Ok, got it. Thanks a lot :slight_smile:

@karan_4336 Does your code working on input like 2, 100. Please use ``` when you write your code in text box.


@zoso_floyd n2-n1 >= 35 here, so n1=2 and n2=100 is valid input. But this gives too long output in python.

For full Explanation, you can refer here
https://www.youtube.com/watch?v=a4B48aQ3DEM

Try these accepted solutions for better understanding:

Its straightforward nothing to explain here. If you see carefully.
d/(v * v) = sqrt(d)/sqrt(v * v) = sqrt(d)/v

Hey Guys, does anyone have links to previous code vitas or PDFs. Would be great if you could help out. Thanks.

For the problem prime Fibonacci TCS is actually bounding the signed integer size to 64 bits that’s why we are getting big outputs for (2 100) in python beacuse python auto allocates more space as needed where c and c++ depend on default long long int size so inspite of the solution being correct TCS wants wrong answer from us(Wrong as it was nowhere mentioned that the int size is capped to 64bits) in c c++ it is easily doable for python u need to convert the number to 64bits signed int while calculating fibonacci series. Here is the code…

    #formatting python int to signed_int64
    def getSignedNumber(number, bitLength):
        mask = (2 ** bitLength) - 1
        if number & (1 << (bitLength - 1)):
            #bitwise or with the number
            return number | ~mask
        else:
            #bitwise and with the number
            return number & mask

    #evaluating fibonacci series as 'a' 1st term 'b' 2nd term 'n' length of series including zero
    def fib(a, b, n):
        if n == 0:
            return a
        if n == 1:
            return b
        if n > 1:
            sum1 = a
            sum2 = b
            for m in range(2, n):
                temp = sum2
                sum2 = getSignedNumber(sum1+sum2, 64)
                sum1 = temp
        return sum2

    #checking if prime
    def isPrime(x):
        count = 0
        if x < 10:
            for i in range(2, x):
                if x % i == 0:
                    count = count+1
        else:
            #as no number greater than half of itself other than itself can divide itself
            for i in range(2, int(x/2)):
                if x % i == 0:
                    count = count+1
        if count > 0:
            return False
        elif count == 0:
            return True


    inp = [int(i) for i in input().split()]
    assert(len(inp) == 2)
    a = min(inp)
    b = max(inp)
    assert(a >= 2 and b <= 100)
    assert(b-a >= 35)
    
    #1st prime list
    prime = list()
    for i in range(a, b):
        if isPrime(i):
            prime.append(i)

    #2nd prime list
    new_prime = list()
    for i in prime:
        for j in prime:
            if i != j and int(str(i)+str(j)) not in new_prime and isPrime(int(str(i)+str(j))):
                new_prime.append(int(str(i)+str(j)))
    first = min(new_prime)
    second = max(new_prime)

    #result
    print(fib(first, second, len(new_prime))) 

TCS accepted this code.
If anything I have done wrong or explained wrongly comment below.

if u got the content,plz attach it here a well

No I think today is the last day to give Mockvita and it is Mockvita 2 already and Mockvita 1 has got finished.