这道题当初做的时候本来想用数学方法,但是取舍了一下,还是数位DP比较好明白。
c[i][(j + k) % 10] += c[i - 1][j];
c[i][(x+j)%10]++;
这两个是主要的公式。
c[][]是存储数位加和的数组。
#pragma warning(disable:4786)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<ctime>
#include<string.h>
using namespace std;
#define pi acos(-1.0)
//#define LL __int64
typedef long long LL;
#define INF 0x7fffffffffffffffdin)
#define bug puts("hear!")
#define inf 0x7fffffff
#define eps 1e-10
#define FRE freopen("in.txt","r",st
#define E exp(1.0)
#define mod 1000000007
long long c[20][11];
long long get_d (int a[], int n)
{
int i, j, k;
memset(c,0,sizeof(c));
int x = 0;
for(i = 1; i <= n; i++){
for(j = 0; j < 10; j++){
for(k = 0; k < 10; k++){
c[i][(j + k) % 10] += c[i - 1][j];
}
}
for(j = 0; j < a[i]; j++) c[i][(x+j)%10]++;
x = (x + a[i]) % 10;
}
if(x == 0) c[n][0]++;
return c[n][0];
}
int main()
{
int T, tt = 0;
scanf("%d",&T);
while(T--){
int s[20], n, i, j, num = 0;
long long ans = 0;
char a[20],b[20];
scanf("%s %s", a, b);
n = strlen(b);
for(i = 0; i < n; i++) s[i + 1] = b[i] - '0';
ans += get_d(s,n);
n = strlen(a);
for(i = 0; i < n; i++){
s[i + 1] = a[i] - '0';
num += s[i + 1];
}
ans -= get_d(s,n);
if(num % 10 == 0) ans++;
printf("Case #%d: %I64d\n", ++tt, ans);
}
return 0;
}