HDOJ 4722 - Good Numbers 数位dp

本文介绍了一种使用数位dp方法解决在指定区间内寻找满足特定条件的数字数量的问题。具体而言,当一个数字的每一位之和为10的倍数时,我们称其为GoodNumber。通过数位dp技术,我们可以高效地计算出在给定范围内的GoodNumber总数。

                 题意:

                          如果一个数每一位之和为10的倍数..则称该数为Good Number..问A~B区间有多少个Good Numbers...

                 题解:

                          简单的数位dp...dp[x][y]代表位数为x时..各位之和%10=y的个数...然后用这个来更新答案..


Program:

#include<iostream>
#include<stack>
#include<queue>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<cmath>
#define ll long long
#define oo 1000000007
#define MAXN 100010
using namespace std;   
ll dp[20][10];
char s[20];
ll count(ll x)
{ 
      int len,i,j;
      ll p=0,ans=0; 
      sprintf(s+1,"%I64d",x);
      len=strlen(s+1);
      for (i=1;i<=len;i++)
      {
             for (j=0;j<s[i]-'0';j++)
                 ans+=dp[len-i][(1000+10-j-p)%10];
             p+=s[i]-'0';
      }
      if (p%10==0) ans++;
      return ans;
} 
void PreWork()
{
      int i,j,x;
      memset(dp,0,sizeof(dp));
      dp[0][0]=1; 
      for (i=1;i<=18;i++)
         for (x=0;x<10;x++)
            for (j=0;j<10;j++)
               dp[i][(j+x)%10]+=dp[i-1][j];
}
int main()
{ 
      int C,cases;
      ll A,B; 
      PreWork();
      scanf("%d",&C);
      for (cases=1;cases<=C;cases++)
      {
               scanf("%I64d%I64d",&A,&B);
               printf("Case #%d: %I64d\n",cases,count(B)-count(A-1));
      }
      return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值