C. Classy Numbers

C. Classy Numbers

               time limit per test

               3 seconds

               memory limit per test

               256 megabytes


Let's call some positive integer classy if its decimal representation contains no more than 3

non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000

are not.

You are given a segment [L;R]

. Count the number of classy integers x such that L≤x≤R

.

Each testcase contains several segments, for each of them you are required to solve the problem separately.

Input

The first line contains a single integer T  (1≤T≤104) — the number of segments in a testcase.Each of the next T lines contains two integers Li and Ri (1≤Li≤Ri≤1018).

Output

Print T lines — the i-th line should contain the number of classy integers on a segment [Li;Ri]

.

Example

Input

4
1 1000
1024 1024
65536 65536
999999 1000001

Output

1000
1
0
2

 

AC代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
int a[20];
int dp[20][2][4];
int dfs(int pos, int check, bool limit)
{
	if (pos == -1) return 1;
	if (!limit && dp[pos][limit][check] != -1) return dp[pos][limit][check];
	if (check >= 3) return dp[pos][limit][check] = 1;
	int up = limit ? a[pos] : 9;
	int tmp = 0;
	for (int i = 0; i <= up; i++)
	{
		tmp += dfs(pos - 1, check + (i ? 1 : 0), limit && i == a[pos]);
	}
	if (!limit) dp[pos][limit][check] = tmp;
	return tmp;
}
int solve(long long x)
{
	int pos = 0;
	while (x)
	{
		a[pos++] = x % 10;
		x /= 10;
	}
	return dfs(pos - 1, 0, true);
}
int main()
{
	long long le, ri;
	int T;
	cin>>T;
	//memset(dp,-1,sizeof dp);可優化
	while (T--)
	{
	    scanf("%lld%lld", &le, &ri);
		memset(dp, -1, sizeof dp);
		printf("%d\n", solve(ri) - solve(le - 1));
	}
	return 0;
}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章