beautiful number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Let
A=∑
n
i=1
a
i
∗10
n−i
(1≤a
i
≤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)?
Could you tell me the number of “beautiful number” in the interval [L,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≤10
9
)
.
Each test case begins with two integers L,R(1≤L≤R≤10
Output
For each case, output an integer means the number of “beautiful number”.
Sample Input
2 1 11 999999993 999999999
Sample Output
10 2解题思路:根据"beautiful number"的特征,在10^9范围内数的个数肯定较少,故可以先打表求出10^9内的所有"beautiful number",这里可以采用bfs打表生成,然后对于每一次的L和R,采用二分找到相应的范围。#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; int table[1000000]; int tot,L,R; bool judge(int u,int k) { while(u) { int tmp = u % 10; if(tmp % k != 0) return false; u = u / 10; } return true; } void bfs() { queue<int> q; tot = 0; for(int i = 1; i <= 9; i++) q.push(i); while(!q.empty()) { int u = q.front(); q.pop(); if(u > 1000000000) break; table[++tot] = u; for(int i = 1; i <= 9; i++) { if(u % 10 < i) break; if(judge(u,i)) q.push(u * 10 + i); } } } int main() { int t; bfs(); //先用bfs打表 scanf("%d",&t); while(t--) { scanf("%d%d",&L,&R); int l = lower_bound(table+1,table+1+tot,L) - table; int r = upper_bound(table+1,table+1+tot,R) - table; printf("%d\n",r - l); } return 0; }