其实K大于100之后结果肯定为0
因为各位的和不可能大于100左右。。。
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int digit[13], cnt;
long long dp[20][150][150];
int A, B, K, K1;
long long DFS( int pos, int pre1, int pre2, int statu ){
if( pos < 0 ){
return pre1 == 0 && pre2 == 0 ;
}
if( !statu && dp[pos][pre1][pre2] != -1 ){
return dp[pos][pre1][pre2];
}
long long ans = 0;
int end = statu ? digit[pos] : 9;
for( int i = 0; i <= end; i++ ){
ans += DFS( pos - 1, ( pre1 * 10 + i ) % K, ( pre2 + i ) % K, statu && i == end );
}
return dp[pos][pre1][pre2] = ans;
}
long long solve( int num ){
cnt = 0;
while( num ){
digit[cnt++] = num % 10;
num /= 10;
}
memset( dp, -1, sizeof( dp ) );
return DFS( cnt - 1, 0, 0, 1 );
}
int main(){
int T;
cin >> T;
while( T-- ){
cin >> A >> B >> K;
if( K > 110 ){
cout << 0 << endl;
continue;
}
cout << solve( B ) - solve( A - 1 ) << endl;
}
return 0;
}