hdu 4722 good numbers (带整除的数位dp)

题目链接 题意:求一个区间内所有位数字之和能被10整除的数的个数。 思路:数位dp,dfs要一个参数记录从最高位到现在的pos位置的数字之和的结果。

  1 dp[i][j] 表示长度为i,和为j的方案数
  2 记得开long long ,然而我开了那么多long long 忘了dp long long 结果wa到死。。果然大早上不清醒吗==
  3
  4
  5
  6/* ***********************************************
  7Author :111qqz
  8Created Time :2016年03月16日 星期三 08时10分19秒
  9File Name :code/hdu/4722.cpp
 10************************************************ */
 11
 12#include <cstdio>
 13#include <cstring>
 14#include <iostream>
 15#include <algorithm>
 16#include <vector>
 17#include <queue>
 18#include <set>
 19#include <map>
 20#include <string>
 21#include <cmath>
 22#include <cstdlib>
 23#include <ctime>
 24#define fst first
 25#define sec second
 26#define lson l,m,rt<<1
 27#define rson m+1,r,rt<<1|1
 28#define ms(a,x) memset(a,x,sizeof(a))
 29typedef long long LL;
 30#define pi pair < int ,int >
 31#define MP make_pair
 32
 33using namespace std;
 34const double eps = 1E-8;
 35const int dx4[4]={1,0,0,-1};
 36const int dy4[4]={0,-1,1,0};
 37const int inf = 0x3f3f3f3f;
 38LL l,r;
 39int  digit[30];
 40LL dp[30][15];  //dp 数组忘记开long long ,wa到死。。。。。。。。。日了哈士奇。
 41LL dfs ( int pos,int sum,bool limit)
 42{
 43    if (pos==0)
 44    {
 45	if (sum==0) return 1;
 46	else return 0;
 47    }
 48    if (!limit&&dp[pos][sum]!=-1) return dp[pos][sum];
 49
 50    int mx = limit?digit[pos]:9;
 51
 52    LL res = 0 ;
 53    for ( int i = 0 ; i <= mx; i ++)
 54    {
 55	res+=dfs(pos-1,(sum+i),limit&&i==mx);
 56    }
 57
 58    if (!limit) dp[pos][sum] = res;
 59
 60    return res;
 61
 62}
 63LL solve ( LL n)
 64{
 65//    if (n==0) return 1;
 66  //  if (n<=9) return 0;
 67    if (n<0) return 0;
 68    ms(digit,0);
 69    int len =  0 ;
 70    while (n)
 71    {
 72	digit[++len] = n % 10;
 73	n /= 10;
 74    }
 75
 76    return dfs(len,0,true);
 77}
 78int main()
 79{
 80	#ifndef  ONLINE_JUDGE 
 81	freopen("code/in.txt","r",stdin);
 82  #endif
 83//	ios::sync_with_stdio(false);
 84	int T;
 85	cin>>T;
 86	ms(dp,-1);
 87	int cas = 0 ; 
 88	while (T--)
 89	{
 90	    scanf("%lld %lld",&l,&r);
 91	    LL ans = solve (r)-solve(l-1);
 92
 93	    printf("Case #%d: %lld\n",++cas,ans);
 94	}
 95
 96  #ifndef ONLINE_JUDGE  
 97  fclose(stdin);
 98  #endif
 99    return 0;
100}