Sort by

recency

|

726 Discussions

|

  • + 0 comments

    Despite having recurrence relation, I have no doubt that this doesn't belong to DP category.

    You can use C++'s external library: (gmpxx.h) Note that "Online Judges" don't know about the external library, and if you submit the below code, you are definitely going to fail.

    Efficiency and speed are significantly faster than those from Java and Python's built-in libraries. It's worth to learn it because the problem only requires you to implement super simple recurrence relation.

    include

    include

    using namespace std;

    int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    mpz_class t1, t2, ret; 
    int nth; 
    cin >> t1 >> t2 >> nth; 
    
    for(int i = 2; i < nth; ++i) {
        ret = t1 + t2 * t2; 
        t1 = t2; t2 = ret; 
    }
    
    cout << ret << endl; 
    return 0; 
    

    }

  • + 0 comments

    having to change the printout length because the template assumes string conversion is just cumbersome, the template for python isn't that great

  • + 0 comments

    Here is my Python solution!

    def fibonacciModified(t1, t2, n):
        sys.set_int_max_str_digits(99999999)
        first = t1
        second = t2
        for i in range(n - 2):
            current = first + second ** 2
            first = second
            second = current
        return current
    
  • + 0 comments

    Java:

    public static BigInteger fibonacciModified(int t1, int t2, int n) {
        BigInteger n2 = BigInteger.valueOf(t1), n1 = BigInteger.valueOf(t2), fib = BigInteger.ZERO;
        if (n == 1) {
            return n2;
        }
        if (n == 2) {
            return n1;
        }
        for (int i=3; i <=n; ++i) {
            fib = n2.add(n1.multiply(n1));
            n2 = n1;
            n1 = fib;
        }
        return fib;
    }
    
  • + 1 comment
    def fibonacciModified(t1, t2, n):
        for _ in range(n-2):
            t1=t1+t2**2
            t1^=t2
            t2^=t1
            t1^=t2
        return t2