AMIFIB - Editorial

Please tell, how is checkSquare() method implemented in author’s solution? Which formulae is used to check whether a number is perfect square or not? Any link/information will be highly appreciated.
Thanks

here is simplest solution by RAJAT De

hi here is a simple code
#include <stdio.h>
 #include <string.h>
 #include<iostream>

 using namespace std;
 long long c(char *s){

 long long i,n=0;
    for(i=0;i<strlen(s);i++)
        n=10*n+s[i]-'0';
      return n;
    }

  int main()
 {

            int t;
           cin>>t;

      while(t--)
    {
      long long a[6000],i,m;
      char s[1001];
      cin>>s;
      m=c(s);

      a[0]=1;a[1]=1;

      for(i=2;i<6000;i++){
          a[i]=a[i-1]+a[i-2];
          if(a[i]==m){
             cout<<"YES"<<endl;
             break;
          }
         if(i==5999){
          cout<<"NO"<<endl;
        }
    }
   }
   return 0;
 }

HAPPY CODING
can anyone explain how it does work with long string

hey,can anyone tell me the error in the code, i am using property 5nn + 4 or 5nn - 4 or both are perfect square to solve it .CodeChef: Practical coding for everyone

For those who are saying this question was biased towards JAVA, Well then I solved this problem in C++ in 5 minutes CodeChef: Practical coding for everyone and make sure you save my template so that you can do the BIGINTEGER stuff in c++ using strings.

@sikander_nsit, questions are not language dependent, some languages have advantages over others depending on the case. Competitors will always try and take advantage of their languages features.

1 Like

@junior94 By language dependent I meant the same thing as one language being advantageous than other. My point was that a beginner who uses C/C++ would not have been able to solve this question. This should not be the case for the first(easiest) question in the contest.

4 Likes

testers solution says 10256117644121029666 is a fibonacci no. but it isnt!

1 Like

ya definately there is something wrong with test cases !!

He is just doing the operations in a unsigned long long, so when ever over flow occurs he is JUST ALLOWING IT. It is equal to performing calculations modulo 2^64.

No, his solution is wrong. It says 10256117644121029666 is a fibonacci no., but it is not.

Your code link: WMDHVe - Online Python Interpreter & Debugging Tool - Ideone.com
Tester code link: GLjOzF - Online C++0x Compiler & Debugging Tool - Ideone.com

1 Like

In the tester’s solution the mx limit upto which the fib sequence is calculated is taken as 6666.Is it just some random limit and if not then how’d we arrive to the no 6666.Someone please explain me the logic behind this.

Tester’s solution also gives wrong answer for 10256117644121029666 which is not a fibonacci number.

admin please answer!!!

@junior94 this is not a platform to test language skills. If algorithmic skills were to be tested, the limits would have been within 10^18.

1 Like

@rishul_nsit, I didn’t say it was a platform to test language skills, I only said that users usually take advantage of their language features. In cook offs it’s always better to do a task in the shortest way possible. I won’t deny that python and java had a significant advantage over C and C++ but this is not the first time that a question regarding BigInteger is appearing and possibly not the last either. But I must agree with @sikander_nsit statement too, for one of the easiest problems in the cook off, it required a significant amount of coding.

Can you explain how the tester is storing 1000 digit numbers into a set of unsigned long long !

3 Likes

@junior94 what I meant was such questions should appear in Long contests and not the cook offs

Those who know java or python well can easily solve it…
I applied the logic that if 5nn + 4 or 5nn - 4 is a perfect square number then the number would be fibinoci number…
but that will not fit in the normal data types…this may be an easy problem…but not a confidence booster for newbies like me who are learning to swim in the pool of c++…:frowning:

As described, the method is based on hashing technique. Numbers which are bigger than ULL are automatically stored as remainders of this max value. Now when you take the input as string and convert it to number again and if IT ALSO generates the same remainder (after being ‘wrapped’ around ULL_MAX) will be found in the set, otherwise not.

This obviously assumes that there are no two numbers (one of which is Fibonacci while other not) that give same remainder, atleast in the given constraints). This technique however, doesn’t guarantee solution and may or may not work.

1 Like

Was this done in purpose so Tester’s solution would pass? Because while hashing is a nice and general technique, it shouldn’t be used in such “tricky”/improvised way as it might mislead some beginners as to when to use hashing or not… The fact that the test cases were “good” enough so that this “natural hashing” technique would pass makes me think that this was done in purpose as this solution can’t even guarantee correctness…

5 Likes