AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
long long dp[30][30][2];
int digit[30];
int num[30];
long long DFS( int pos, int beginpos, bool limit, bool pre_all_0 ){
if( pos <= 0 ){
return 1;
}
if( !limit && dp[pos][beginpos][pre_all_0==true] != EOF ){
return dp[pos][beginpos][pre_all_0==true];
}
int end = limit ? digit[pos] : 9;
long long sum = 0;
for( int i = 0; i <= end; i++ ){
if( pre_all_0 ){
num[pos] = i;
sum += DFS( pos - 1, beginpos - !i, limit && i == end, pre_all_0 && !i );
}else{
int temp = ( beginpos + 1 ) / 2;
bool contion = beginpos % 2 == 1 ? pos < temp : pos <= temp;
if( contion ){
if( num[beginpos+1-pos] == i ){
sum += DFS( pos - 1, beginpos, limit && i == end, pre_all_0 );
}
}else{
num[pos] = i;
sum += DFS( pos - 1, beginpos, limit && i == end, pre_all_0 );
}
}
}
if( !limit ){
dp[pos][beginpos][pre_all_0==true] = sum;
}
return sum;
}
long long solve( long long N ){
int len = 0;
while( N ){
digit[++len] = N % 10;
N /= 10;
}
return DFS( len, len, true, true );
}
int main(){
int T, Case = 1;
long long A, B;
cin >> T;
while( T-- ){
memset( dp, -1, sizeof( dp ) );
cin >> A >> B;
if( A > B ){
swap( A, B );
}
cout << "Case " << Case++ << ": "<< solve( B ) - solve( A - 1 ) << endl;
}
return 0;
}