// C# code for the above approach
using System;
using System.Collections.Generic;
class GFG
{
static int MOD = 1000000007;
// Dp table initialized with -1
static int[,,,] dp=new int[21,21,10,190];
// Recursive Function to find numbers
// in the range A to B such that adjacent
// digits are different and their sum is
// equal to T
static int recur(int i, int j, int k, int sum, int T, string a)
{
// Base case
if (i == a.Length) {
// If current sum is equal to T
if (sum == T)
return 1;
// If not equal to T
else
return 0;
}
// If answer for current state is already
// calculated then just
// return dp[i,j,k,sum]
if (dp[i,j,k,sum] != -1)
return dp[i,j,k,sum];
// Answer initialized with zero
int ans = 0;
// Choosing first value
if (i == 0) {
// Iterating from 0 to maxvalue
// of digit current position
// can have
for (int l = 0; l <= (a[j] - '0'); l++) {
// l is maximum value of digit
if (l ==(a[j] - '0')) {
// Calling recursive function
// for selecting last max digit
// for current position
ans += recur(i + 1, j + 1, l, sum + l, T,
a);
}
else
// Calling recursive function
// for selecting l as current
// digit and its not max digit
ans += recur(i + 1, j, l, sum + l, T, a);
}
}
else {
// Digits taken till now were
// all zero
if (sum == 0) {
// All digits can be possible
// from 0 to 9
for (int l = 0; l <= 9; l++) {
// Calling recursive function
// for all digits from 0 to 9
ans += recur(i + 1, j, l, sum + l, T, a);
}
}
// Already non zero digits are taken
else {
// Iterating from 0 to 9
for (int l = 0; l <= 9; l++) {
// If max digit was taken
// last time
if (j == i) {
// If current digit
// is different
if (l <= (a[j] - '0') && l != k) {
// max digit taken calling
// recursive function
if (l == (a[j] - '0'))
ans += recur(i + 1, j + 1, l,
sum + l, T, a);
// Calling recursive
// function for non
// max digit
else
ans += recur(i + 1, j, l,
sum + l, T, a);
}
}
// If max digit was not taken
else if (l != k)
ans += recur(i + 1, j, l, sum + l, T,
a);
}
}
}
// Save and return dp value
return dp[i,j,k,sum] = ans;
}
// Function to find numbers
// in the range A to B such that adjacent
// digits are different and their sum is
// equal to T
static int countInRange(int T, int A, int B)
{
// Initializing dp array with - 1
for(int i=0; i<21; i++)
{
for(int j=0; j<21; j++)
{
for(int k=0; k<10; k++)
{
for(int l=0; l<190; l++)
dp[i,j,k,l]=-1;
}
}
}
A--;
string L = A.ToString(), R = B.ToString();
// Numbers with sum of digits T from
// 1 to L - 1
int ans1 = recur(0, 0, 0, 0, T, L);
// Initializing dp array with - 1
for(int i=0; i<21; i++)
{
for(int j=0; j<21; j++)
{
for(int k=0; k<10; k++)
{
for(int l=0; l<190; l++)
dp[i,j,k,l]=-1;
}
}
}
// Numbers with sum of digits T in
// the range 1 to R
int ans2 = recur(0, 0, 0, 0, T, R);
// Difference of ans2 and ans1
// will generate answer for
// required range
return ans2 - ans1;
}
// Driver Code
static void Main(string[] args)
{
// Input 1
int T = 5, A = 1, B = 100;
// Function Call
Console.WriteLine(countInRange(T, A, B));
// Input 2
int T1 = 5, A1 = 1, B1 = 100000000;
// Function Call
Console.WriteLine(countInRange(T1, A1, B1));
}
}