Sort by

recency

|

141 Discussions

|

  • + 0 comments

    My C++ sollution using fast matrix exponentiation at compile time.

    #include <array>
    #include <bitset>
    #include <cstdint>
    #include <iostream>
    
    using u32 = std::uint32_t;
    using u64 = std::uint64_t;
    
    struct u64_p
    {
        constexpr static u64 P{ 1000000007u };
    
        constexpr u64_p() = default;
        constexpr u64_p( u64 value ) : m_value { value % P }{}
        constexpr u64_p operator+( u64_p r ) const{ return { m_value + r.m_value }; }
        constexpr u64_p operator*( u64_p r ) const{ return { m_value * r.m_value }; }
    
        u64 m_value{};
    };
    
    constexpr u64_p operator""_p( unsigned long long number )
    {
        return u64_p{ static_cast<u64>( number ) };
    }
    
    struct Vec
    {
        u64_p m_a{};
        u64_p m_b{};
    };
    
    struct Matrix
    {
        constexpr Matrix operator*( Matrix r ) const
        { 
            return 
            { m_a * r.m_a + m_b * r.m_c
            , m_a * r.m_b + m_b * r.m_d
            , m_c * r.m_a + m_d * r.m_c
            , m_c * r.m_b + m_d * r.m_d };
        }
        constexpr Vec operator*( Vec r ) const
        { 
            return 
            { m_a * r.m_a + m_b * r.m_b
            , m_c * r.m_a + m_d * r.m_b };
        }
    
        u64_p m_a{};
        u64_p m_b{};
        u64_p m_c{};
        u64_p m_d{};
    };
    
    constexpr Matrix IDENTITY{ 1_p, 0_p, 0_p, 1_p };
    constexpr Matrix LAMBDA{ 1_p, 1_p, 1_p, 0_p };
    constexpr std::size_t U32_BITCOUNT{ 32u };
    
    constexpr auto LAMBDA_POWERS_2_POW_I{
        []() constexpr
        {
            std::array<Matrix, U32_BITCOUNT> matrices{};
            matrices[ 0u ] = LAMBDA;
            for ( std::size_t i{ 1u }; i < U32_BITCOUNT; ++i )
            {
                matrices[ i ] = matrices[ i - 1u ] * matrices[ i - 1u ];
            }
            return matrices;
        }()
    };
    
    Matrix pow_lambda( const u32 exponent )
    {
        const std::bitset<U32_BITCOUNT> bits{ exponent };
        Matrix result{ IDENTITY };
        for( std::size_t i{}; i < U32_BITCOUNT; i++ )
        {
            if( bits.test( i ) )
            {
                result = result * LAMBDA_POWERS_2_POW_I[ i ];
            }
        }
        return result;
    }
    
    u64 solve( const u64 a, const u64 b, const u32 n ) {
        return ( pow_lambda( n - 1 ) * Vec{ b, a } ).m_a.m_value;
    }
    
    int main()
    {
        std::size_t t{};
        std::cin >> t;
        while( t-- )
        {
            u64 a{}, b{};
            u32 n{};
            std::cin >> a >> b >> n;
            std::cout << solve( a, b, n ) << std::endl;
        }
    }
    
  • + 1 comment

    simple recurrance relation formula

    maths

    def solve(a, b, n):
        x = (1+sqrt(5))/2
        y = (1-sqrt(5))/2
    
        first = ((a/2)*(1-(1/sqrt(5))) + (b/sqrt(5))) * (x)**n
    
        second = ((a/2)*(1+(1/sqrt(5))) - (b/sqrt(5))) * (y)**n
    		
    		return int((first+second).real)
    # 		why this shows me wrong but answer is correct
    
        ans = first + second
        return str(int(ans.real))
        
    
  • + 0 comments

    Simple and efficient algorithm for finding Fibonacci numbers. Fascinating sequence that unlocks nature's hidden math! Cricbet99 com

  • + 0 comments

    My code works only for the first case, in larger cases I get an error. Can you give me some advice on how to proceed?

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'solve' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts following parameters:
    #  1. INTEGER a
    #  2. INTEGER b
    #  3. INTEGER n
    #
    
    def solve(a, b, n):
        # First Fibonacci number is a
        if n == 1:
            return b
        # Second Fibonacci number is b
        elif n == 2:
            return a+b
        else:
            return (solve(a,b,n-1) + solve(a,b,n-2))%(10**9+7) 
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        t = int(input().strip())
    
        for t_itr in range(t):
            first_multiple_input = input().rstrip().split()
    
            a = int(first_multiple_input[0])
    
            b = int(first_multiple_input[1])
    
            n = int(first_multiple_input[2])
    
            result = solve(a, b, n)
    
            fptr.write(str(result) + '\n')
    
        fptr.close()
    
  • + 0 comments

    Tagging the problem as easy seems like an intentional troll.

    Here's my Python3 solution, using matrix exponentiation by repeated squaring:

    from functools import reduce
    
    MOD = 10**9 + 7
    
    # return matrix product A*B^T
    def mprod(A,B):
        return [[sum(p*q for p,q in zip(r,c)) for c in B] for r in A]
    
    def solve(a,b,n):
        b_str = '1' + format(n,'b')[::-1]
        pows = [[[1,0],[0,1]],
                [[1,1],[1,0]]]
        for _ in range(len(b_str)-1):
            pows.append(mprod(pows[-1],pows[-1]))
        F = reduce(mprod,[M for M,c in zip(pows,b_str) if c=='1'])
        k1,k2 = F[1]
        return (k1*b + k2*a)%MOD