dhyuti-n/xlcost-variable-roles huggingface dataset:
Python program:
def FindXorSum ( arr , k , n ) :
    if ( n < k ) :
        return 0 ;
    x = [ 0 ] * n ;
    result = 0 ;
    for i in range ( n ) :
        if ( i > 0 ) :
            x [ i ] = x [ i - 1 ] ^ arr [ i ] ;
        else :
            x [ i ] = arr [ i ] ;
        if ( i >= k - 1 ) :
            sum = 0 ;
            sum = x [ i ] ;
            if ( i - k > - 1 ) :
                sum ^= x [ i - k ] ;
            result += sum ;
    return result ;
arr = [ 1 , 2 , 3 , 4 ] ;
n = 4 ; k = 2 ;
print ( FindXorSum ( arr , k , n ) ) ;

Index/key variables: i
Accumulator variables: result, sum
Iterator variables: i
Python program:
def reverse ( n ) :
    rev = 0 ;
    while ( n > 0 ) :
        d = n % 10 ;
        rev = rev * 10 + d ;
        n = n // 10 ;
    return rev ;
def isPalin ( n ) :
    return ( n == reverse ( n ) ) ;
def countDigits ( n ) :
    c = 0 ;
    while ( n > 0 ) :
        n = n // 10 ;
        c += 1 ;
    return c ;
def countPalinDigits ( arr , n ) :
    s = 0 ;
    for i in range ( n ) :
        if ( arr [ i ] < 10 or isPalin ( arr [ i ] ) ) :
            s += countDigits ( arr [ i ] ) ;
    return s ;
arr = [ 121 , 56 , 434 ] ;
n = len ( arr ) ;
print ( countPalinDigits ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: c, s
Iterator variables: i
Python program:
def reverse ( n ) :
    d = 0 ; s = 0 ;
    while ( n > 0 ) :
        d = n % 10 ;
        s = s * 10 + d ;
        n = n // 10 ;
    return s ;
def isPalin ( n ) :
    return n == reverse ( n ) ;
def sumOfArray ( arr , n ) :
    s = 0 ;
    for i in range ( n ) :
        if ( ( arr [ i ] > 10 ) and isPalin ( arr [ i ] ) ) :
            s += arr [ i ] ;
    return s ;
if __name__ == " _ _ main _ _ " :
    n = 6 ;
    arr = [ 12 , 313 , 11 , 44 , 9 , 1 ] ;
    print ( sumOfArray ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: s
Iterator variables: i
Python program:
from math import sqrt
MAX = 1000000
prime = [ 0 ] * ( MAX + 1 ) ;
def updatePrimes ( ) :
    for i in range ( 2 , MAX + 1 ) :
        prime [ i ] = 1 ;
    prime [ 0 ] = prime [ 1 ] = 0 ;
    for i in range ( 2 , int ( sqrt ( MAX ) + 1 ) ) :
        if ( prime [ i ] == 1 ) :
            for j in range ( i * i , MAX , i ) :
                prime [ j ] = 0 ;
    for i in range ( 1 , MAX ) :
        prime [ i ] += prime [ i - 1 ] ;
def getDifference ( l , r ) :
    total = r - l + 1 ;
    primes = prime [ r ] - prime [ l - 1 ] ;
    composites = total - primes ;
    return ( abs ( primes - composites ) ) ;
if __name__ == " _ _ main _ _ " :
    queries = [ [ 1 , 10 ] , [ 5 , 30 ] ] ;
    q = len ( queries ) ;
    updatePrimes ( ) ;
    for i in range ( q ) :
        print ( getDifference ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) )

Index/key variables: i, j, r
Iterator variables: i, j
Python program:
MAX = 1000000
Sieve = [ 0 ] * ( MAX + 1 )
def constructSieve ( ) :
    for i in range ( 2 , MAX + 1 ) :
        if ( Sieve [ i ] == 0 ) :
            for j in range ( 2 * i , MAX + 1 , i ) :
                temp = j ;
                while ( temp > 1 and temp % i == 0 ) :
                    Sieve [ j ] += 1 ;
                    temp = temp // i ;
def checkElements ( A , n , k ) :
    for i in range ( n ) :
        if ( Sieve [ A [ i ] ] == k ) :
            print ( " YES " ) ;
        else :
            print ( " NO " ) ;
if __name__ == " _ _ main _ _ " :
    constructSieve ( ) ;
    k = 3 ;
    A = [ 12 , 36 , 42 , 72 ] ;
    n = len ( A ) ;
    checkElements ( A , n , k ) ;

Index/key variables: i, j
Iterator variables: i, j
Python program:
def cntWays ( arr , n ) :
    if ( n == 1 ) :
        return 0 ;
    tot_sum = 0 ; sum = 0 ; ans = 0 ;
    for i in range ( 0 , n ) :
        tot_sum += arr [ i ] ;
    for i in range ( 0 , n - 1 ) :
        sum += arr [ i ] ;
        if ( sum == tot_sum / 2 ) :
            ans += 1 ;
    return ans ;
arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ;
n = len ( arr ) ;
print ( cntWays ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: ans, sum, tot_sum
Iterator variables: i
Python program:
MAX = 32
pow2 = [ 0 for i in range ( MAX ) ]
visited = [ False for i in range ( MAX ) ]
ans = [ ]
def power_2 ( ) :
    an = 1
    for i in range ( MAX ) :
        pow2 [ i ] = an
        an *= 2
def countSetBits ( x ) :
    setBits = 0
    while ( x != 0 ) :
        x = x & ( x - 1 )
        setBits += 1
    return setBits
def add ( num ) :
    point = 0
    value = 0
    for i in range ( MAX ) :
        if ( visited [ i ] ) :
            continue
        else :
            if ( num & 1 ) :
                value += ( 1 << i )
            num = num // 2
    ans . append ( value )
def solve ( n , k ) :
    ans . append ( k )
    countk = countSetBits ( k )
    if ( pow2 [ countk ] < n ) :
        print ( - 1 )
        return
    count = 0
    for i in range ( pow2 [ countk ] - 1 ) :
        add ( i )
        count += 1
        if ( count == n ) :
            break
    for i in range ( n ) :
        print ( ans [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    n = 3
    k = 5
    power_2 ( )
    solve ( n , k )

Index/key variables: countk, i
Accumulator variables: an, count, setBits, value
Iterator variables: i
Python program:
def countX ( a , b ) :
    if ( b > a ) :
        return 0
    elif ( a == b ) :
        return - 1
    else :
        x = a - b
        ans = 0
        i = 1
        while i * i <= x :
            if ( x % i == 0 ) :
                d1 = i
                d2 = b - 1
                if ( i * i != x ) :
                    d2 = x // i
                if ( d1 > b ) :
                    ans += 1
                if ( d2 > b ) :
                    ans += 1
            i += 1
        return ans
if __name__ == " _ _ main _ _ " :
    a = 21
    b = 5
    print ( countX ( a , b ) )

Accumulator variables: ans, i
Python program:
def sumPowersK ( n , k ) :
    sum = 0 ; num = 1 ;
    while ( num <= n ) :
        sum += num ;
        num *= k ;
    return sum ;
def getSum ( n , k ) :
    pwrK = sumPowersK ( n , k ) ;
    sumAll = ( n * ( n + 1 ) ) / 2 ;
    return ( sumAll - pwrK ) ;
if __name__ == " _ _ main _ _ " :
    n = 10 ; k = 3 ;
    print ( getSum ( n , k ) ) ;

Accumulator variables: num, sum
Python program:
from math import sqrt
def maxPeople ( p ) :
    tmp = 0 ; count = 0 ;
    for i in range ( 1 , int ( sqrt ( p ) ) + 1 ) :
        tmp = tmp + ( i * i ) ;
        if ( tmp <= p ) :
            count += 1 ;
        else :
            break ;
    return count ;
if __name__ == " _ _ main _ _ " :
    p = 14 ;
    print ( maxPeople ( p ) ) ;

Accumulator variables: count
Iterator variables: i
Python program:
kN = 1000000 ;
def maxPeople ( p ) :
    sums = [ 0 ] * kN ;
    sums [ 0 ] = 0 ;
    for i in range ( 1 , kN ) :
        sums [ i ] = ( i * i ) + sums [ i - 1 ] ;
    it = lower_bound ( sums , 0 , kN , p ) ;
    if ( it > p ) :
        it -= 1 ;
    return it ;
def lower_bound ( a , low , high , element ) :
    while ( low < high ) :
        middle = int ( low + ( high - low ) / 2 ) ;
        if ( element > a [ middle ] ) :
            low = middle + 1 ;
        else :
            high = middle ;
    return low ;
if __name__ == ' _ _ main _ _ ' :
    p = 14 ;
    print ( maxPeople ( p ) ) ;

Index/key variables: i, middle
Iterator variables: i
Python program:
def findIndex ( a , n , k ) :
    index = - 1
    max_ceil = - 10 ** 9
    for i in range ( n ) :
        a [ i ] = ( a [ i ] + k - 1 ) // k
    for i in range ( n ) :
        if ( a [ i ] >= max_ceil ) :
            max_ceil = a [ i ]
            index = i
    return index
arr = [ 31 , 12 , 25 , 27 , 32 , 19 ]
K = 5
N = len ( arr )
print ( findIndex ( arr , N , K ) )

Index/key variables: i
Iterator variables: i
Python program:
MAXN = 60
def solveQuery ( L , X ) :
    res = 0
    a = [ 0 for i in range ( MAXN ) ]
    b = [ 0 for i in range ( MAXN ) ]
    ref = X
    size_a = 0
    while ( ref > 0 ) :
        a [ size_a ] = ref % 2
        ref //= 2
        size_a += 1
    for i in range ( min ( size_a , L ) ) :
        if ( a [ i ] == 1 ) :
            b [ i ] = 0
        else :
            b [ i ] = 1
    for i in range ( min ( size_a , L ) , L ) :
        b [ i ] = 1
    b [ L - 1 ] = 1
    temp = 0
    p = 1
    for i in range ( L ) :
        temp += b [ i ] * p
        p *= 2
    res = temp ^ X
    return res
queries = [ [ 2 , 5 ] , [ 3 , 15 ] ]
q = len ( queries )
for i in range ( q ) :
    print ( solveQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) )

Index/key variables: i, size_a
Accumulator variables: p, ref, temp
Iterator variables: i
Python program:
def Count_Segment ( p , n ) :
    count = 0
    upto = [ False ] * ( n + 1 )
    for i in range ( 1 , n - 1 ) :
        if p [ i ] > p [ i - 1 ] and p [ i ] > p [ i + 1 ] :
            curr = p [ i ]
            j = i - 1
            while j >= 0 and p [ j ] < curr :
                upto [ p [ j ] ] = curr
                j -= 1
            j = i + 1
            while j < n and p [ j ] < curr :
                if upto [ curr - p [ j ] ] == curr :
                    count += 1
                j += 1
    return count
if __name__ == " _ _ main _ _ " :
    p = [ 3 , 4 , 1 , 5 , 2 ]
    n = len ( p )
    print ( Count_Segment ( p , n ) )

Index/key variables: i, j
Accumulator variables: count
Iterator variables: i
Python program:
def closetAND ( arr , n , k ) :
    ans = 10 ** 9
    for i in range ( n ) :
        X = arr [ i ]
        for j in range ( i , n ) :
            X &= arr [ j ]
            ans = min ( ans , abs ( k - X ) )
    return ans
arr = [ 4 , 7 , 10 ]
n = len ( arr )
k = 2 ;
print ( closetAND ( arr , n , k ) )

Index/key variables: i, j
Accumulator variables: X
Iterator variables: i, j
Python program:
from math import sqrt
def getPrime ( arr , n ) :
    max_val = arr [ 0 ]
    for i in range ( len ( arr ) ) :
        if ( arr [ i ] > max_val ) :
            max_val = arr [ i ]
    prime = [ True for i in range ( max_val + 1 ) ]
    prime [ 0 ] = False
    prime [ 1 ] = False
    for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 , 1 ) :
        if ( prime [ p ] == True ) :
            for i in range ( p * 2 , max_val + 1 , p ) :
                prime [ i ] = False
    maximum = - 1
    for i in range ( n ) :
        if ( prime [ arr [ i ] ] ) :
            maximum = max ( maximum , arr [ i ] )
    return maximum
if __name__ == ' _ _ main _ _ ' :
    arr = [ 2 , 10 , 15 , 7 , 6 , 8 , 13 ]
    n = len ( arr )
    print ( getPrime ( arr , n ) )

Index/key variables: i, p
Iterator variables: i, p
Python program:
from math import gcd
def getMinNum ( N ) :
    lcm = 1 ;
    for i in range ( 2 , N + 1 ) :
        lcm = ( ( i * lcm ) // ( gcd ( i , lcm ) ) ) ;
    return ( lcm + 1 ) ;
if __name__ == " _ _ main _ _ " :
    N = 5 ;
    print ( getMinNum ( N ) ) ;

Iterator variables: i
Python program:
def isSafe ( N , M , str ) :
    coll = 0
    colr = 0
    rowu = 0
    rowd = 0
    for i in range ( len ( str ) ) :
        if ( str [ i ] == ' L ' ) :
            coll += 1
            if ( colr > 0 ) :
                colr -= 1
            if ( coll == M ) :
                break
        elif ( str [ i ] == ' R ' ) :
            colr += 1
            if ( coll > 0 ) :
                coll -= 1
            if ( colr == M ) :
                break
        elif ( str [ i ] == ' U ' ) :
            rowu += 1
            if ( rowd > 0 ) :
                rowd -= 1
            if ( rowu == N ) :
                break
        elif ( str [ i ] == ' D ' ) :
            rowd += 1
            if ( rowu > 0 ) :
                rowu -= 1
            if ( rowd == N ) :
                break
    if ( abs ( rowd ) < N and abs ( rowu ) < N and abs ( coll ) < M and abs ( colr ) < M ) :
        return True
    return False
if __name__ == ' _ _ main _ _ ' :
    N = 1
    M = 1
    str = " R "
    if ( isSafe ( N , M , str ) ) :
        print ( " Yes " )
    else :
        print ( " No " )

Index/key variables: i
Accumulator variables: coll, colr, rowd, rowu
Iterator variables: i
Python program:
def printAns ( x , y , num ) :
    print ( " Indices ▁ in ▁ array " , num , " : " , end = " ▁ " )
    for i in range ( x , y ) :
        print ( i , end = " , ▁ " )
    print ( y )
def findSubarray ( N , a , b , swap ) :
    index = { }
    difference , j = 0 , 0
    index [ 0 ] = ( - 1 , - 1 )
    for i in range ( 0 , N ) :
        while b [ j ] < a [ i ] :
            j += 1
        difference = b [ j ] - a [ i ]
        if difference in index :
            if swap :
                idx = index [ b [ j ] - a [ i ] ]
                printAns ( idx [ 1 ] + 1 , j , 1 )
                printAns ( idx [ 0 ] + 1 , i , 2 )
            else :
                idx = index [ b [ j ] - a [ i ] ]
                printAns ( idx [ 0 ] + 1 , i , 1 )
                printAns ( idx [ 1 ] + 1 , j , 2 )
            return
        index [ difference ] = ( i , j )
    print ( " - 1" )
def cumulativeSum ( arr , n ) :
    for i in range ( 1 , n ) :
        arr [ i ] += arr [ i - 1 ]
if __name__ == " _ _ main _ _ " :
    a = [ 1 , 2 , 3 , 4 , 5 ]
    b = [ 6 , 2 , 1 , 5 , 4 ]
    N = len ( a )
    cumulativeSum ( a , N )
    cumulativeSum ( b , N )
    if b [ N - 1 ] > a [ N - 1 ] :
        findSubarray ( N , a , b , False )
    else :
        findSubarray ( N , b , a , True )

Index/key variables: difference, i, j
Iterator variables: i
Python program:
def Permutation ( n , k ) :
    p = [ 0 for i in range ( n + 1 ) ]
    for i in range ( 1 , n + 1 ) :
        p [ i ] = i
    for i in range ( 1 , n - k ) :
        p [ i + 1 ] = i
    p [ 1 ] = n - k
    for i in range ( 1 , n + 1 ) :
        print ( p [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
    n = 5
    k = 2
    Permutation ( n , k )

Index/key variables: i
Iterator variables: i
Python program:
def validPosition ( arr , N , K ) :
    count = 0 ; sum = 0 ;
    for i in range ( N ) :
        sum += arr [ i ] ;
    for i in range ( N ) :
        if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) :
            count += 1 ;
    return count ;
arr = [ 2 , 1 , 6 , 7 ] ;
K = 4 ;
N = len ( arr ) ;
print ( validPosition ( arr , N , K ) ) ;

Index/key variables: i
Accumulator variables: count, sum
Iterator variables: i
Python program:
def maxBitElement ( arr , n ) :
    num = 0
    max = - 1
    for i in range ( n ) :
        cnt = bin ( arr [ i ] ) . count ( '1' )
        if ( cnt > max ) :
            max = cnt
            num = arr [ i ]
    return num
if __name__ == ' _ _ main _ _ ' :
    arr = [ 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 ]
    n = len ( arr )
    print ( maxBitElement ( arr , n ) )

Index/key variables: i
Iterator variables: i
Python program:
N = 1000
def countPairs ( arr , n ) :
    size = ( 2 * N ) + 1
    freq = [ 0 for i in range ( size ) ]
    for i in range ( n ) :
        x = arr [ i ]
        freq [ x + N ] += 1
    ans = 0
    for i in range ( size ) :
        if ( freq [ i ] > 0 ) :
            ans += int ( ( ( freq [ i ] ) * ( freq [ i ] - 1 ) ) / 2 )
            for j in range ( i + 2 , 2001 , 2 ) :
                if ( freq [ j ] > 0 and ( freq [ int ( ( i + j ) / 2 ) ] > 0 ) ) :
                    ans += ( freq [ i ] * freq [ j ] )
    return ans
if __name__ == ' _ _ main _ _ ' :
    arr = [ 4 , 2 , 5 , 1 , 3 , 5 ]
    n = len ( arr )
    print ( countPairs ( arr , n ) )

Index/key variables: i, j
Accumulator variables: ans
Iterator variables: i, j
Python program:
def count_pairs ( a , b , n , m ) :
    odd1 = 0
    even1 = 0
    odd2 = 0
    even2 = 0
    for i in range ( n ) :
        if ( a [ i ] % 2 ) :
            odd1 += 1
        else :
            even1 += 1
    for i in range ( m ) :
        if ( b [ i ] % 2 ) :
            odd2 += 1
        else :
            even2 += 1
    pairs = ( min ( odd1 , even2 ) + min ( odd2 , even1 ) )
    return pairs
if __name__ == ' _ _ main _ _ ' :
    a = [ 9 , 14 , 6 , 2 , 11 ]
    b = [ 8 , 4 , 7 , 20 ]
    n = len ( a )
    m = len ( b )
    print ( count_pairs ( a , b , n , m ) )

Index/key variables: i
Accumulator variables: even1, even2, odd1, odd2
Iterator variables: i
Python program:
def find_leftmost_unsetbit ( n ) :
    ind = - 1 ;
    i = 1 ;
    while ( n ) :
        if ( ( n % 2 ) != 1 ) :
            ind = i ;
        i += 1 ;
        n >>= 1 ;
    return ind ;
def perform_steps ( n ) :
    left = find_leftmost_unsetbit ( n ) ;
    if ( left == - 1 ) :
        print ( " No ▁ steps ▁ required " ) ;
        return ;
    step = 1 ;
    while ( find_leftmost_unsetbit ( n ) != - 1 ) :
        if ( step % 2 == 0 ) :
            n += 1 ;
            print ( " Step " , step ,   " : Increase by 1 " ) ;
        else :
            m = find_leftmost_unsetbit ( n ) ;
            num = ( 2 ** m ) - 1 ;
            n = n ^ num ;
            print ( " Step " , step , " : ▁ Xor ▁ with " , num ) ;
        step += 1 ;
n = 39 ;
perform_steps ( n ) ;

Accumulator variables: i, n, step
Python program:
def vertices ( N , A , B ) :
    position = 0
    miniSum = 10 ** 9
    Sum = 0
    for i in range ( 1 , N + 1 ) :
        if ( i == A or i == B ) :
            continue
        else :
            x = abs ( i - A )
            y = abs ( i - B )
            Sum = x + y
            if ( Sum < miniSum ) :
                miniSum = Sum
                position = i
    return position
N = 3
A = 1
B = 2
print ( " Vertex ▁ = ▁ " , vertices ( N , A , B ) )

Iterator variables: i
Python program:
def factorial ( n ) :
    f = 1 ;
    for i in range ( 1 , n + 1 ) :
        f *= i ;
    return f ;
def sumFactorial ( arr , n ) :
    s = 0 ;
    for i in range ( 0 , n ) :
        s += factorial ( arr [ i ] ) ;
    return s ;
arr = [ 7 , 3 , 5 , 4 , 8 ] ;
n = len ( arr ) ;
print ( sumFactorial ( arr , n ) ) ;

Index/key variables: i
Accumulator variables: f, s
Iterator variables: i
Python program:
ans = 0
gr = [ [ ] for i in range ( 100005 ) ]
def Add_Edge ( u , v ) :
    gr [ u ] . append ( v )
    gr [ v ] . append ( u )
def dfs ( child , par , color ) :
    global ans
    if ( color [ child ] != color [ par ] ) :
        ans += 1
    for it in gr [ child ] :
        if ( it == par ) :
            continue
        dfs ( it , child , color )
color = [ 0 , 1 , 2 , 3 , 2 , 2 , 3 ]
Add_Edge ( 1 , 2 )
Add_Edge ( 1 , 3 )
Add_Edge ( 2 , 4 )
Add_Edge ( 2 , 5 )
Add_Edge ( 3 , 6 )
dfs ( 1 , 0 , color )
print ( ans )

Index/key variables: child, par, u, v
Iterator variables: it
Python program:
def highestPower ( str , length ) :
    ans = 0 ;
    for i in range ( length - 1 , - 1 , - 1 ) :
        if ( str [ i ] == '0' ) :
            ans += 1 ;
        else :
            break ;
    return ans ;
def main ( ) :
    str = "100100" ;
    length = len ( str ) ;
    print ( highestPower ( str , length ) ) ;
if __name__ == ' _ _ main _ _ ' :
    main ( )

Index/key variables: i
Accumulator variables: ans
Iterator variables: i
Python program:
mod = 10 ** 9 + 7
def factorial ( n , p ) :
    for i in range ( 2 , n + 1 ) :
        res = ( res * i ) % p
    return res
def power ( x , y , p ) :
    while ( y > 0 ) :
        if ( y & 1 ) :
            res = ( res * x ) % p
        x = ( x * x ) % p
    return res
def modInverse ( n , p ) :
    return power ( n , p - 2 , p )
def nCrModP ( n , r , p ) :
    if ( r == 0 ) :
        return 1
    fac = [ 0 for i in range ( n + 1 ) ]
    fac [ 0 ] = 1
    for i in range ( 1 , n + 1 ) :
        fac [ i ] = fac [ i - 1 ] * i % p
    return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p
def countArrangements ( n , k , p ) :
    return ( factorial ( n , p ) * nCrModP ( k , n , p ) ) % p
N = 5
K = 8
print ( countArrangements ( N , K , mod ) )

Index/key variables: i, n, r
Iterator variables: i
Python program:
N = 2
M = 3
def check ( mat ) :
    xorr = 0
    for i in range ( N ) :
        xorr ^= mat [ i ] [ 0 ]
    if ( xorr != 0 ) :
        return True
    for i in range ( N ) :
        for j in range ( 1 , M ) :
            if ( mat [ i ] [ j ] != mat [ i ] [ 0 ] ) :
                return True
    return False
mat = [ [ 7 , 7 , 7 ] , [ 10 , 10 , 7 ] ]
if ( check ( mat ) ) :
    print ( " Yes " )
else :
    print ( " No " )

Index/key variables: i, j
Accumulator variables: xorr
Iterator variables: i, j
Python program:
def sumodd ( n ) :
    return ( n * n )
def sumeven ( n ) :
    return ( n * ( n + 1 ) )
def findSum ( num ) :
    sumo = 0
    sume = 0
    x = 1
    cur = 0
    ans = 0
    while ( num > 0 ) :
        inc = min ( x , num )
        num -= inc
        if ( cur == 0 ) :
            ans = ans + sumodd ( sumo + inc ) - sumodd ( sumo )
            sumo += inc
        else :
            ans = ans + sumeven ( sume + inc ) - sumeven ( sume )
            sume += inc
        x *= 2
        cur ^= 1
    return ans
n = 4
print ( findSum ( n ) )

Accumulator variables: cur, num, sume, sumo, x
Python program:
from math import sqrt
MAXN = 100001
mod = 1000000007
spf = [ 0 for i in range ( MAXN ) ]
cnt = { i : 0 for i in range ( 10 ) }
def sieve ( ) :
    spf [ 1 ] = 1
    for i in range ( 2 , MAXN ) :
        spf [ i ] = i
    for i in range ( 4 , MAXN , 2 ) :
        spf [ i ] = 2
    for i in range ( 3 , int ( sqrt ( MAXN ) ) + 1 , 1 ) :
        if ( spf [ i ] == i ) :
            for j in range ( i * i , MAXN , i ) :
                if ( spf [ j ] == j ) :
                    spf [ j ] = i
def factorize ( f ) :
    while ( f > 1 ) :
        x = spf [ f ]
        while ( f % x == 0 ) :
            cnt [ x ] += 1
            f = int ( f / x )
def factorial ( n , p ) :
    res = 1
    for i in range ( 2 , n + 1 , 1 ) :
        res = ( res * i ) % p
    return res
def power ( x , y , p ) :
    res = 1
    x = x % p
    while ( y > 0 ) :
        if ( y & 1 ) :
            res = ( res * x ) % p
        y = y >> 1
        x = ( x * x ) % p
    return res
def modInverse ( n , p ) :
    return power ( n , p - 2 , p )
def nCrModP ( n , r , p ) :
    if ( r == 0 ) :
        return 1
    fac = [ 0 for i in range ( n + 1 ) ]
    fac [ 0 ] = 1
    for i in range ( 1 , n + 1 , 1 ) :
        fac [ i ] = fac [ i - 1 ] * i % p
    return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p
def countArrays ( arr , N , K , P ) :
    res = 1
    sieve ( )
    for i in range ( N ) :
        factorize ( arr [ i ] )
    for key , value in cnt . items ( ) :
        ci = value
        res = ( res * nCrModP ( ci + K - 1 , K - 1 , P ) ) % P
    return res
if __name__ == ' _ _ main _ _ ' :
    arr = [ 1 , 3 , 5 , 2 ]
    K = 3
    N = len ( arr )
    print ( countArrays ( arr , N , K , mod ) )

Index/key variables: f, i, j, n, r, x
Iterator variables: i, j, key, value
Python program:
MAXN = 100000
MOD = 1000000007
fact = [ 0 ] * MAXN ;
def factorial ( ) :
    fact [ 0 ] = 1
    for i in range ( 1 , MAXN ) :
        fact [ i ] = ( fact [ i - 1 ] * i ) % MOD
def power ( x , y , p ) :
    while ( y > 0 ) :
        if ( y & 1 ) :
            res = ( res * x ) % p
        x = ( x * x ) % p
    return res
def modInverse ( x , p ) :
    return power ( x , p - 2 , p )
def countDifferentNumbers ( arr , P ) :
    factorial ( ) ;
    res = 0 ; X = 0 ;
    for i in range ( 10 ) :
        X += arr [ i ]
    res = fact [ X ]
    for i in range ( 10 ) :
        if ( arr [ i ] > 1 ) :
            res = ( res * modInverse ( fact [ arr [ i ] ] , P ) ) % P ;
    return res ;
if __name__ == " _ _ main _ _ " :
    arr = [ 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 ]
    print ( countDifferentNumbers ( arr , MOD ) )

Index/key variables: X, i
Iterator variables: i
Python program:
N = 3
M = 7
def printOriginalMatrix ( a , b , mat ) :
    for i in range ( N ) :
        for j in range ( M ) :
            if ( mat [ i ] [ j ] == 1 ) :
                print ( min ( a [ i ] , b [ j ] ) , end = " ▁ " ) ;
            else :
                print ( 0 , end = " ▁ " ) ;
        print ( )
if __name__ == " _ _ main _ _ " :
    a = [ 2 , 1 , 3 ]
    b = [ 2 , 3 , 0 , 0 , 2 , 0 , 1 ]
    mat = [ [ 1 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 , 0 ] ] ;
    printOriginalMatrix ( a , b , mat ) ;

Index/key variables: i, j
Iterator variables: i, j