dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def Max_Sum ( arr1 , arr2 , n ) :
    dp = [ [ 0 for i in range ( 2 ) ] for j in range ( n ) ]
    for i in range ( n ) :
        if ( i == 0 ) :
            dp [ i ] [ 0 ] = arr1 [ i ]
            dp [ i ] [ 1 ] = arr2 [ i ]
            continue
        else :
            dp [ i ] [ 0 ] = max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] + arr1 [ i ] )
            dp [ i ] [ 1 ] = max ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] + arr2 [ i ] )
    return max ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] )
if __name__ == ' _ _ main _ _ ' :
    arr1 = [ 9 , 3 , 5 , 7 , 3 ]
    arr2 = [ 5 , 8 , 1 , 4 , 5 ]
    n = len ( arr1 )
    print ( Max_Sum ( arr1 , arr2 , n ) )

Index/key variables: i
Iterator variables: i
Python program:
import numpy as np
import sys
RODS = 3
N = 3
dp = np . zeros ( ( N + 1 , RODS + 1 , RODS + 1 ) ) ;
def initialize ( ) :
    for i in range ( N + 1 ) :
        for j in range ( 1 , RODS + 1 ) :
            for k in range ( 1 , RODS + 1 ) :
                dp [ i ] [ j ] [ k ] = sys . maxsize ;
def mincost ( idx , src , dest , costs ) :
    if ( idx > N ) :
        return 0 ;
    if ( dp [ idx ] [ src ] [ dest ] != sys . maxsize ) :
        return dp [ idx ] [ src ] [ dest ] ;
    rem = 6 - ( src + dest ) ;
    ans = sys . maxsize ;
    case1 = costs [ src - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , rem , costs ) + mincost ( idx + 1 , rem , dest , costs ) ;
    case2 = ( costs [ src - 1 ] [ rem - 1 ] + mincost ( idx + 1 , src , dest , costs ) + mincost ( idx + 1 , dest , src , costs ) + costs [ rem - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , dest , costs ) ) ;
    ans = min ( case1 , case2 ) ;
    dp [ idx ] [ src ] [ dest ] = ans ;
    return ans ;
if __name__ == " _ _ main _ _ " :
    costs = [ [ 0 , 1 , 2 ] , [ 2 , 0 , 1 ] , [ 3 , 2 , 0 ] ] ;
    initialize ( ) ;
    print ( mincost ( 1 , 1 , 3 , costs ) ) ;

Index/key variables: dest, i, idx, j, k, src
Iterator variables: i, j, k
Python program:
C = 5
R = 3
INT_MAX = 10000000
table = [ [ 0 for i in range ( C ) ] for j in range ( R ) ]
visited = [ [ 0 for i in range ( C ) ] for j in range ( R ) ]
def min ( p , q , r , s ) :
    if ( p < q ) :
        temp1 = p
    else :
        temp1 = q
    if ( r < s ) :
        temp2 = r
    else :
        temp2 = s
    if ( temp1 < temp2 ) :
        return temp1
    return temp2
def Distance ( arr , i , j ) :
    if ( i >= R or j >= C or i < 0 or j < 0 ) :
        return INT_MAX
    elif ( arr [ i ] [ j ] == 0 ) :
        table [ i ] [ j ] = INT_MAX
        return INT_MAX
    elif ( arr [ i ] [ j ] == 2 ) :
        table [ i ] [ j ] = 0
        return 0
    elif ( visited [ i ] [ j ] ) :
        return INT_MAX
    else :
        visited [ i ] [ j ] = 1
        temp1 = Distance ( arr , i + 1 , j )
        temp2 = Distance ( arr , i - 1 , j )
        temp3 = Distance ( arr , i , j + 1 )
        temp4 = Distance ( arr , i , j - 1 )
        min_value = 1 + min ( temp1 , temp2 , temp3 , temp4 )
        if table [ i ] [ j ] > 0 and table [ i ] [ j ] < INT_MAX :
            if min_value < table [ i ] [ j ] :
                table [ i ] [ j ] = min_value
        else :
            table [ i ] [ j ] = min_value
        visited [ i ] [ j ] = 0
    return table [ i ] [ j ]
def minTime ( arr ) :
    max = 0
    for i in range ( R ) :
        for j in range ( C ) :
            if ( arr [ i ] [ j ] == 1 ) :
                Distance ( arr , i , j )
    for i in range ( R ) :
        for j in range ( C ) :
            if ( arr [ i ] [ j ] == 1 and table [ i ] [ j ] > max ) :
                max = table [ i ] [ j ]
    if ( max < INT_MAX ) :
        return max
    return - 1
if __name__ == ' _ _ main _ _ ' :
    arr = [ [ 2 , 1 , 0 , 2 , 1 ] , [ 0 , 0 , 1 , 2 , 1 ] , [ 1 , 0 , 0 , 2 , 1 ] ]
    print ( minTime ( arr ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
maxLen = 10
dp = [ 0 ] * maxLen ;
visit = [ 0 ] * maxLen ;
def maxSum ( arr , i , n , k ) :
    if ( i >= n ) :
        return 0 ;
    if ( visit [ i ] ) :
        return dp [ i ] ;
    visit [ i ] = 1 ;
    tot = 0 ;
    dp [ i ] = maxSum ( arr , i + 1 , n , k ) ;
    j = i
    while ( j < i + k and j < n ) :
        tot += arr [ j ] ;
        dp [ i ] = max ( dp [ i ] , tot + maxSum ( arr , j + 2 , n , k ) ) ;
        j += 1
    return dp [ i ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ - 1 , 2 , - 3 , 4 , 5 ] ;
    k = 2 ;
    n = len ( arr ) ;
    print ( maxSum ( arr , 0 , n , k ) ) ;

Index/key variables: i, j
Accumulator variables: tot
Python program:
import numpy as np
SIZE = 3 ;
N = 3 ;
def minSum ( A , B , C , i , n , curr , dp ) :
    if ( n <= 0 ) :
        return 0 ;
    if ( dp [ n ] [ curr ] != - 1 ) :
        return dp [ n ] [ curr ] ;
    if ( curr == 0 ) :
        dp [ n ] [ curr ] = min ( B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ;
        return dp [ n ] [ curr ]
    if ( curr == 1 ) :
        dp [ n ] [ curr ] = min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ;
        return dp [ n ] [ curr ]
    dp [ n ] [ curr ] = min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) ) ;
    return dp [ n ] [ curr ]
if __name__ == " _ _ main _ _ " :
    A = [ 1 , 50 , 1 ] ;
    B = [ 50 , 50 , 50 ] ;
    C = [ 50 , 50 , 50 ] ;
    dp = np . zeros ( ( SIZE , N ) ) ;
    for i in range ( SIZE ) :
        for j in range ( N ) :
            dp [ i ] [ j ] = - 1 ;
    print ( min ( A [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 0 , dp ) , min ( B [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 1 , dp ) , C [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 2 , dp ) ) ) ) ;

Index/key variables: curr, i, j, n
Iterator variables: i, j
Python program:
import numpy as np
n = 3
dp = np . zeros ( ( n , n , 2 ) ) ;
v = np . zeros ( ( n , n , 2 ) ) ;
def max_three ( a , b , c ) :
    m = a ;
    if ( m < b ) :
        m = b ;
    if ( m < c ) :
        m = c ;
    return m ;
def maxScore ( arr , i , j , s ) :
    if ( i > n - 1 or i < 0 or j > n - 1 ) :
        return 0 ;
    if ( i == 0 and j == n - 1 ) :
        return arr [ i ] [ j ] ;
    if ( v [ i ] [ j ] [ s ] ) :
        return dp [ i ] [ j ] [ s ] ;
    v [ i ] [ j ] [ s ] = 1 ;
    if ( not bool ( s ) ) :
        dp [ i ] [ j ] [ s ] = arr [ i ] [ j ] + max_three ( maxScore ( arr , i + 1 , j , s ) , maxScore ( arr , i , j + 1 , s ) , maxScore ( arr , i - 1 , j , not bool ( s ) ) ) ;
    else :
        dp [ i ] [ j ] [ s ] = arr [ i ] [ j ] + max ( maxScore ( arr , i - 1 , j , s ) , maxScore ( arr , i , j + 1 , s ) ) ;
    return dp [ i ] [ j ] [ s ] ;
if __name__ == " _ _ main _ _ " :
    arr = [ [ 1 , 1 , 1 ] , [ 1 , 5 , 1 ] , [ 1 , 1 , 1 ] , ] ;
    print ( maxScore ( arr , 0 , 0 , 0 ) ) ;

Index/key variables: i, j, s
Python program:
import numpy as np
def MaximumMarks ( marksarr , timearr , h , n , p ) :
    no_of_topics = n + 1 ;
    total_time = h + 1 ;
    T = np . zeros ( ( no_of_topics , total_time ) ) ;
    for i in range ( no_of_topics ) :
        T [ i ] [ 0 ] = 0 ;
    for j in range ( total_time ) :
        T [ 0 ] [ j ] = 0 ;
    for i in range ( 1 , no_of_topics ) :
        for j in range ( 1 , total_time ) :
            if ( j < timearr [ i ] ) :
                T [ i ] [ j ] = T [ i - 1 ] [ j ] ;
            else :
                T [ i ] [ j ] = max ( marksarr [ i ] + T [ i - 1 ] [ j - timearr [ i ] ] , T [ i - 1 ] [ j ] ) ;
    i = no_of_topics - 1 ; j = total_time - 1 ;
    sum = 0 ;
    while ( i > 0 and j > 0 ) :
        if ( T [ i ] [ j ] == T [ i - 1 ] [ j ] ) :
            i -= 1 ;
        else :
            sum += timearr [ i ] ;
            j -= timearr [ i ] ;
            i -= 1 ;
    marks = T [ no_of_topics - 1 ] [ total_time - 1 ] ;
    if ( marks < p ) :
        return - 1 ;
    return sum ;
if __name__ == " _ _ main _ _ " :
    n = 4 ; h = 10 ; p = 10 ;
    marksarr = [ 0 , 6 , 4 , 2 , 8 ] ;
    timearr = [ 0 , 4 , 6 , 2 , 7 ] ;
    print ( MaximumMarks ( marksarr , timearr , h , n , p ) ) ;

Index/key variables: i, j
Accumulator variables: sum
Iterator variables: i, j
Python program:
maxLen = 10
maskLen = 130
dp = [ [ 0 for i in range ( maskLen ) ] for i in range ( maxLen ) ]
v = [ [ False for i in range ( maskLen ) ] for i in range ( maxLen ) ]
def minSteps ( arr , i , mask , n ) :
    if ( i == n - 1 ) :
        return 0
    if ( i > n - 1 or i < 0 ) :
        return 9999999
    if ( ( mask >> i ) & 1 ) :
        return 9999999
    if ( v [ i ] [ mask ] == True ) :
        return dp [ i ] [ mask ]
    v [ i ] [ mask ] = True
    dp [ i ] [ mask ] = 1 + min ( minSteps ( arr , i - arr [ i ] , ( mask | ( 1 << i ) ) , n ) , minSteps ( arr , i + arr [ i ] , ( mask | ( 1 << i ) ) , n ) )
    return dp [ i ] [ mask ]
arr = [ 1 , 2 , 2 , 2 , 1 , 1 ]
n = len ( arr )
ans = minSteps ( arr , 0 , 0 , n )
if ( ans >= 9999999 ) :
    print ( - 1 )
else :
    print ( ans )

Index/key variables: i, mask
Python program:
def oSRec ( arr , i , j , Sum ) :
    if ( j == i + 1 ) :
        return max ( arr [ i ] , arr [ j ] )
    return max ( ( Sum - oSRec ( arr , i + 1 , j , Sum - arr [ i ] ) ) , ( Sum - oSRec ( arr , i , j - 1 , Sum - arr [ j ] ) ) )
def optimalStrategyOfGame ( arr , n ) :
    Sum = 0
    Sum = sum ( arr )
    return oSRec ( arr , 0 , n - 1 , Sum )
arr1 = [ 8 , 15 , 3 , 7 ]
n = len ( arr1 )
print ( optimalStrategyOfGame ( arr1 , n ) )
arr2 = [ 2 , 2 , 2 , 2 ]
n = len ( arr2 )
print ( optimalStrategyOfGame ( arr2 , n ) )
arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ]
n = len ( arr3 )
print ( optimalStrategyOfGame ( arr3 , n ) )

Index/key variables: i, j
Python program:
def ispower ( n ) :
    if ( n < 125 ) :
        return ( n == 1 or n == 5 or n == 25 )
    if ( n % 125 != 0 ) :
        return 0
    else :
        return ispower ( n // 125 )
def number ( s , i , j ) :
    ans = 0
    for x in range ( i , j ) :
        ans = ans * 2 + ( ord ( s [ x ] ) - ord ( '0' ) )
    return ans
def minCuts ( s , n ) :
    dp = [ n + 1 for i in range ( n + 1 ) ]
    dp [ 0 ] = 0 ;
    for i in range ( 1 , n + 1 ) :
        if ( s [ i - 1 ] == '0' ) :
            continue
        for j in range ( i ) :
            if ( s [ j ] == '0' ) :
                continue
            num = number ( s , j , i )
            if ( not ispower ( num ) ) :
                continue
            dp [ i ] = min ( dp [ i ] , dp [ j ] + 1 )
    if dp [ n ] < n + 1 :
        return dp [ n ]
    else :
        return - 1
if __name__ == " _ _ main _ _ " :
    s = "101101101"
    n = len ( s )
    print ( minCuts ( s , n ) )

Index/key variables: i, j, n, x
Iterator variables: i, j, x
Python program:
def MinOfCubed ( k ) :
    if ( k < 8 ) :
        return k ;
    res = k ;
    for i in range ( 1 , k + 1 ) :
        if ( ( i * i * i ) > k ) :
            return res ;
        res = min ( res , MinOfCubed ( k - ( i * i * i ) ) + 1 ) ;
    return res ;
num = 15 ;
print ( MinOfCubed ( num ) ) ;

Iterator variables: i
Python program:
import sys
def MinOfCubedDP ( k ) :
    DP = [ 0 ] * ( k + 1 ) ;
    j = 1 ;
    t = 1 ;
    DP [ 0 ] = 0 ;
    for i in range ( 1 , k + 1 ) :
        DP [ i ] = sys . maxsize ;
        while ( j <= i ) :
            if ( j == i ) :
                DP [ i ] = 1 ;
            elif ( DP [ i ] > DP [ i - j ] ) :
                DP [ i ] = DP [ i - j ] + 1 ;
            t += 1 ;
            j = t * t * t ;
        t = j = 1 ;
    return DP [ k ] ;
num = 15 ;
print ( MinOfCubedDP ( num ) ) ;

Index/key variables: i, k
Accumulator variables: t
Iterator variables: i
Python program:
def maxSum ( a , n ) :
    ans = 0
    arr = [ 0 ] * ( n + 1 )
    for i in range ( 1 , n + 1 ) :
        arr [ i ] = a [ i - 1 ]
    dp = [ [ 0 for i in range ( 3 ) ] for j in range ( n + 1 ) ]
    for i in range ( 0 , n + 1 ) :
        dp [ i ] [ 0 ] = max ( arr [ i ] , dp [ i - 1 ] [ 0 ] + arr [ i ] )
        dp [ i ] [ 1 ] = max ( 0 , dp [ i - 1 ] [ 0 ] ) - arr [ i ]
        if i >= 2 :
            dp [ i ] [ 1 ] = max ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 1 ] + arr [ i ] )
        if i >= 2 :
            dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] - arr [ i ]
        if i >= 3 :
            dp [ i ] [ 2 ] = max ( dp [ i ] [ 2 ] , dp [ i - 1 ] [ 2 ] + arr [ i ] )
        ans = max ( ans , dp [ i ] [ 0 ] )
        ans = max ( ans , dp [ i ] [ 1 ] )
        ans = max ( ans , dp [ i ] [ 2 ] )
    return ans
if __name__ == " _ _ main _ _ " :
    arr = [ - 5 , 3 , 2 , 7 , - 8 , 3 , 7 , - 9 , 10 , 12 , - 6 ]
    n = len ( arr )
    print ( maxSum ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
def maxSum ( arr , k , n ) :
    if ( n == 0 ) :
        return 0 ;
    if ( n == 1 ) :
        return arr [ 0 ] ;
    if ( n == 2 ) :
        return max ( arr [ 0 ] , arr [ 1 ] ) ;
    dp = [ 0 ] * n ;
    dp [ 0 ] = arr [ 0 ] ;
    for i in range ( 1 , k + 1 ) :
        dp [ i ] = max ( arr [ i ] , dp [ i - 1 ] ) ;
    for i in range ( k + 1 , n ) :
        dp [ i ] = max ( arr [ i ] , dp [ i - ( k + 1 ) ] + arr [ i ] ) ;
    max_element = max ( dp ) ;
    return max_element ;
if __name__ == " _ _ main _ _ " :
    arr = [ 6 , 7 , 1 , 3 , 8 , 2 , 4 ] ;
    n = len ( arr ) ;
    k = 2 ;
    print ( maxSum ( arr , k , n ) ) ;

Index/key variables: i
Iterator variables: i
Python program:
def MinimumCost ( a , n , x ) :
    for i in range ( 1 , n , 1 ) :
        a [ i ] = min ( a [ i ] , 2 * a [ i - 1 ] )
    ind = 0
    sum = 0
    while ( x ) :
        if ( x & 1 ) :
            sum += a [ ind ]
        ind += 1
        x = x >> 1
    return sum
if __name__ == ' _ _ main _ _ ' :
    a = [ 20 , 50 , 60 , 90 ]
    x = 7
    n = len ( a )
    print ( MinimumCost ( a , n , x ) )

Index/key variables: i, ind
Accumulator variables: sum
Iterator variables: i
Python program:
def countWays ( n , l , r ) :
    tL , tR = l , r
    L = [ 0 for i in range ( 2 ) ]
    R = [ 0 for i in range ( 2 ) ]
    L [ l % 2 ] = l
    R [ r % 2 ] = r
    l += 1
    r -= 1
    if ( l <= tR and r >= tL ) :
        L [ l % 2 ] , R [ r % 2 ] = l , r
    cnt0 , cnt1 = 0 , 0
    if ( R [ 0 ] and L [ 0 ] ) :
        cnt0 = ( R [ 0 ] - L [ 0 ] ) // 2 + 1
    if ( R [ 1 ] and L [ 1 ] ) :
        cnt1 = ( R [ 1 ] - L [ 1 ] ) // 2 + 1
    dp = [ [ 0 for i in range ( 2 ) ] for i in range ( n + 1 ) ]
    dp [ 1 ] [ 0 ] = cnt0
    dp [ 1 ] [ 1 ] = cnt1
    for i in range ( 2 , n + 1 ) :
        dp [ i ] [ 0 ] = ( cnt0 * dp [ i - 1 ] [ 0 ] + cnt1 * dp [ i - 1 ] [ 1 ] )
        dp [ i ] [ 1 ] = ( cnt0 * dp [ i - 1 ] [ 1 ] + cnt1 * dp [ i - 1 ] [ 0 ] )
    return dp [ n ] [ 0 ]
n , l , r = 2 , 1 , 3
print ( countWays ( n , l , r ) )

Index/key variables: i, n
Iterator variables: i
Python program:
M = 1001 ;
MOD = 998244353 ;
dp = [ [ - 1 ] * M ] * M
def solve ( idx , diff , N , M , K ) :
    if ( idx > N ) :
        if ( diff == K ) :
            return 1
        return 0
    if ( dp [ idx ] [ diff ] != - 1 ) :
        return dp [ idx ] ;
    ans = solve ( idx + 1 , diff , N , M , K ) ;
    ans += ( M - 1 ) * solve ( idx + 1 , diff + 1 , N , M , K ) ;
    dp [ idx ] [ diff ] = ans % MOD ;
    return dp [ idx ] [ diff ]
if __name__ == " _ _ main _ _ " :
    N = 3
    M = 3
    K = 0
    print ( M * solve ( 2 , 0 , N , M , K ) )

Index/key variables: diff, idx
Python program:
N = 3
def maxPathSum ( tri ) :
    ans = 0 ;
    for i in range ( N - 2 , - 1 , - 1 ) :
        for j in range ( 0 , N - i ) :
            if ( j - 1 >= 0 ) :
                tri [ i ] [ j ] += max ( tri [ i + 1 ] [ j ] , tri [ i + 1 ] [ j - 1 ] ) ;
            else :
                tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ;
            ans = max ( ans , tri [ i ] [ j ] ) ;
    return ans
tri = [ [ 1 , 5 , 3 ] , [ 4 , 8 , 0 ] , [ 1 , 0 , 0 ] ]
print ( maxPathSum ( tri ) )

Index/key variables: i, j
Iterator variables: i, j
Python program:
from math import log2
def subsetXOR ( arr , n , K ) :
    max_ele = arr [ 0 ]
    for i in range ( 1 , n ) :
        if ( arr [ i ] > max_ele ) :
            max_ele = arr [ i ]
    m = ( 1 << int ( log2 ( max_ele ) + 1 ) ) - 1
    dp = [ [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ] for k in range ( n + 1 ) ]
    for i in range ( n + 1 ) :
        for j in range ( m + 1 ) :
            for k in range ( n + 1 ) :
                dp [ i ] [ j ] [ k ] = 0
    for i in range ( n + 1 ) :
        dp [ i ] [ 0 ] [ 0 ] = 1
    for i in range ( 1 , n + 1 ) :
        for j in range ( m + 1 ) :
            for k in range ( n + 1 ) :
                dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ]
                if ( k != 0 ) :
                    dp [ i ] [ j ] [ k ] += k * dp [ i - 1 ] [ j ^ arr [ i - 1 ] ] [ k - 1 ]
    ans = 0
    for i in range ( 1 , n + 1 ) :
        ans += dp [ n ] [ K ] [ i ]
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 2 , 3 ]
    k = 1
    n = len ( arr )
    print ( subsetXOR ( arr , n , k ) )

Index/key variables: K, i, j, k, n
Accumulator variables: ans
Iterator variables: i, j, k
Python program:
M = 20
dp = [ ]
d , K = None , None
def count ( pos , cnt , tight , nonz , num : list ) :
    if pos == len ( num ) :
        if cnt == K :
            return 1
        return 0
    if dp [ pos ] [ cnt ] [ tight ] [ nonz ] != - 1 :
        return dp [ pos ] [ cnt ] [ tight ] [ nonz ]
    ans = 0
    limit = 9 if tight else num [ pos ]
    for dig in range ( limit + 1 ) :
        currCnt = cnt
        if dig == d :
            if d != 0 or not d and nonz :
                currCnt += 1
        currTight = tight
        if dig < num [ pos ] :
            currTight = 1
        ans += count ( pos + 1 , currCnt , currTight , ( nonz or dig != 0 ) , num )
    dp [ pos ] [ cnt ] [ tight ] [ nonz ] = ans
    return dp [ pos ] [ cnt ] [ tight ] [ nonz ]
def solve ( x ) :
    global dp , K , d
    num = [ ]
    while x :
        num . append ( x % 10 )
        x //= 10
    num . reverse ( )
    dp = [ [ [ [ - 1 , - 1 ] for i in range ( 2 ) ] for j in range ( M ) ] for k in range ( M ) ]
    return count ( 0 , 0 , 0 , 0 , num )
if __name__ == " _ _ main _ _ " :
    L = 11
    R = 100
    d = 2
    K = 1
    print ( solve ( R ) - solve ( L - 1 ) )

Index/key variables: cnt, nonz, pos, tight
Accumulator variables: ans, currCnt, num, x
Iterator variables: dig
Python program:
r = 4
col = 5
def findMinSteps ( mat , n , m , dp , vis ) :
    if ( n == 0 or m == 0 or n == ( r - 1 ) or m == ( col - 1 ) ) :
        return 0
    if ( dp [ n ] [ m ] != - 1 ) :
        return dp [ n ] [ m ]
    vis [ n ] [ m ] = True
    ans1 , ans2 , ans3 , ans4 = 10 ** 9 , 10 ** 9 , 10 ** 9 , 10 ** 9
    if ( mat [ n - 1 ] [ m ] == 0 ) :
        if ( vis [ n - 1 ] [ m ] == False ) :
            ans1 = 1 + findMinSteps ( mat , n - 1 , m , dp , vis )
    if ( mat [ n ] [ m + 1 ] == 0 ) :
        if ( vis [ n ] [ m + 1 ] == False ) :
            ans2 = 1 + findMinSteps ( mat , n , m + 1 , dp , vis )
    if ( mat [ n ] [ m - 1 ] == 0 ) :
        if ( vis [ n ] [ m - 1 ] == False ) :
            ans3 = 1 + findMinSteps ( mat , n , m - 1 , dp , vis )
    if ( mat [ n + 1 ] [ m ] == 0 ) :
        if ( vis [ n + 1 ] [ m ] == False ) :
            ans4 = 1 + findMinSteps ( mat , n + 1 , m , dp , vis )
    dp [ n ] [ m ] = min ( ans1 , min ( ans2 , min ( ans3 , ans4 ) ) )
    return dp [ n ] [ m ]
def minimumSteps ( mat , n , m ) :
    twox = - 1
    twoy = - 1
    for i in range ( n ) :
        for j in range ( m ) :
            if ( mat [ i ] [ j ] == 2 ) :
                twox = i
                twoy = j
                break
        if ( twox != - 1 ) :
            break
    dp = [ [ - 1 for i in range ( col ) ] for i in range ( r ) ]
    vis = [ [ False for i in range ( col ) ] for i in range ( r ) ]
    res = findMinSteps ( mat , twox , twoy , dp , vis )
    if ( res >= 10 ** 9 ) :
        return - 1
    else :
        return res
mat = [ [ 1 , 1 , 1 , 0 , 1 ] , [ 1 , 0 , 2 , 0 , 1 ] , [ 0 , 0 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 0 ] ]
print ( minimumSteps ( mat , r , col ) )

Index/key variables: i, j, m, n
Iterator variables: i, j
Python program:
n = 3
dp = [ [ 0 for i in range ( n ) ] for j in range ( n ) ]
def answerQueries ( queries , q ) :
    for i in range ( q ) :
        x = queries [ i ] [ 0 ]
        x -= 1
        y = queries [ i ] [ 1 ]
        y -= 1
        print ( dp [ x ] [ y ] )
def pre_compute ( a ) :
    if a [ 0 ] [ 0 ] == ' a ' :
        dp [ 0 ] [ 0 ] = 0
    else :
        dp [ 0 ] [ 0 ] = 1
    for row in range ( n ) :
        for col in range ( n ) :
            if ( row != 0 or col != 0 ) :
                dp [ row ] [ col ] = 9999
            if ( row != 0 ) :
                dp [ row ] [ col ] = min ( dp [ row ] [ col ] , dp [ row - 1 ] [ col ] )
            if ( col != 0 ) :
                dp [ row ] [ col ] = min ( dp [ row ] [ col ] , dp [ row ] [ col - 1 ] )
            if ( a [ row ] [ col ] != ' a ' and ( row != 0 or col != 0 ) ) :
                dp [ row ] [ col ] += 1
if __name__ == ' _ _ main _ _ ' :
    a = [ ( ' a ' , ' b ' , ' a ' ) , ( ' a ' , ' c ' , ' d ' ) , ( ' b ' , ' a ' , ' b ' ) ]
    queries = [ ( 1 , 3 ) , ( 3 , 3 ) ]
    q = 2
    pre_compute ( a )
    answerQueries ( queries , q )

Index/key variables: col, i, row, x, y
Iterator variables: col, i, row
Python program:
def squares ( i ) :
    if ( ( i & 1 ) == 1 ) :
        return int ( i / 4 ) * 2 + 1
    else :
        return int ( ( i - 1 ) / 4 ) * 2 + 2
def bishop_placements ( n , k ) :
    if ( k > 2 * n - 1 ) :
        return 0
    dp = [ [ 0 for i in range ( k + 1 ) ] for i in range ( n * 2 ) ]
    for i in range ( n * 2 ) :
        dp [ i ] [ 0 ] = 1
    dp [ 1 ] [ 1 ] = 1
    for i in range ( 2 , n * 2 , 1 ) :
        for j in range ( 1 , k + 1 , 1 ) :
            dp [ i ] [ j ] = ( dp [ i - 2 ] [ j ] + dp [ i - 2 ] [ j - 1 ] * ( squares ( i ) - j + 1 ) )
    ans = 0
    for i in range ( 0 , k + 1 , 1 ) :
        ans += ( dp [ n * 2 - 1 ] [ i ] * dp [ n * 2 - 2 ] [ k - i ] )
    return ans
if __name__ == ' _ _ main _ _ ' :
    n = 2
    k = 2
    ans = bishop_placements ( n , k )
    print ( ans )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
MAX = 10
F = [ [ - 1 for i in range ( MAX ) ] for j in range ( MAX ) ]
C = [ None ] * MAX
def noOfAssignments ( S , n , i , c_x ) :
    if F [ i ] [ c_x ] != - 1 :
        return F [ i ] [ c_x ]
    if i == n :
        F [ i ] [ c_x ] = not c_x
        return F [ i ] [ c_x ]
    c_y = C [ i ] - c_x
    if S [ i ] == ' ( ' :
        F [ i ] [ c_x ] = noOfAssignments ( S , n , i + 1 , c_x + 1 ) + noOfAssignments ( S , n , i + 1 , c_x )
        return F [ i ] [ c_x ]
    F [ i ] [ c_x ] = 0
    if c_x :
        F [ i ] [ c_x ] += noOfAssignments ( S , n , i + 1 , c_x - 1 )
    if c_y :
        F [ i ] [ c_x ] += noOfAssignments ( S , n , i + 1 , c_x )
    return F [ i ] [ c_x ]
if __name__ == " _ _ main _ _ " :
    S = " ( ) "
    n = len ( S )
    C [ 0 ] = 0
    for i in range ( 0 , n ) :
        if S [ i ] == ' ( ' :
            C [ i + 1 ] = C [ i ] + 1
        else :
            C [ i + 1 ] = C [ i ] - 1
    print ( noOfAssignments ( S , n , 0 , 0 ) )

Index/key variables: c_x, i
Iterator variables: i
Python program:
import sys
n = 3
def minFallingPathSum ( A ) :
    for R in range ( n - 2 , - 1 , - 1 ) :
        for C in range ( n ) :
            best = A [ R + 1 ] [ C ]
            if C > 0 :
                best = min ( best , A [ R + 1 ] [ C - 1 ] )
            if C + 1 < n :
                best = min ( best , A [ R + 1 ] [ C + 1 ] )
            A [ R ] [ C ] = A [ R ] [ C ] + best
    ans = sys . maxsize
    for i in range ( n ) :
        ans = min ( ans , A [ 0 ] [ i ] )
    return ans
if __name__ == " _ _ main _ _ " :
    A = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ]
    print ( minFallingPathSum ( A ) )

Index/key variables: C, R, i
Iterator variables: C, R, i