首页 > 其他 > 详细

hdu 5179 数位dp

时间:2015-03-02 11:18:52      阅读:524      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179

beautiful number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 198    Accepted Submission(s): 116


Problem Description
Let A=∑ni=1ai?10n?i(1≤ai≤9)(n is the number of A‘s digits). We call A as “beautiful number” if and only if a[i]≥a[i+1] when 1≤i<n and a[i]mod a[j]=0 when 1≤i≤n,i<j≤n(Such as 931 is a "beautiful number" while 87 isn‘t).
Could you tell me the number of “beautiful number” in the interval [L,R](including L and R)?
 

Input
The fist line contains a single integer T(about 100), indicating the number of cases.
Each test case begins with two integers L,R(1≤L≤R≤109).
 

Output
For each case, output an integer means the number of “beautiful number”.
 

Sample Input
2 1 11 999999993 999999999
 

Sample Output
10 2
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5181 5180 5179 5178 5177 
 


思路:数位dp模板题~~

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;

int dp[25][25][3];
int bit[25];

int dfs(int pos,int pre,int flag,int z)
{
  if(pos==0)return 1;
  if( !flag && dp[pos][pre][z]!=-1)return dp[pos][pre][z];
  int end=flag?bit[pos]:9;
  int i=0,ans=0;
  if(z==0)i=1;
  for(;i<=end;i++)
  {
       if(pre==0)
         ans+=dfs(pos-1,i,flag&&(i==end),z&&(i==0));
       else
       {
           if( pre>=i && pre%i==0)ans+=dfs(pos-1,i,flag&&(i==end),z&&(i==0));
       }
  }
  if(!flag)dp[pos][pre][z]=ans;
  return ans;
}

int cal(int x)
{
 int len=0;
 memset(bit,0,sizeof(bit));
 memset(dp,-1,sizeof(dp));
 while(x)
 {
   bit[++len]=x%10;
   x/=10;
 }
 return dfs(len,0,1,1);
}

int main()
{
  int l,r;
  int T;
  cin>>T;
  while(T--)
  {
      scanf("%d%d",&l,&r);
      int s1=cal(r);
      int s2=cal(l-1);
      printf("%d\n",s1-s2);
  }
  return 0;
}


hdu 5179 数位dp

原文:http://blog.csdn.net/liusuangeng/article/details/44014889

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!