Calculate total count of sequences of length even_count with sum divisible by A where first digit can be zero
let dp = new Array ( even_count ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < even_count ; i ++ ) { for ( var j = 0 ; j < max_sum + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= 9 ; i ++ ) dp [ 0 ] [ i % A ] ++ ; for ( let i = 1 ; i < even_count ; i ++ ) { for ( let j = 0 ; j <= 9 ; j ++ ) { for ( let k = 0 ; k <= max_sum ; k ++ ) { if ( dp [ i - 1 ] [ k ] > 0 ) dp [ i ] [ ( j + k ) % A ] += dp [ i - 1 ] [ k ] ; } } }