WA in Fibonacci Number

The following code is working perfect on my Dev C++ interface, but Codechef gives WA.


#include<iostream>

using namespace std;


int fibonacciCheck(int n)
{
	int c;
	int a=0;
	int b=1;
	c=a+b;
	while(c<n)
	{
		a=b;
		b=c;
		c=a+b;
	}
    if(c==n || n==0)
    return 1;
    else
    return 0;
}


int main()
{
    
    int t;
    int a;
    cin>>t;
    while(t>0){
    cin>>a;
    if(fibonacciCheck(a))
    cout<<"Yes";
    else
    cout<<"No";
	t--;
    }
    return 0;
} 

The input number might contain 1000 digits, and the variable you have chosen is an int, which will overflow. Read the constraints of the problem carefully.

2 Likes

Bro, the number of digits is upto 1000. How can integer accommodate that value.You need to think something else.

1 Like

Hm. I changed int to long long. Now its showing time limit exceeded. I will work on my efficieny. Thanks.

Hm. I changed int to long long. Now its showing time limit exceeded. I will work on my efficieny. Thanks.

it wont work with long long too. 1000 chars is so much more, either you need to create your own adding function for integer array to use your algo. But i think there is a better approach described in the tutorial.

1 Like
//