3 1 50 500
0 1 15HintFrom 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
数位dp基础题,和网上的方法不大一样,用dp[i][j]记录以j开头的i位数里面含有49的有多少个。
此题和那个杭电的不要‘62’dp记录表示的正好相反,本来还以为计算方法一样的,结果代码一敲出来答案全是0。。。
所以自己又仔细思考了一下,这个dp记录方式如果相反的话,得到dp值是需要我们思考的!
typedef long long ll; ll dp[25][25]; void init() { memset(dp,0,sizeof(dp)); ll hh=1; for(int i=2;i<=18;i++) { for(int j=0;j<10;j++) for(int k=0;k<10;k++) { if(j==4&&k==9) dp[i][j]+=hh; else dp[i][j]+=dp[i-1][k]; } hh*=10; } }如果理解dp值的含义并自己推到一下其实我的代码很好理解的。
再就是最后的处理了。dp的最终处理绝对比算出dp值要难。。。。
将数字分解为每一位,从高位到低位枚举,不过,需要注意的是若遇到494949这个数字,我们枚举的步骤也得要理解!首先ans+=dp[6][0~3],然后ans+=[5][0~8],接着!看到49了!也就意味着49****后面4个数字怎么变都会含有49,那么我们在此直接ans+=4950!(因为从0到4949有4950个数)
?然后怎么办呢?继续?,这就是我说的要理解枚举的步骤,我们首先分别枚举了以0,1,2,3开头的6位数含有49的个数,剩下400000到494949含有49的个数,然后我们枚举了400000到489999的含有49的个数,剩下490000到494949含有49的个数,接着,我们就直接加上了4950,因为490000到494949所有的数字都有49,直接加上就ok!然后我们不就枚举完毕了吗?!!我们还需要继续往下用ans去加dp[0~4]的值吗??我觉得已经说得比较明了了吧!所以枚举这个过程一定要自己理解!
#include <iostream> #include <stdio.h> #include<string.h> #include<math.h> using namespace std; typedef long long ll; ll dp[25][25]; void init() { memset(dp,0,sizeof(dp)); ll hh=1; for(int i=2;i<=18;i++) { for(int j=0;j<10;j++) for(int k=0;k<10;k++) { if(j==4&&k==9) dp[i][j]+=hh; else dp[i][j]+=dp[i-1][k]; } hh*=10; } } int main() { init(); int t; //for(int i=1;i<=18;i++){for(int j=0;j<10;j++)cout<<dp[i][j]<<' ';cout<<endl;} scanf("%d",&t); ll n; while(t--) { int digit[25]={0},bj=0; scanf("%lld",&n);ll nn=n,ans=0; while(n) {digit[++bj]=n%10;n/=10;} bool flag=0; for(int i=bj;i>0;i--) { for(int j=0;j<digit[i];j++) ans+=dp[i][j]; if(flag==1) break; if(digit[i]==4&&digit[i-1]==9&&flag==0) { ll tem=0; if(bj>2) { for(int mm=i-2;mm>0;mm--) tem*=10,tem+=digit[mm]; ans+=tem+1; }else ans++; flag=1; } } cout<<ans<<endl; } return 0; }