codeparrot/xlcost-text-to-code huggingface dataset:
Maximize product of digit sum of consecutive pairs in a subsequence of length K | Python3 implementation to find the maximum product of the digit sum of the consecutive pairs of the subsequence of the length K ; Function to find the product of two numbers digit sum in the pair ; Loop to find the digits of the number ; Loop to find the digits of other number ; Function to find the subsequence of the length K ; Base case ; Condition when we didn 't reach  the length K, but ran out of  elements of the array ; Condition if already calculated ; If length upto this point is odd ; If length is odd , it means we need second element of this current pair , calculate the product of digit sum of current and previous element and recur by moving towards next index ; If length upto this point is even ; Exclude this current element and recur for next elements . ; Return by memoizing it , by selecting the maximum among two choices . ; Driver code
import sys NEW_LINE MAX = 100 NEW_LINE dp = [ ] NEW_LINE for i in range ( 1000 ) : NEW_LINE INDENT temp1 = [ ] NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT temp2 = [ ] NEW_LINE for k in range ( MAX ) : NEW_LINE INDENT temp2 . append ( 0 ) NEW_LINE DEDENT temp1 . append ( temp2 ) NEW_LINE DEDENT dp . append ( temp1 ) NEW_LINE DEDENT def productDigitSum ( x , y ) : NEW_LINE INDENT sumx = 0 NEW_LINE while x : NEW_LINE INDENT sumx += x % 10 NEW_LINE x = x // 10 NEW_LINE DEDENT sumy = 0 NEW_LINE while y : NEW_LINE INDENT sumy += y % 10 NEW_LINE y = y // 10 NEW_LINE DEDENT return sumx * sumy NEW_LINE DEDENT def solve ( arr , i , len , prev , n , k ) : NEW_LINE INDENT if len == k : NEW_LINE INDENT return 0 NEW_LINE DEDENT if i == n : NEW_LINE INDENT return - sys . maxsize - 1 NEW_LINE DEDENT if dp [ i ] [ len ] [ prev ] : NEW_LINE INDENT return dp [ i ] [ len ] [ prev ] NEW_LINE DEDENT if len & 1 : NEW_LINE INDENT inc = ( productDigitSum ( arr [ prev ] , arr [ i ] ) + solve ( arr , i + 1 , len + 1 , i , n , k ) ) NEW_LINE DEDENT else : NEW_LINE INDENT inc = solve ( arr , i + 1 , len + 1 , i , n , k ) NEW_LINE DEDENT exc = solve ( arr , i + 1 , len , prev , n , k ) NEW_LINE dp [ i ] [ len ] [ prev ] = max ( inc , exc ) NEW_LINE return dp [ i ] [ len ] [ prev ] NEW_LINE DEDENT arr = [ 10 , 5 , 9 , 101 , 24 , 2 , 20 , 14 ] NEW_LINE n = len ( arr ) NEW_LINE k = 6 NEW_LINE print ( solve ( arr , 0 , 0 , 0 , n , k ) ) NEW_LINE
Count minimum factor jumps required to reach the end of an Array | vector to store factors of each integer ; dp array ; Precomputing all factors of integers from 1 to 100000 ; Function to count the minimum jumps ; If we reach the end of array , no more jumps are required ; If the jump results in out of index , return INT_MAX ; If the answer has been already computed , return it directly ; Else compute the answer using the recurrence relation ; Iterating over all choices of jumps ; Considering current factor as a jump ; Jump leads to the destination ; Return ans and memorize it ; Driver code ; pre - calculating the factors
factors = [ [ ] for i in range ( 100005 ) ] ; NEW_LINE dp = [ 0 for i in range ( 100005 ) ] ; NEW_LINE def precompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT for j in range ( i , 100001 , i ) : NEW_LINE INDENT factors [ j ] . append ( i ) ; NEW_LINE DEDENT DEDENT DEDENT def solve ( arr , k , n ) : NEW_LINE INDENT if ( k == n - 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( k >= n ) : NEW_LINE INDENT return 1000000000 NEW_LINE DEDENT if ( dp [ k ] ) : NEW_LINE INDENT return dp [ k ] ; NEW_LINE DEDENT ans = 1000000000 NEW_LINE for j in factors [ arr [ k ] ] : NEW_LINE INDENT res = solve ( arr , k + j , n ) ; NEW_LINE if ( res != 1000000000 ) : NEW_LINE INDENT ans = min ( ans , res + 1 ) ; NEW_LINE DEDENT DEDENT dp [ k ] = ans ; NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT precompute ( ) NEW_LINE arr = [ 2 , 8 , 16 , 55 , 99 , 100 ] NEW_LINE n = len ( arr ) NEW_LINE print ( solve ( arr , 0 , n ) ) NEW_LINE DEDENT
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Python3 program to find the maximum distance from a 0 - cell to a 1 - cell ; If the matrix consists of only 0 ' s ▁ ▁ or ▁ only ▁ 1' s ; If it 's a 0-cell ; Calculate its distance with every 1 - cell ; Compare and store the minimum ; Compare ans store the maximum ; Driver code
def maxDistance ( grid ) : NEW_LINE INDENT one = [ ] NEW_LINE M = len ( grid ) NEW_LINE N = len ( grid [ 0 ] ) NEW_LINE ans = - 1 NEW_LINE for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE if ( grid [ i ] [ j ] == 1 ) : NEW_LINE INDENT one . append ( [ i , j ] ) NEW_LINE DEDENT DEDENT if ( one == [ ] or M * N == len ( one ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE if ( grid [ i ] [ j ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT dist = float ( ' inf ' ) NEW_LINE for p in one : NEW_LINE INDENT d = abs ( p [ 0 ] - i ) + abs ( p [ 1 ] - j ) NEW_LINE dist = min ( dist , d ) NEW_LINE if ( dist <= ans ) : NEW_LINE break NEW_LINE ans = max ( ans , dist ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ] NEW_LINE print ( maxDistance ( arr ) ) NEW_LINE
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Function to find the maximum distance ; Queue to store all 1 - cells ; Grid dimensions ; Directions traversable from a given a particular cell ; If the grid contains only 0 s or only 1 s ; Access every 1 - cell ; Traverse all possible directions from the cells ; Check if the cell is within the boundaries or contains a 1 ; Driver code
def maxDistance ( grid ) : NEW_LINE INDENT q = [ ] NEW_LINE M = len ( grid ) NEW_LINE N = len ( grid [ 0 ] ) NEW_LINE ans = - 1 NEW_LINE dirs = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 0 , - 1 ] , [ - 1 , 0 ] ] NEW_LINE for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( grid [ i ] [ j ] == 1 ) : NEW_LINE INDENT q . append ( [ i , j ] ) NEW_LINE DEDENT DEDENT DEDENT if ( len ( q ) == 0 or M * N == len ( q ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( len ( q ) > 0 ) : NEW_LINE INDENT cnt = len ( q ) NEW_LINE while ( cnt > 0 ) : NEW_LINE INDENT p = q [ 0 ] NEW_LINE q . pop ( ) NEW_LINE for Dir in dirs : NEW_LINE INDENT x = p [ 0 ] + Dir [ 0 ] NEW_LINE y = p [ 1 ] + Dir [ 1 ] NEW_LINE if ( x < 0 or x >= M or y < 0 or y >= N or grid [ x ] [ y ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT q . append ( [ x , y ] ) NEW_LINE grid [ x ] [ y ] = 1 NEW_LINE DEDENT cnt -= 1 NEW_LINE DEDENT ans += 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 1 ] ] NEW_LINE print ( maxDistance ( arr ) ) NEW_LINE
Count the number of ways to give ranks for N students such that same ranks are possible | Python3 program to calculate the number of ways to give ranks for N students such that same ranks are possible ; Initializing a table in order to store the bell triangle ; Function to calculate the K - th bell number ; If we have already calculated the bell numbers until the required N ; Base case ; First Bell Number ; If the value of the bell triangle has already been calculated ; Fill the defined dp table ; Function to return the number of ways to give ranks for N students such that same ranks are possible ; Resizing the dp table for the given value of n ; Variables to store the answer and the factorial value ; Iterating till N ; Simultaneously calculate the k ! ; Computing the K - th bell number and multiplying it with K ! ; Driver code
mod = 1e9 + 7 NEW_LINE dp = [ [ - 1 for x in range ( 6 ) ] for y in range ( 6 ) ] NEW_LINE def f ( n , k ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( k == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ n ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ k ] NEW_LINE DEDENT dp [ n ] [ k ] = ( ( ( ( k * f ( n - 1 , k ) ) % mod + ( f ( n - 1 , k - 1 ) ) % mod ) % mod ) ) NEW_LINE return dp [ n ] [ k ] NEW_LINE DEDENT def operation ( n ) : NEW_LINE INDENT global dp NEW_LINE ans = 0 NEW_LINE fac = 1 NEW_LINE for k in range ( 1 , n + 1 ) : NEW_LINE INDENT fac *= k NEW_LINE ans = ( ans + ( fac * f ( n , k ) ) % mod ) % mod NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE print ( int ( operation ( n ) ) ) NEW_LINE DEDENT
Print all Longest dividing subsequence in an Array | Function to print LDS [ i ] element ; Traverse the Max [ ] ; Function to construct and print Longest Dividing Subsequence ; 2D vector for storing sequences ; Push the first element to LDS [ ] [ ] ; Interate over all element ; Loop for every index till i ; If current elements divides arr [ i ] and length is greater than the previous index , then insert the current element to the sequences LDS [ i ] ; L [ i ] ends with arr [ i ] ; LDS stores the sequences till each element of arr [ ] Traverse the LDS [ ] [ ] to find the maximum length ; Print all LDS with maximum length ; Find size ; If size = maxLength ; Print LDS ; Driver Code ; Function call
def printLDS ( Max ) : NEW_LINE INDENT for it in Max : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT def LongestDividingSeq ( arr , N ) : NEW_LINE INDENT LDS = [ [ ] for i in range ( N ) ] NEW_LINE LDS [ 0 ] . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ i ] % arr [ j ] == 0 ) and ( len ( LDS [ i ] ) < len ( LDS [ j ] ) + 1 ) ) : NEW_LINE INDENT LDS [ i ] = LDS [ j ] . copy ( ) NEW_LINE DEDENT DEDENT LDS [ i ] . append ( arr [ i ] ) NEW_LINE DEDENT maxLength = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = len ( LDS [ i ] ) NEW_LINE maxLength = max ( maxLength , x ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT size = len ( LDS [ i ] ) NEW_LINE if ( size == maxLength ) : NEW_LINE INDENT printLDS ( LDS [ i ] ) NEW_LINE print ( ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 2 , 11 , 16 , 12 , 36 , 60 , 71 ] NEW_LINE N = len ( arr ) NEW_LINE LongestDividingSeq ( arr , N ) ; NEW_LINE
Count the number of ordered sets not containing consecutive numbers | Function to calculate the count of ordered set for a given size ; Base cases ; Function returns the count of all ordered sets ; Prestore the factorial value ; Iterate all ordered set sizes and find the count for each one maximum ordered set size will be smaller than N as all elements are distinct and non consecutive ; Multiply ny size ! for all the arrangements because sets are ordered ; Add to total answer ; Driver code
def CountSets ( x , pos ) : NEW_LINE INDENT if ( x <= 0 ) : NEW_LINE INDENT if ( pos == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( pos == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT answer = ( CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ) NEW_LINE return answer NEW_LINE DEDENT def CountOrderedSets ( n ) : NEW_LINE INDENT factorial = [ 1 for i in range ( 10000 ) ] NEW_LINE factorial [ 0 ] = 1 NEW_LINE for i in range ( 1 , 10000 , 1 ) : NEW_LINE INDENT factorial [ i ] = factorial [ i - 1 ] * i NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT sets = CountSets ( n , i ) * factorial [ i ] NEW_LINE answer = answer + sets NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( CountOrderedSets ( N ) ) NEW_LINE DEDENT
Maximum sum such that exactly half of the elements are selected and no two adjacent | Python3 program to find maximum sum possible such that exactly floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Intitialising the dp table ; Base case ; Condition to select the current element ; Condition to not select the current element if possible ; Driver code
import sys NEW_LINE def MaximumSum ( a , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = - sys . maxsize - 1 NEW_LINE DEDENT DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 0 NEW_LINE DEDENT for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 , 1 ) : NEW_LINE INDENT val = - sys . maxsize - 1 NEW_LINE if ( ( i - 2 >= 0 and dp [ i - 2 ] [ j - 1 ] != - sys . maxsize - 1 ) or i - 2 < 0 ) : NEW_LINE INDENT if ( i - 2 >= 0 ) : NEW_LINE INDENT val = a [ i - 1 ] + dp [ i - 2 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT val = a [ i - 1 ] NEW_LINE DEDENT DEDENT if ( i - 1 >= j ) : NEW_LINE INDENT val = max ( val , dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT dp [ i ] [ j ] = val NEW_LINE DEDENT DEDENT return dp [ n ] [ n // 2 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( A ) NEW_LINE print ( MaximumSum ( A , N ) ) NEW_LINE DEDENT
Check if N can be converted to the form K power K by the given operation | Python3 implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
kPowKform = dict ( ) NEW_LINE def func ( n ) : NEW_LINE INDENT global kPowKform NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n in kPowKform ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT answer = 0 NEW_LINE x = n NEW_LINE while ( x > 0 ) : NEW_LINE INDENT d = x % 10 NEW_LINE if ( d != 0 ) : NEW_LINE INDENT if ( func ( n - d * d ) ) : NEW_LINE INDENT answer = 1 NEW_LINE break NEW_LINE DEDENT DEDENT x //= 10 NEW_LINE DEDENT return answer NEW_LINE DEDENT def canBeConverted ( n ) : NEW_LINE INDENT if ( func ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 90 NEW_LINE for i in range ( 1 , 9 ) : NEW_LINE INDENT val = 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT val *= i NEW_LINE DEDENT kPowKform [ val ] = 1 NEW_LINE DEDENT canBeConverted ( N ) NEW_LINE DEDENT
Count of subarrays of an Array having all unique digits | Function to check whether the subarray has all unique digits ; Storing all digits occurred ; Traversing all the numbers of v ; Storing all digits of v [ i ] ; Checking whether digits of v [ i ] have already occurred ; Inserting digits of v [ i ] in the set ; Function to count the number subarray with all digits unique ; Traverse through all the subarrays ; To store elements of this subarray ; Generate all subarray and store it in vector ; Check whether this subarray has all digits unique ; Increase the count ; Return the count ; Driver code
def check ( v ) : NEW_LINE INDENT digits = set ( ) NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT d = set ( ) NEW_LINE while ( v [ i ] != 0 ) : NEW_LINE INDENT d . add ( v [ i ] % 10 ) NEW_LINE v [ i ] //= 10 NEW_LINE DEDENT for it in d : NEW_LINE INDENT if it in digits : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for it in d : NEW_LINE INDENT digits . add ( it ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def numberOfSubarrays ( a , n ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( 1 , 1 << n ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT temp . append ( a [ j ] ) NEW_LINE DEDENT DEDENT if ( check ( temp ) ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE A = [ 1 , 12 , 23 , 34 ] NEW_LINE print ( numberOfSubarrays ( A , N ) ) NEW_LINE DEDENT
Find the row whose product has maximum count of prime factors | Python3 implementation to find the row whose product has maximum number of prime factors ; function for SieveOfEratosthenes ; Create a boolean array " isPrime [ 0 . . N ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is not a prime , else true . ; check if isPrime [ p ] is not changed ; Update all multiples of p ; Print all isPrime numbers ; function to display the answer ; function to Count the row number of divisors in particular row multiplication ; Find count of occurrences of each prime factor ; Compute count of all divisors ; Update row number if factors of this row is max ; Clearing map to store prime factors for next row ; Driver code
N = 3 NEW_LINE M = 5 NEW_LINE Large = int ( 1e6 ) ; NEW_LINE prime = [ ] ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT isPrime = [ True ] * ( Large + 1 ) ; NEW_LINE for p in range ( 2 , int ( Large ** ( 1 / 2 ) ) ) : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , Large + 1 , p ) : NEW_LINE INDENT isPrime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT for p in range ( 2 , Large + 1 ) : NEW_LINE INDENT if ( isPrime [ p ] ) : NEW_LINE INDENT prime . append ( p ) ; NEW_LINE DEDENT DEDENT DEDENT def Display ( arr , row ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT print ( arr [ row ] [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def countDivisorsMult ( arr ) : NEW_LINE INDENT mp = { } ; NEW_LINE row_no = 0 ; max_factor = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT no = arr [ i ] [ j ] NEW_LINE for k in range ( len ( prime ) ) : NEW_LINE INDENT while ( no > 1 and no % prime [ k ] == 0 ) : NEW_LINE INDENT no //= prime [ k ] ; NEW_LINE if prime [ k ] not in mp : NEW_LINE INDENT mp [ prime [ k ] ] = 0 NEW_LINE DEDENT mp [ prime [ k ] ] += 1 ; NEW_LINE DEDENT if ( no == 1 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT res = 1 ; NEW_LINE for it in mp : NEW_LINE INDENT res *= mp [ it ] ; NEW_LINE DEDENT if ( max_factor < res ) : NEW_LINE INDENT row_no = i ; NEW_LINE max_factor = res ; NEW_LINE DEDENT mp . clear ( ) ; NEW_LINE DEDENT Display ( arr , row_no ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 , 10 , 23 ] , [ 4 , 5 , 6 , 7 , 8 ] , [ 7 , 8 , 9 , 15 , 45 ] ] ; NEW_LINE SieveOfEratosthenes ( ) ; NEW_LINE countDivisorsMult ( arr ) ; NEW_LINE DEDENT
Smallest N digit number whose sum of square of digits is a Perfect Square | Python3 implementation to find Smallest N digit number whose sum of square of digits is a Perfect Square ; function to check if number is a perfect square ; function to calculate the smallest N digit number ; place digits greater than equal to prev ; check if placing this digit leads to a solution then return it ; else backtrack ; create a representing the N digit number ; Driver code ; initialise N
from math import sqrt NEW_LINE def isSquare ( n ) : NEW_LINE INDENT k = int ( sqrt ( n ) ) NEW_LINE return ( k * k == n ) NEW_LINE DEDENT def calculate ( pos , prev , sum , v ) : NEW_LINE INDENT if ( pos == len ( v ) ) : NEW_LINE INDENT return isSquare ( sum ) NEW_LINE DEDENT for i in range ( prev , 9 + 1 ) : NEW_LINE INDENT v [ pos ] = i NEW_LINE sum += i * i NEW_LINE if ( calculate ( pos + 1 , i , sum , v ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT sum -= i * i NEW_LINE DEDENT return 0 NEW_LINE DEDENT def minValue ( n ) : NEW_LINE INDENT v = [ 0 ] * ( n ) NEW_LINE if ( calculate ( 0 , 1 , 0 , v ) ) : NEW_LINE INDENT answer = " " NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT answer += chr ( v [ i ] + ord ( '0' ) ) NEW_LINE DEDENT return answer NEW_LINE DEDENT else : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE print ( minValue ( N ) ) NEW_LINE DEDENT
Sum of GCD of all possible sequences | Pyhton3 implementation of the above approach ; A recursive function that generates all the sequence and find GCD ; If we reach the sequence of length N g is the GCD of the sequence ; Initialise answer to 0 ; Placing all possible values at this position and recursively find the GCD of the sequence ; Take GCD of GCD calculated uptill now i . e . g with current element ; Take modulo to avoid overflow ; Return the final answer ; Function that finds the sum of GCD of all the subsequence of N length ; Recursive function that generates the sequence and return the GCD ; Driver code ; Function Call
MOD = 1e9 + 7 NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def calculate ( pos , g , n , k ) : NEW_LINE INDENT if ( pos == n ) : NEW_LINE INDENT return g NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT answer = ( answer % MOD + calculate ( pos + 1 , gcd ( g , i ) , n , k ) % MOD ) NEW_LINE answer %= MOD NEW_LINE DEDENT return answer NEW_LINE DEDENT def sumofGCD ( n , k ) : NEW_LINE INDENT return calculate ( 0 , 0 , n , k ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE K = 2 NEW_LINE print ( sumofGCD ( N , K ) ) NEW_LINE DEDENT
Sum of GCD of all possible sequences | Python3 implementation of the above approach ; Function to find a ^ b in log ( b ) ; Function that finds the sum of GCD of all the subsequence of N length ; To stores the number of sequences with gcd i ; Find contribution of each gcd to happen ; To count multiples ; possible sequences with overcounting ; to avoid overflow ; Find extra element which will not form gcd = i ; Find overcounting ; Remove the overcounting ; To store the final answer ; Return Final answer ; Driver code ; Function call
MOD = ( int ) ( 1e9 + 7 ) NEW_LINE def fastexpo ( a , b ) : NEW_LINE INDENT res = 1 NEW_LINE a = a % MOD NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( ( b & 1 ) != 0 ) : NEW_LINE INDENT res = ( res * a ) % MOD NEW_LINE DEDENT a = a * a NEW_LINE a = a % MOD NEW_LINE b = b >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def sumofGCD ( n , k ) : NEW_LINE INDENT count = [ 0 ] * ( k + 1 ) NEW_LINE for g in range ( k , 0 , - 1 ) : NEW_LINE INDENT count_multiples = k // g NEW_LINE temp = fastexpo ( count_multiples , n ) NEW_LINE temp = temp % MOD NEW_LINE extra = 0 NEW_LINE for j in range ( g * 2 , k + 1 , g ) : NEW_LINE INDENT extra = extra + count [ j ] NEW_LINE extra = extra % MOD NEW_LINE DEDENT count [ g ] = temp - extra + MOD NEW_LINE count [ g ] = count [ g ] % MOD NEW_LINE DEDENT Sum = 0 NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT add = count [ i ] % MOD * i % MOD NEW_LINE add = add % MOD NEW_LINE Sum = Sum + add NEW_LINE Sum = Sum % MOD NEW_LINE DEDENT return Sum NEW_LINE DEDENT N , K = 3 , 2 NEW_LINE print ( sumofGCD ( N , K ) ) NEW_LINE