Minimum steps
You have a starting number X. You can perform one of the following operations on it any number of times:
Find out the minimum number of steps required to reach Y starting from X, or print -1 if it is not possible
Function description
Complete the function solve(). This function takes the following 2 parameters and returns the required
answer.
Input format for custom testing
Note: Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code
Output format
For each test case, print a single line containing a single integer representing the answer.
Constraints
1 ≤ T ≤ 10^5
1 ≤ X, Y ≤ 10^18
Sample input Sample Output
2 2
24 -1
6
24
16
Explanation
The first test case is explained in the statement.
For the second test case
Given
Approach
• It is impossible to reach 16 by performing operations, so the answer is -1.
Sample Input 1 Sample Output 1
2 2
24 -1
6
24
16
Sample Input 2 Sample Output 2
100 16
50 26
3276800 2
6308233216 21
94 5
12 4
48 18
55 23
115343360 26
2464 12
Shopping Spree
Steve goes to a shop to buy N candies. After his purchase, the shopkeeper offers him a crazy refund on his bill.
The shopkeeper tells Steve that he can select any of the [N/2] candies and the shopkeeper will refund the sum of the square of the prices of those candies.
Now, before buying the candies, Steve can perform an infinite number of the following operations:
Now, Steve wonders what is the maximum refund he can get if the number and cost of the candies are already decided. Help Steve get the maximum benefit of this craziness.
Since the answer can be too great, output the answer modulo 10^9 +7
Function description
Complete the function solution(). This function takeş 2 parameters and returns the answer:
Input format for custom testing
Note:Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code.
Output format
In a single line, print the maximum refund Steve can get
Constraints
1≤N≤ 10^5
1≤candyPrice ≤ 10^8
Sample Input Sample Output
5 98
1 6 7 5 3
Explanation
Let's perform the operation on the cost of Candy 1 and Candy 2.
The cost of Candy 1becomes (3 | 6)= 7.
The cost of Candy 2becomes (3 & 6) = 2.
Now, we can pick Candy 1 and Candy 3, and ask for the sum of squares of their prices as a refund.
That is 7^2 + 7^2 = 49 +49 = 98
The following test cases are the actual test cases of this question that may be used to evaluate your submission.
Sample input 1 Sample output 1
5 98
3 6 7 5 3
Sample input 2 Sample Output 2
10 56075
84 87 78 16 94 36 87 93 50 22
Note:
Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your cod these hidden test cases to solve the problem statement.
Scoring
Score is assigned if any testcase passes
Allowed Languages
C#, Java 8, Java 14, JavaScript(Node.js)
public static void main(String args[]) throws IOException {
int t=in.nextInt();
while(t-->0){
long x=in.nextLong();
long y=in.nextLong();
if(x>y){
if(x%y!=0)print(-1);
else{
print(f((long)(x/y)));
}
}
else{
if(y%x!=0)print(-1);
else{
print(f((long)(y/x)));
}
}
}
}
public static long f(long p){
for(long i=0;i<63;i++){
if((long)(1l<<i)==p)return i;
}
return -1;
}
Q1 solution can be this