hdu 4722 good numbers (带整除的数位dp)
题目链接 题意:求一个区间内所有位数字之和能被10整除的数的个数。 思路:数位dp,dfs要一个参数记录从最高位到现在的pos位置的数字之和的结果。 dp[i][j] 表示长度为i,和为j的方案数。 记得开long long ,然而我开了那么多long long 忘了dp 的long long 结果wa到死。。果然大早上不清醒吗==
/* ***********************************************
Author :111qqz
Created Time :2016年03月16日 星期三 08时10分19秒
File Name :code/hdu/4722.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6LL l,r;
7int digit[30];
8LL dp[30][15]; //dp 数组忘记开long long ,wa到死。。。。。。。。。日了哈士奇。
9LL dfs ( int pos,int sum,bool limit)
10{
11 if (pos==0)
12 {
13 if (sum==0) return 1;
14 else return 0;
15 }
16 if (!limit&&dp[pos][sum]!=-1) return dp[pos][sum];
int mx = limit?digit[pos]:9;
1 LL res = 0 ;
2 for ( int i = 0 ; i <= mx; i ++)
3 {
4 res+=dfs(pos-1,(sum+i),limit&&i==mx);
5 }
if (!limit) dp[pos][sum] = res;
return res;
1}
2LL solve ( LL n)
3{
4// if (n==0) return 1;
5 // if (n<=9) return 0;
6 if (n<0) return 0;
7 ms(digit,0);
8 int len = 0 ;
9 while (n)
10 {
11 digit[++len] = n % 10;
12 n /= 10;
13 }
1 return dfs(len,0,true);
2}
3int main()
4{
5 #ifndef ONLINE_JUDGE
6 freopen("code/in.txt","r",stdin);
7 #endif
8// ios::sync_with_stdio(false);
9 int T;
10 cin>>T;
11 ms(dp,-1);
12 int cas = 0 ;
13 while (T--)
14 {
15 scanf("%lld %lld",&l,&r);
16 LL ans = solve (r)-solve(l-1);
printf("Case #%d: %lld\n",++cas,ans);
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}