# what is wrong in this code for twonumbers?

why i am getting 30 points?
what are the test cases on which this code get fails?
Problem-https://www.codechef.com/LTIME49/problems/TWONMS

solution-

``````if(n==1)
{

cout<<max(a*((n/2)+1)*2,b)/min(a*((n/2)+1)*2,b)<<endl;

}

else if(n%2==0)

{

cout<<max(a*(n/2)*2,b*(n/2)*2)/min(a*(n/2)*2,b*(n/2)*2)<<endl;

}

else

{

cout<<max(a*((n/2)+1)*2,b*(n/2)*2)/min(a*((n/2)+1)*2,b*(n/2)*2)<<endl;

}``````

I don’t get it exactly, why are you doing (n/2+1) and (n/2) with a and b in your solution.
You can do it like this:

``````if(n%2==0)
cout << max(a,b)/min(a,b);
else
cout << max(a*2,b)/min(a*2,b);
``````

that’s all it takes to get AC.

2 Likes

@vivek96 that’s true that if odd we have to multiply with 2 n/2+1 but u see n/2 is common in both those so if n is odd a is getting multiplied by an extra 2 otherwise both are multiplied by same number of 2
The thing is that you need not put that n/2 factor it is common in both.

Vivek , N can range upto 10^9 , along with A and B. And problem statement says that they multiply number by 2. So it should be 2^N/2 and 2^(N+1)/2

And it will cause overflow. You need to follow what vishesh said.

if(n%2)
a*=2;
if(a<b)
swap(a,b)
cout << a/b;

its simple as that

means if n is odd we have to multiply a with 2 n/2+1 times,ex n=3 we will multiply n with 2 times thats why n/2+1 and b with n/2 times means 1 times because game is always start from A and if n is even then a is multiplied by n/2 times and b is also n/2 times with 2(same thing if you didnt multiply),but n==1 means we dont have to multiply b because there is only 1 turn so a is multiplied with n only 1 times and rest i used min and max functions

I don’t think so, I need to say anymore.@vishesh_345 and @vijju123 clears your doubt already.
Happy Coding!

2 Likes

i also used bigintegers for overcoming overflow,check my solution of bigintegers https://www.codechef.com/viewsolution/14332958

i agree with u,but why i am getting wa? i know my code is multiplying un-necessary but what is incorrect?

What is the expression you are trying to achieve by that big integer?? Is it 2^N/2 etc. ?

Also, you have to print max(a,b)/min(a,b) [after multiplication]. Are you doing that?

have u checked my code,please read out my above comment for understanding my code

``````we will multiply n with 2 times thats why n/2+1 and b with n/2 times means 1 times
``````

Multiply with WHAT? You dont ahve to multiply with n or n/w or anything. Its pow(2,n/2) and pow(2,n/2+1) if i am not wrong. Each turn, the numbers are multiplied by 2.

If we multiply with 2 three times, its 2^3 = 8, not 2x3=6.

1 Like

i think that is my mistake,i have to use power function,means we have multiply the number with n times of 2?

Yes. 2^(number of turns the respective player got.)

1 Like