[关闭]
@darkproject 2017-04-08T05:17:49.000000Z 字数 2850 阅读 1062

集训队大一下学期第一周作业(数位dp专题)

数位dp


为什么是数位dp专题呢233,因为只做了一道和这个相关的于是就叫数位dp
了,同时自己又去补了几道数位dp水题。
B - How Many Zeroes?

统计一个区间自然数据中有多少个0,数据范围为无符号32位正整数,裸的数位dp,这里需要注意的是我们需要在模板在多出一个状态去判断前导零对结果造成的影响,自然002这种数据我们是不能算作0的计数的
ps:cnt表示数据前面有cnt个0,pre为前导0状态表示

  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. ll dp[99][99];
  5. ll a[99];
  6. ll n,m,t;
  7. ll dfs(ll pos,ll cnt,int pre,bool limit)
  8. {
  9. if(pos==0) return cnt;
  10. if(!limit&&dp[pos][cnt]!=-1&&pre)
  11. return dp[pos][cnt];
  12. int mxr=limit?a[pos]:9;
  13. ll ret=0;
  14. for(ll i=0;i<=mxr;i++)
  15. {
  16. ret+=dfs(pos-1,cnt+(pre&&i==0),pre||i,limit&&(i==mxr));
  17. }
  18. if(!limit&&pre) dp[pos][cnt]=ret;
  19. return ret;
  20. }
  21. ll solve(ll x)
  22. {
  23. memset(dp,-1,sizeof(dp));
  24. ll pos=0;
  25. while(x)
  26. {
  27. a[++pos]=x%10;
  28. x/=10;
  29. }
  30. return dfs(pos,0,0,1);
  31. }
  32. int main()
  33. {
  34. int temp=1;
  35. cin>>t;
  36. while(t--)
  37. {
  38. cin>>m>>n;
  39. printf("Case %d: ",temp++);
  40. if(m==0)
  41. cout<<solve(n)-solve(m-1)+1<<endl;
  42. else
  43. cout<<solve(n)-solve(m-1)<<endl;
  44. }
  45. return 0;
  46. }

C - Jason的特殊爱好 FZU - 2113

a,b(1<=a,b<=10^18)。
求数据区间含有多少个1,一个套路,只是这里不需要再对前导0进行限制啦,于是可以去掉判断前导0的状态pre

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. typedef long long ll;
  5. using namespace std;
  6. ll c,b;
  7. ll a[25];
  8. ll dp[25][25];
  9. ll dfs(ll pos,ll cnt,bool limit)
  10. {
  11. if(pos==0) return cnt;
  12. if(!limit&&dp[pos][cnt]!=-1)
  13. return dp[pos][cnt];
  14. int up=limit? a[pos]:9;
  15. ll ret=0;
  16. for(int i=0;i<=up;i++)
  17. ret+=dfs(pos-1,cnt+(i==1),limit&&(i==up));
  18. if(!limit)
  19. return dp[pos][cnt]=ret;
  20. return ret;
  21. }
  22. ll solve(ll x)
  23. {
  24. memset(dp,-1,sizeof(dp));
  25. ll pos=0;
  26. while(x)
  27. {
  28. a[++pos]=x%10;
  29. x/=10;
  30. }
  31. return dfs(pos,0,1);
  32. }
  33. int main()
  34. {
  35. while(cin>>c>>b)
  36. {
  37. cout<<solve(b)-solve(c-1)<<endl;
  38. }
  39. return 0;
  40. }

E - Bomb HDU - 3555

还是一样的套路,一样的感觉,给你一个数据n,求1到n这个区间内含多少个49.这里我们需要判断前缀数字于是需要一个pre状态,用sta判断是否为数字4开头,如果是就状态传递下去

  1. #include <stdio.h>
  2. #include <string.h>
  3. typedef long long ll;
  4. ll t,n;
  5. ll a[66],dp[66][66][66];
  6. ll dfs(ll pos,int sta,int pre,bool limit)
  7. {
  8. if(pos==0) return sta;
  9. if(!limit&&dp[pos][pre][sta]!=-1)
  10. return dp[pos][pre][sta];
  11. int up=limit? a[pos]:9;
  12. ll ret=0;
  13. for(int i=0;i<=up;i++)
  14. ret+=dfs(pos-1,sta||(pre==4&&i==9),i,limit&&(i==up));
  15. if(!limit)
  16. return dp[pos][pre][sta]=ret;
  17. return ret;
  18. }
  19. ll solve(ll x)
  20. {
  21. ll pos=0;
  22. while(x)
  23. {
  24. a[++pos]=x%10;
  25. x/=10;
  26. }
  27. return dfs(pos,0,0,1);
  28. }
  29. int main()
  30. {
  31. scanf("%d",&t);
  32. memset(dp,-1,sizeof(dp));
  33. while(t--)
  34. {
  35. scanf("%lld",&n);
  36. printf("%lld\n",solve(n));
  37. }
  38. return 0;
  39. }

G - windy数 HYSBZ - 1026

题意:windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,
在A和B之间,包括A和B,总共有多少个windy数?
解析:
这里不仅要讨论前导零,我们也需要用pre状态讨论前缀数字,于是可以新建一个flag状态来另讨论前导0
需要注意的一点根据hint来看,002也算作windy数.也就是说前面全是0的情况要另作判断,其他情况自然就是flag为1即不含前导0的i-pre关系判断即可,具体看if语句的处理部分

  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. ll a,b;
  5. ll num[66],dp[66][66][2];
  6. ll dfs(ll pos,ll pre,int flag,bool limit)
  7. {
  8. if(pos==0) return 1;
  9. if(!limit&&dp[pos][pre][flag]!=-1)
  10. return dp[pos][pre][flag];
  11. int up=limit? num[pos]:9;
  12. ll ret=0;
  13. for(int i=0;i<=up;i++)
  14. {
  15. if(!flag||abs(i-pre)>=2)
  16. ret+=dfs(pos-1,i,flag||i,limit&&(i==up));
  17. }
  18. if(!limit)
  19. return dp[pos][pre][flag]=ret;
  20. return ret;
  21. }
  22. ll solve(ll x)
  23. {
  24. memset(dp,-1,sizeof(dp));
  25. ll pos=0;
  26. while(x)
  27. {
  28. num[++pos]=x%10;
  29. x/=10;
  30. }
  31. return dfs(pos,0,0,1);
  32. }
  33. int main()
  34. {
  35. cin>>a>>b;
  36. cout<<solve(b)-solve(a-1)<<endl;
  37. return 0;
  38. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注