• + 0 comments

    Tried doing it with strings. But, it TLE'd on 4 test cases! T_T

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define PI 3.1415926535897932384626
    #define pb push_back
    #define mp make_pair
    #define F first
    #define S second
    #define br '\n'
    #define FOR(i, a, b) for(int i=int(a);i<=int(b);++i)
    #define FORR(i, a, b) for(int i=int(a);i>=int(b);--i)
    #define TR(it, x) for(auto it = x.begin(); it != x.end(); it++)
    #define TRR(it, x) for(auto it = x.rbegin(); it != x.rend(); it++)
    #define deb(x) cout<<#x<<"-"<<x<<"\n"
    typedef pair<int, int> pii;
    typedef pair<float, float> pff;
    typedef vector<int> vi;
    typedef vector<float> vf;
    typedef vector<pii> vpii;
    typedef vector<vi> vvi; 
    typedef stack<int> si;
    typedef queue<int> qi;
    mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
    const int mod = 1e7;
    const int MAX = 1e8;
    
    /* ========== START HERE ========= */
    string multiply(string num1, string num2){
        int n1 = num1.size();
        int n2 = num2.size();
        if (n1 == 0 || n2 == 0)
            return "0";
        vector<int> result(n1 + n2, 0);
        int i_n1 = 0;
        int i_n2 = 0;
        for (int i = n1 - 1; i>=0; i--) {
            int carry = 0;
            int n1 = num1[i] - '0';
            i_n2 = 0;
            FORR(j,n2 - 1,0) {
                int n2 = num2[j] - '0';
                int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
                carry = sum / 10;
                result[i_n1 + i_n2] = sum % 10;
     
                i_n2++;
            }
            if (carry > 0)
                result[i_n1 + i_n2] += carry;
            i_n1++;
        }
        int i = result.size() - 1;
        while (i >= 0 && result[i] == 0)
            i--;
        if (i == -1)
            return "0";
        string s = "";
           
        while (i >= 0)
            s += std::to_string(result[i--]);
     
        return s;
    }
    
    string findSum(string str1, string str2){
        if (str1.length() > str2.length())
            swap(str1, str2);
        string str = "";
        int n1 = str1.length(), n2 = str2.length();
        reverse(str1.begin(), str1.end());
        reverse(str2.begin(), str2.end());
     
        int carry = 0;
        for (int i=0; i<n1; i++){
            int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
            str.push_back(sum%10 + '0');
            carry = sum/10;
        }
        FOR(i,n1,n2-1){
            int sum = ((str2[i]-'0')+carry);
            str.push_back(sum%10 + '0');
            carry = sum/10;
        }
        if (carry)
            str.push_back(carry+'0');
        reverse(str.begin(), str.end());
        return str;
    }
    
    string fibonacci_modified(string t1,string t2,int n){
        string dp[n+1];
        dp[1]=t1;
        dp[2]=t2;
        FOR(i,3,n){
            dp[i]=findSum(multiply(dp[i-1],dp[i-1]),dp[i-2]);
        }
        return dp[n];
    }
    
    
    void solve() {
        string t1,t2;
        int n;
        cin>>t1>>t2>>n;
        cout<<fibonacci_modified(t1,t2,n);
    }
    
    /* ========== THE MAIN ========= */
    
    int main() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        srand(chrono::high_resolution_clock::now().time_since_epoch().count());
        int t=1;
        //scanf("%d",&t);
        while(t--){
            solve();
        }
        return EXIT_SUCCESS;
    }