HDU--warmup2(1007)

这道题当初做的时候本来想用数学方法,但是取舍了一下,还是数位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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值