FZU 2113 Jason的特殊爱好 (数位dp)
题意:统计区间[a,b]里数字1出现的次数。
思路:数位dp。
收获是,dfs传递的参数可能是为了判断符合条件的答案(比如不要62中的preis6等)
但是也可能是在统计答案信息。。。pos等于0的时候返回值未必是1和0.。。
然后傻逼fzu。。。long long 必须交 I64d..因为这个wa到死。
傻逼fzu,毁我青春。
1/* ***********************************************
2Author :111qqz
3Created Time :Thu 29 Sep 2016 02:20:09 AM CST
4File Name :code/fzu/2113.cpp
5************************************************ */
6#include <cstdio>
7#include <cstring>
8#include <iostream>
9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31LL l,r;
32int digit[30];
33LL dp[30][30];
34LL dfs( int pos,int cnt,bool limit)
35{
36 if (pos==0) return cnt;
37 if (!limit&&dp[pos][cnt]!=-1) return dp[pos][cnt];
38 int mx = limit?digit[pos]:9;
39 LL res = 0 ;
40 for ( int i = 0 ; i <= mx ; i++)
41 {
42 if (i==1)
43 res = res + dfs(pos-1,cnt+1,limit&&i==mx);
44 else res = res + dfs(pos-1,cnt,limit&&i==mx);
45 }
46 if (!limit) dp[pos][cnt] = res;
47 return res;
48}
49LL solve( LL n)
50{
51 int len = 0 ;
52 ms(digit,0);
53 while (n)
54 {
55 digit[++len] = n;
56 n/=10;
57 }
58 ms(dp,-1);
59 return dfs(len,0,true);
60}
61int main()
62{
63 #ifndef ONLINE_JUDGE
64 freopen("code/in.txt","r",stdin);
65 #endif
66 ms(dp,-1);
67 while (~scanf("%I64d%I64d",&l,&r))
68 {
69 LL ans = solve(r) - solve(l-1);
70 printf("%I64d\n",ans);
71 }
72 #ifndef ONLINE_JUDGE
73 fclose(stdin);
74 #endif
75 return 0;
76}