1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>

using namespace std;
long long int powe10[19]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,1000000000000000000};


void add(short* num, int & position)
{
    num[position]++;
    if(num[position]>9)
    {
        num[position]=1;
        position++;
        add(num,position);
    }
}

long long int allNum(short* num)
{
    long long int number=0;
    for(int i=0;i<19;i++)
    {
        number+=num[i]*powe10[i];
    }
    return number;
}

bool check(short* num,int div)
{
    switch (div)
    {
    case 1:
        return true;
        break;
    case 2:
        if(num[0]%2==0)
            return true;
        break;
    case 3:
    {
        int m=0;
        for(int i=0;i<19;i++)
        {
            m+=num[i];
        }
        if(m%3==0)
            return true;  
        break; 
    }
    case 4:
        if((num[0]+(num[1]*10))%4==0)
            return true;
        break;
    case 5:
        if(num[0]==5)
            return true;
        break;
    case 6:
    {
        int m=0;
        for(int i=0;i<19;i++)
        {
            m+=num[i];
        }
        if(m%3==0 && num[0]%2==0)
            return true;  
        break; 
    }   
    case 7:
    {
        
        if(allNum(num)%7==0)
            return true;
        break;
    }
    case 8:
        if((num[0]+(num[1]*10)+(num[2]*100))%8==0)
            return true;
        break;
    case 9:
    {
        int m=0;
        for(int i=0;i<19;i++)
        {
            m+=num[i];
        }
        if(m%9==0)
            return true;  
        break; 
    }    
    default:
        break;
    }
    return false;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long int l,r,ans=0;
    cin>>l>>r;
    short num[19];
    for(int i=0;i<18;i++)
    {
        num[i]=(l%powe10[i+1])/powe10[i];

    }
    num[18]=l/powe10[18];
    int last0;
    for(int i=18; i>=0;i--)
    {
        if(num[i]==0)
            last0=i;
        else break;
    }

    bool yes=true;
    int posit=0;
    bool checked[10]={false,false,false,false,false,false,false,false,false,false};
    while(allNum(num)<=r)
    {
        //cout<<allNum(num)<<"\n";
        for(int i=1;i<10;i++)
        {
            checked[i]=false;
        }
        posit=0;
        yes=true;
        for(int i=0;i<19;i++)
        {
            if(last0 <=i && num[i]==0)
            {
                break;
            }
            else if(num[0]==0)
            {
                yes=false;
                break;
            }
                
            if(checked[num[i]]==true)
                continue;
            if(!check(num,num[i]))
            {
                yes=false;
                break;
            }
            else checked[num[i]]=true;
        }
        if(yes)
        {
            ans++;
            //cout<<allNum(num)<<" ";
        }
            
        add(num,posit);
    }
    cout<<ans;
    return 0;
}