Question

In: Computer Science

given pair (a,b) after each unit of time pair (a,b) get changed to (b-a,b+a).you are given...

given pair (a,b) after each unit of time pair (a,b) get changed to (b-a,b+a).you are given the initial value of pair and an integer n and you to print the value of pair at the nth unit of time.

input format:

1.First line:t donating the number of test cases

2.Next t lines:three space-seperated integer a,b and n

output format :

for each test case ,print two integers (mod 10^9 +7) per

line denoting the value of the pair at the n th unit time.

constrains:

1<t< 10^6

0< a<b<10^9

1<n<10^5

sample input :

5

1 3 5

2 3 9  

sample output

4 12

32 48

81920 131072

Solutions

Expert Solution

In 1 seconds we can make 10^8 operations to run in a CPU and the judges of online programming contests.

If you use a imple approach to to loop for n times ,the solution will time out because the it would take t*n =10^6*10^5

=10^11 operatiosn per scond which we give a Time Limit Exceeded error.So we nee dto reduce our complexity

.We cannot reduce the contrinution of t but we can reduce for the contrinution of n in complexity.We can observe the following :

The time T The value of a The value of b
t=1 a(original) b(original)
t=2 b-a b+a
t=3 (b+a)-(b-a)=2a (b+a)+(b-a)=2b
t=4 2b-2a=2(b-a) 2b+2a=2(b+a)
t=5 2(b+a)-2(b-a)=4a 2(b+a)+2(b-a)=4b
t=6 4(b-a) 4(b+a)
t=7 8a 8b
and so on...

We can take the inference from above that At odd values of t ,The value

a= (2^((t-1)/2))*a and b=(2^((t-1)/2))*b;

and At even values of t ,The value of t,a=(2^(t/2-1))*(b-a) and ,b=(2^(t/2-1))*(b+a)

which can be calculated in O(1) time.

Also we have used a modulo function to find (a^b)%mod with complexity log(b).

So the overalll complexity of the  questions is t*O(logn).

The question is running on standard test cases,I hope it will definitely run on all,if it does not plz provide more test case I am there to help

Feel free to comment if any doubts


import java.util.Scanner;

public class Swapper {
  static int mod=(int)10e9+7;



    public static long pow(long a,long b)
  {
      if(b==0)
          return 1;
      long temp=pow(a,b/2);
      long f=(temp*temp)%mod;
      if(b%2==0)
          return f;
      else
          return (a*f)%mod;

  }
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int x=s.nextInt();
        while(x--!=0) {
            long a = s.nextLong();
             long b=s.nextLong();
             int t=s.nextInt();
             if(t%2!=0)
            {
               a=(a*pow(2,(t-1)/2))%mod ;
               b=(b*pow(2,(t-1)/2))%mod ;

            }
             else
             {
                 long temp=a;
                 a=((b-a)*pow(2,t/2-1))%mod ;
                 b=((b+temp)%mod*pow(2,t/2-1)%mod )%mod;

             }
             System.out.println(a%mod+" "+b%mod);


        }
    }
}

Related Solutions

you are givena pair (a,b) .after eatch unit of time pair(a,b) getd changeed to (b-a,b+a).you are...
you are givena pair (a,b) .after eatch unit of time pair(a,b) getd changeed to (b-a,b+a).you are given the initial value of pair and an integer n and you have to print the value of the pair at the nth unit of time
For the given pair of events A and​ B, complete parts​ (a) and​(b) below. ​ A:...
For the given pair of events A and​ B, complete parts​ (a) and​(b) below. ​ A: A marble is randomly selected from a bag containing 14 marbles consisting of 1​ red, 9 ​blue, and 4 green marbles. The selected marble is one of the green marbles. ​ B: A second marble is selected and it is the 1 red marble in the bag. a. Determine whether events A and B are independent or dependent.​ (If two events are technically dependent...
For each pair a, b with a ∈ R − {0} and b ∈ R, define...
For each pair a, b with a ∈ R − {0} and b ∈ R, define a function fa,b : R → R by fa,b(x) = ax + b for each x ∈ R. (a) Prove that for each a ∈ R − {0} and each b ∈ R, the function fa,b is a bijection. (b) Let F = {fa,b | a ∈ R − {0}, b ∈ R}. Prove that the set F with the operation of composition of...
. Recall from the previous page that for each pair a, b with a ∈ R...
. Recall from the previous page that for each pair a, b with a ∈ R − {0} and b ∈ R, we have a bijection fa,b : R → R where fa,b(x) = ax + b for each x ∈ R. (b) Let F = {fa,b | a ∈ R − {0}, b ∈ R}. Prove that the set F with the operation of composition of functions is a non-abelian group. You may assume that function composition is associative
The emphasis on public health nursing has changed over time. After careful review of the reading,...
The emphasis on public health nursing has changed over time. After careful review of the reading, answer the following questions: What priorities would you set for the work of the public health nurse in today’s society? How should a nurse balance the relationship between advocacy and case management?
How would you connect a pair of equal resistors across a battery in order to get...
How would you connect a pair of equal resistors across a battery in order to get the most power dissipation in the resistors?
Each unit of A is composed of one unit of B, two units of C, and...
Each unit of A is composed of one unit of B, two units of C, and one unit of D. C is composed of two units of D and three units of E. Items A, C, D, and E have on-hand inventories of 20, 10, 20, and 10 units, respectively. Item B has a scheduled receipt of 10 units in Period 1, and C has a scheduled receipt of 50 units in Period 1. Lot-for-lot (L4L) lot sizing is used...
Which molecule or formula unit in each pair has the greater dipole moment? Use polar arrows...
Which molecule or formula unit in each pair has the greater dipole moment? Use polar arrows to indicate the bond polarity of each bond in the species and then indicate overall polarity: a)    HCl or HI b)   BF3 or NF3 c)    AlCl3 or CHCl3
A firm makes two products, A and B. Each unit of A costs $10 and sells for $30. Each unit of B costs $5 and sells for $20.
A firm makes two products, A and B. Each unit of A costs $10 and sells for $30. Each unit of B costs $5 and sells for $20. If the firm's goal were to maximize profit what would be the appropriate objective function? Maximize profit = $40A - $25B Maximize profit = $40A + $25B Maximize profit = $20A +$15B Maximize profit = 0.25A +0.20B Maximize profit = $50(A + B)
Which of the two will acquire a greater angular speed after a given time?
Torques of equal magnitude are applied to a hollow cylinder and a solid sphere, both having the same mass and radius. The cylinder is free to rotate about its standard axis of symmetry, and the sphere is free to rotate about an axis passing through its centre. Which of the two will acquire a greater angular speed after a given time?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT