• + 2 comments
    // I store numbers as vectors. Any number can be represented as a vector of digits
    // [ a0 , .. , an] = a0 + a1*10 + .. + an*10^n
    // During calculations I temporarily do not use a constraint that each number a0 .. an is a single digit. I restore it after the summations and multiplications are done
    
    // Transform integer to vector
    vector<int> number_to_vector(int t)
    {
        vector <int> ans;
        
        while(t != 0)
        {
            ans.push_back(t % 10);
            t = int(t/10);
        }
        return ans;
    }
    
    
    
    
    
    // Compute the sum of two integers represented as vectors
    vector <int> my_sum_raw(const vector<int> &t1 , const vector<int> &t2)
    {
        int N1 = t1.size();
        int N2 = t2.size();
        int N = max(N1 , N2);
        
        int carry = 0;
        
        vector<int> ans (N);
        
        for (int i = 0 ; i < N ; i++)
        {
            int a = 0;
            int b = 0;
            
            if (i < N1)
                a = t1[i];
            if (i < N2)
                b = t2[i];
                
            ans[i] = a + b;
        }        
            
        return ans;
        
    }
    
    
    // The most brute-force way to compute the square of a number represented by a vector
    vector<int> my_square_2(const vector<int> &t)
    {
        vector<int> ans(2*t.size());
        
        int carry = 0;
        
        for(int i = 0 ; i < t.size() ; i++)
        {
    			  // Notice, that here I am avoiding double-counting. Otherwise my code would not pass all tests
            for(int j = i ; j < t.size() ; j++)
            {
                int x = t[i] * t[j];
                if (i != j)
                    x *= 2;
                ans[i+j] += x;
            }
        }
        
        
        
        return ans;
    }
    
    // Now I have got an answer represented as a vector [a0, .. , an], where the number is
    a0 + a1 * 10 +  ... + an * 10^n. I had abandoned the constraint of each number as a single digit. I restore it at the end.
    
    vector<int> clean_ans(const vector<int> &t)
    {
        vector<int> ans(t.size());    
        
        int a = 0;
        
        for (int i = 0 ; i < ans.size() ; i++)
        {
            a += t[i];
            
            ans[i] = a % 10;
            a = int(a/10);
            
        }
        
        
        while(a > 0)
        {
            ans.push_back(a % 10);
            a = int(a/10);
        }
        
        
        
        while (ans[ans.size()-1] == 0)
            ans.pop_back();
        
        return ans;
    
    }
    
    
    
    
    
    vector<int> fibonacciModified(int t1, int t2, int n) 
    {      
        vector<int> vect_t1 = number_to_vector(t1);
        vector<int> vect_t2 = number_to_vector(t2);
        
        if (n == 1)
            return vect_t1;
        if (n == 2)
            return vect_t2;
            
       
        // Compute Fibonacci numbers using the conventional dynamic programming method
        vector<int> vect_t3;
        
        for (int i = 2; i < n ; i++)
        {
            
            
            vect_t3 =  clean_ans(my_sum_raw( vect_t1 , my_square_2( vect_t2 ) ));
           
            vect_t1 = vect_t2;
            vect_t2 = vect_t3;
        }
    
        return vect_t3;
    }
    // At the end it is strange that the answer is too big to be int, but the return type of the  function was int. I changed it to a vector. Therefore I had to change the print function too
    int main()
    {
        ofstream fout(getenv("OUTPUT_PATH"));
    
        string first_multiple_input_temp;
        getline(cin, first_multiple_input_temp);
    
        vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
    
        int t1 = stoi(first_multiple_input[0]);
    
        int t2 = stoi(first_multiple_input[1]);
    
        int n = stoi(first_multiple_input[2]);
    
        vector<int> result = fibonacciModified(t1, t2, n);
    
        for (int i = result.size()-1 ; i >= 0 ; i--)
        {
            fout << result[i];
        }
        fout << "\n";
    
    
        fout.close();
    
        return 0;
    }