Sort by

recency

|

43 Discussions

|

  • + 0 comments

    what pre_all from editorial do?

  • + 0 comments

    What is wrong with the code?

    from functools import reduce
    
    _dp={}
    _dp_sum={}
    M=10**9+7
    def _build_dp_sum(arr):
        global _dp_sum
        _dp_partitial_sums=[[0 for _ in range(len(arr))] for _ in range(len(arr))]
        for i in range(len(arr)):
            _dp_partitial_sums[i][i]=arr[i]
        for i in range(len(arr)):
            for j in reversed(range(i)):
                _dp_partitial_sums[j][i]=_dp_partitial_sums[j+1][i]+arr[j]
        _dp_sum=[0]*len(arr)
        _dp_tmp=[0]*len(arr)
        for i in range(1, len(arr)):
            for j in range(i):
                print("tmp sum", i, j, i-j,_dp_partitial_sums[j+1][i])
                _dp_tmp[i]+=(i-j)*_dp_partitial_sums[j+1][i]
        _dp_sum[0]=0
        for i in range(1, len(arr)):
            _dp_sum[i]+=_dp_sum[i]
            for j in range(1, i):
                _dp_sum[i]+=_dp_partitial_sums[j][i]
            _dp_sum[i]+=arr[i]*sum(range(1, i))
            _dp_sum[i]+=_dp_partitial_sums[i][i]
        print(_dp_sum)
        print(_dp_tmp)
        print(_dp_partitial_sums)
        
    
    def _next_sum(arr):
        global _dp
        global _dp_sum
        _dp=[0]*len(arr)
        _dp[0]=arr[0]
        _build_dp_sum(arr)
        for i in range(1, len(arr)):
            _dp[i]=reduce(lambda s,j: s+_dp[j], range(i), 0)+_dp_sum[i]
            print(f"_dp {_dp}, _dp_sum[i] {_dp_sum[i]}, _dp[i] {_dp[i]}")
        return _dp[-1]
           
                
    def summingPieces(arr):
        _sum=_next_sum(arr)
        return _sum 
        # Write your code here
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        arr_count = int(input().strip())
    
        result = summingPieces(arr)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()
    
  • + 0 comments

    What is wrong with the code from editorial?

        _dp_sum[0]=0
        for i in range(1, len(arr)):
            _dp_sum[i]+=_dp_sum[i-1]
            for j in range(1, i):
                _dp_sum[i]+=_dp_partitial_sums[j][i]
            _dp_sum[i]+=arr[i]*sum(range(2, i+1))
            _dp_sum[i]+=_dp_partitial_sums[i][i]
    
  • + 0 comments

    I don't understand what do you mean contribution. Can you elaborate more?

    "Now, count contributions of each number in the 'pieces'. For example, in pair (1,3), 1 and 3 contribute twice. in pair (1,3,6), 1,2and3 contribute thrice."

    In addition, when n is large, wouldn't the second term of the artithmetic grow bigger than the first term and therefore yield negative result?

    | 2^n-1 | p+2^(y) - 2^0 | p+2^(y-1) - 2^1 | p+2^(y-2) - 2^2|...

  • + 0 comments

    😂 sometimes you gotta read the Editorial and keep moving. I'm just glad there are smart people out there!