This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**Lucky Boy**/
#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 pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 100005
#define maxm 500005
#define pii pair <int,int>
#define Task ""
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
long long dp[20][10][10],a,b,f[20][10];
int c[20],cnt;
void Prepare()
{
FOR(i,0,9) f[1][i] = 1,f[2][i] = 9;
FOR(i,0,9)
FOR(j,0,9) dp[2][i][j] = (i != j);
FOR(i,3,19)
FOR(j,0,9)
FOR(k,0,9)
{
if (j == k) continue;
FOR(h,0,9)
{
if (j == h) continue;
dp[i][j][k] += dp[i-1][k][h];
}
f[i][j] += dp[i][j][k];
}
}
long long Calc(long long x)
{
if (x < 0) return 1;
if (x < 10) return x + 1;
long long res = 0;
cnt = 0;
while (x)
{
c[++cnt] = x % 10;
x /= 10;
}
FOR(i,0,c[cnt] - 1)
{
res += f[cnt][i];
}
FORD(i,cnt - 1,1)
{
FOR(j,0,c[i] - 1)
{
if (j == c[i+1] || (i + 2 <= cnt && c[i+2] == j)) continue;
res += dp[i+1][c[i+1]][j];
}
if (c[i] == c[i+1] || (i + 2 <= cnt && c[i+2] == c[i])) break;
++res;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
//freopen(Task".inp", "r",stdin);
//freopen(Task".out", "w",stdout);
Prepare();
cin >> a >> b;
cout << Calc(b) - Calc(a-1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |