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
162
163
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define SZ(x) ((int)(x).size())
#define HASK(S, x) (S.find(x) != S.end())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std; 
 
const int L = 19, M = 1 << 9;

long long dp[L][M][10][9][8][7], pow10[L];
bool valid[M][10][9][8][7];

bool check(long long x)
{
	vector <int> digs;
	for (long long y = x; y != 0; y /= 10)
		digs.pb(y % 10);
	for (int d : digs)
		if (d == 0 or x % d != 0)
			return false;
	return true;
}

void init()
{
	dp[0][0][0][0][0][0] = pow10[0] = 1ll;
	
	FOR(l, 1, L) FOR(m, 1, M) FOR(d, 1, 10) if (ithBit(m, d-1)) FOR(s9, 0, 9) FOR(r8, 0, 8) FOR(r7, 0, 7)
	{
		int det8 = (r8 - d + 16) & 7;
		if (det8 & 1)	continue;
		int m2 = m ^ (1 << d-1);
		int i9 = (s9 - d + 9) % 9;
		int i7 = (r7 - d + 14) * 5 % 7;
		long long& stan = dp[l][m][d][s9][r8][r7];
		FOR(c, 0, 10)
			stan += dp[l-1][m][c][i9][det8/2][i7],
			stan += dp[l-1][m][c][i9][det8/2 + 4][i7],
			stan += dp[l-1][m2][c][i9][det8/2][i7],
			stan += dp[l-1][m2][c][i9][det8/2 + 4][i7];
	}
	
	FOR(i, 1, L)	pow10[i] = pow10[i-1] * 10;
	
	FOR(m, 1, M)
	{
		int md = ~0, m9 = ~0, m8 = ~0, m7 = ~0;

		if (ithBit(m, 2-1))		md &= 0b010101010;
		
		if (ithBit(m, 3-1))		m9 &= 0b001001001;
		
		if (ithBit(m, 4-1))		m8 &= 0b00010001;
		
		if (ithBit(m, 5-1))		md &= 0b000010000;

		if (ithBit(m, 6-1))		md &= 0b010101010,	m9 &= 0b001001001;
		
		if (ithBit(m, 7-1))		m7 &= 1;
		
		if (ithBit(m, 8-1))		m8 &= 1;
		
		if (ithBit(m, 9-1))		m9 &= 1;
		
		FOR(d, 1, 10) FOR(i9, 0, 9) FOR(i8, 0, 8) FOR(i7, 0, 7)
			if (ithBit(md, d-1) and ithBit(m9, i9) and ithBit(m8, i8) and ithBit(m7, i7))
				valid[m][d][i9][i8][i7] = true;
	}
}

int itoa(long long x, char* res)
{
	int n = 0;
	for (; x != 0; x /= 10)
		res[n++] = '0' + x%10;
	reverse(res, res+n);
	return n;
}

long long getDp(long long pref, int l, int mask, int s)
{
	if (l == 0)	return check(pref);
	long long ret = 0;
	
	FOR(m, 1, M) FOR(d, 1, 10) FOR(s9, 0, 9) FOR(r8, 0, 8) FOR(r7, 0, 7)
	{
		int mm = m | mask;
		int i9 = (s + s9) % 9;
		int i8 = (pref + r8) % 8;
		int i7 = (pref + r7) % 7;
		if (valid[mm][d][i9][i8][i7])	
			ret += dp[l][m][d][s9][r8][r7];	
	}
	
	return ret;
}

long long getLE(long long x)
{
	char T[20];
	int n = itoa(x, T), m = 0, s = 0;
	long long pref = 0, ret = 0;
	if (x != 0) ret = check(x);
	
	FOR(i, 1, n)
		ret += getDp(0, i, 0, 0);
	
	FOR(i, 0, n)
	{
		if (T[i] == '0')	break;
		
		int len = n-i-1;
		FOR(d, 1, T[i]-'0')
		{
			pref += pow10[len];
			s = (s + 1) % 9;
			ret += getDp(pref, len, m | (1 << d-1), s);
		}
		
		m |= 1 << (T[i]-'0')-1;
		pref += pow10[len];
		s = (s + 1) % 9;
	}
	return ret;
}

long long query(long long a, long long b)
{	return getLE(b) - getLE(a-1);	}

void solve()
{
	init();
	long long a, b;
	scanf ("%lld%lld", &a, &b);
	printf("%lld\n", query(a, b));
}
 
int main()
{
	int t = 1;
	//scanf ("%d", &t);
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}