Long Drive On 14th

Abhi the smartest boy of IIIT-A is going for a long drive on the holy ocassion of Valentines Day with his GF and if he will drive for x hours then he will get h amount of happiness level. Here h=axk1+bxk2  where  a,b,k1,k2 are non negative integers. Obviously , Abhi wants to be as much happy as possible but Sonu has became jealous from Abhi . That is why,  Sonu can't bear the happiness level of Abhi more than M . As, Abhi is a gentle man and having a golden heart . So, he can't make Sonu angry due to his own happiness and finally he decided to drive only for that amount of hours which Sonu can bear. Abhi asked to Kalyan for calculating the maximum amount of hours x ( an integer) such that he may enjoy Valentines Day without making Sonu angry . But, Kalyan is busy in creating real life programming problems for you. So, Kalyan assigned this task to you. 

In every testcases you have given a,b,k1,k2 and M  and you have to tell the maximum value of integer x such that Sonu shouldn't become angry with Abhi. If the maximum value of x is greater than or equal to 105 then print "Love is immortal" otherwise print the maximum x .

Example:

Input:  0 2 1 2 15
Output: 2

Approach

Java

public class LongDrive14 {
    public static void main(String[] args) {
        long a = 0;
        long b = 2;
        long k1 = 1;
        long k2 = 2;
        long m = 15;
        long low = 0;
        long high = 1000000;
        long ans = 0;
        while (low <= high) {
            long mid = (low + high) / 2;
            long cal = a * ((longMath.pow(mid, k1)) + b * ((longMath.pow(mid, k2));
            if (cal <= m) {
                ans = mid;
                low = mid + 1;
            } else
                high = mid - 1;
        }
        if (ans >= 100000)
            System.out.println("Love is immortal");
        else
            System.out.println(ans);

    }
}


No comments:

Post a Comment