您的位置:首页 > 其它

(hdu 5179)beautiful number(数位DP)

2018-04-10 11:42 543 查看
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5179

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 860 Accepted Submission(s): 559

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

BestCoder Round #31

题意:求[a,b]内的数的前一位都是后一位数的倍数的数的个数

分析:用模板

不同的是 用dp[pos][pre] (pre代表的是前一位数) 而不是以前用的状态0,1,2了

并且还要判断是否有 前导0

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <string>
#include <set>
#include <queue>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define pb(x) push_back(x)
typedef long long LL;
typedef unsigned long long ull;
const int mod=1e9+7;
const double eps=1e-6;
const LL INF=0x3f3f3f3f;
const int N=200+5;
int dp[20][20],digit[20];
int dfs(int pos,int pre,bool lead,bool bounded)
///lead==1代表有前导0  lead==0则没有
{
if(pos==0) return 1;
int& ret=dp[pos][pre];
if(!bounded&&ret!=-1)
return ret;
int ans=0;
int end=bounded?digit[pos]:9;
for(int i=0; i<=end; i++)
{
if(lead||i&&pre>=i&&pre%i==0)
ans+=dfs(pos-1,i,lead&&!i,bounded&&i==end);
}
if(!bounded&&!lead) ret=ans;///不是上界且没有前导0的
return ans;
}
int cal(int x)
{
int len=0;
while(x)
{
digit[++len]=x%10;
x/=10;
}
return dfs(len,0,true,true);
}
int main()
{
int a,b,T;
mem(dp,-1);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
printf("%d\n",cal(b)-cal(a-1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数位dp