简单数位DP

41 篇文章 1 订阅
29 篇文章 0 订阅

https://cn.vjudge.net/problem/HDU-4722

懒得写看,代码注释吧;主要存板子


#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
const int INF=2e9+1e8;

const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++)

/**
 dp[i][j] 代表从高位到i位求和余数为j的;先写dfs,最后再把dp加入就好了,
 还有爆int 需要注意。

*/

LL dp[111][20]; 
int bits[100];
LL dfs(int pos,int sum,bool flag)//pos代表位置,sum代表求和之后的余数,flag代表是否是0~9
{
    if(pos==0) return sum==0;
    if(flag&&dp[pos][sum]!=-1) return dp[pos][sum];
    int len=flag?9:bits[pos];//flag为真 0~9,
    LL res=0;
    for(int i=0;i<=len;i++)
        res+=dfs(pos-1,(sum+i)%10,i!=len||flag);//
    if(flag) dp[pos][sum]=res;
    return res;
}


LL get(LL x)
{
    if(x<0) return 0;
    int i=0;
    while(x>0) bits[++i]=x%10,x/=10;
    return dfs(i,0,false);
}
int main()
{
   // test();
    int cas=0,ncase;
    scanf("%d",&ncase);
    memset(dp,-1,sizeof(dp));
    while(ncase--)
    {
        int i=0;
        LL l,r;
        cin>>l>>r;
        printf("Case #%d: %I64d\n",++cas,get(r)-get(l-1));
    }
    return 0;
}

/**************************************************/
/**             Copyright Notice                 **/
/**  writer: wurong                              **/
/**  school: nyist                               **/
/**  blog  : http://blog.csdn.net/wr_technology  **/
/**************************************************/



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值