Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a balanced number if:
1) Every even digit appears an odd number of times in its decimal representation
2) Every odd digit appears an even number of times in its decimal representation
For example, 77, 211, 6222 and 112334445555677 are balanced numbers while 351, 21, and 662 are not.
Given an interval [A, B], your task is to find the amount of balanced numbers in [A, B] where both A and B are included.
Input
The first line contains an integer T representing the number of test cases.
A test case consists of two numbers A and B separated by a single space representing the interval. You may assume that 1 <= A <= B <= 1019
Output
For each test case, you need to write a number in a single line: the amount of balanced numbers in the corresponding interval
Example
Input:
2
1 1000
1 9
Output:
147
4
三进制表示状态+数位dp
/*************************************************************************
> File Name: spoj10606.cpp
> Author: ALex
> Mail: [email protected]
> Created Time: 2015年02月28日 星期六 19时43分30秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
LL dp[30][60000];
int bit[30];
int three[20];
int S[30];
int getsta (int sta, int e)
{
int ss = 0;
memset (S, 0, sizeof(S));
int cnt = 0;
while (sta)
{
S[cnt++] = sta % 3;
sta /= 3;
}
if ((++S[e]) == 3)
{
S[e] = 1;
}
for (int i = 0; i <= 9; ++i)
{
ss += S[i] * (three[i]);
}
return ss;
}
bool judge (int sta)
{
memset (S, 0, sizeof(S));
int cnt = 0;
while (sta)
{
S[cnt++] = sta % 3;
sta /= 3;
}
for (int i = 0; i <= 9; ++i)
{
if (!S[i])
{
continue;
}
if ((i & 1) != (S[i] & 1))
{
continue;
}
else
{
return 0;
}
}
return 1;
}
LL dfs (int cur, int e, int sta, bool flag, bool zero)
{
if (cur == -1)
{
if (zero)
{
return 0;
}
return judge (sta);
}
if (!flag && ~dp[cur][sta])
{
return dp[cur][sta];
}
LL ans = 0;
int end = flag ? bit[cur] : 9;
for (int i = 0; i <= end; ++i)
{
int newsta = getsta (sta, i);
if (zero && !i)
{
ans += dfs (cur - 1, 0, 0, flag && (i == end), 1);
}
else if (zero && i)
{
ans += dfs (cur - 1, i, three[i], flag && (i == end), 0);
}
else
{
ans += dfs (cur - 1, i, newsta, flag && (i == end), 0);
}
}
if (!flag)
{
dp[cur][sta] = ans;
}
return ans;
}
LL calc (int n)
{
int cnt = 0;
while (n)
{
bit[cnt++] = n % 10;
n /= 10;
}
return dfs (cnt - 1, 0, 0, 1, 1);
}
int main ()
{
int t;
LL l, r;
three[0] = 1;
memset (dp, -1, sizeof(dp));
for (int i = 1; i <= 9; ++i)
{
three[i] = 3 * three[i - 1];
}
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld", &l, &r);
printf("%lld\n", calc (r) - calc (l - 1));
}
return 0;
}