beautiful number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 707 Accepted Submission(s): 451
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
Sample Output
|
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);i++)
#define maxn 100005
#define mod 10001
#define ll long long
#define rush() int t;scanf("%d",&t);while(t--)
int dp[11][11],a[11];
void init()
{
mst(dp,0);
f(i,1,10)
dp[1][i]=1;
f(i,2,11)
f(j,0,10)
{
f(k,1,10)
{
if(j%k==0)
dp[i][j]+=dp[i-1][k];
}
if(!j) dp[i][j]+=dp[i-1][0];
}
}
int fun(int x)
{
int len=1;
while(x)
{
a[len++]=x%10;
x/=10;
}
a[len--]=0;
int ans=0;
for(int i=len; i>=1; i--)
{
if(i==len)
{
f(j,0,a[i])
ans+=dp[i][j];
}
else
{
if(a[i]==0)
break;
for(int j=1; j<=a[i+1]&&j<a[i]; j++)
{
if(a[i+1]%j==0)
ans+=dp[i][j];
}
if(a[i+1]%a[i]||a[i+1]<a[i])
break;
}
}
return ans;
}
int main()
{
int l,r;
init();
rush()
{
scanf("%d%d",&l,&r);
printf("%d\n",fun(r+1)-fun(l));
}
return 0;
}