Question about Two Number (June Lunch Time)

This is my code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	long int t,a,b,n;
	cin>>t;
	for(int i=0;i<t;i++)
	{
		cin>>a>>b>>n;
		if(n%2==0)
		{
			a=a*pow(2,n/2);
			b=b*pow(2,n/2);
		}
		else{
			a=a*pow(2,n/2+1);
			b=b*pow(2,n/2);
		}
		if(a>b)cout<<a/b<<endl;
		else cout<<b/a<<endl;
	}
}  


// I don't understand sub-task 2 ??? How to pass this task ?

You are facing overflow problem for large constraints.

Read this discussion to clear your queries.

Here is the link: https://discuss.codechef.com/questions/103213/what-is-wrong-in-this-code-for-twonumbers?page=1#103216

Happy coding!

What happens that if both a and n are large than the new a obtained after multiplying with suitable power of 2 becomes very large and leads to overflow.
Notice that at last what u are doing is divion.The final answer is a/b or b/a whichever of a or b is greater.so multiplying b by 2(n/2) and a by 2(n/2+ 1) is same as multiplying a by 2 only once and then final answer is max(a,b)/min(a,b).

Please format your code.

1 Like