【HDOJ】5179 beautiful number
DFS。
1 /* 5179 */ 2 #include <iostream> 3 #include <algorithm> 4 #include <map> 5 #include <cstdio> 6 #include <cstring> 7 using namespace std; 8 9 #define MAXN 1500 10 11 map<int, bool> tb; 12 int a[MAXN], n, v; 13 int buf[7][5] = { 14 {1,1}, 15 {3,6,2,1}, {4,8,4,2,1}, 16 {3,6,3,1}, {3,9,3,1}, 17 {2,5,1}, 18 {2,7,1} 19 }; 20 21 void dfs(int beg, int len, int val) { 22 int i, k, tmp = 10*val; 23 if (len >= 10) 24 return ; 25 for (i=beg; i<=buf[v][0]; ++i) { 26 k = tmp + buf[v][i]; 27 if (!tb[k]) { 28 tb[k] = true; 29 a[n++] = k; 30 } 31 dfs(i, len+1, k); 32 } 33 } 34 35 void init() { 36 int i, j, k; 37 38 n = 0; 39 for (v=0; v<7; ++v) 40 dfs(1, 1, 0); 41 sort(a, a+n); 42 } 43 44 int main() { 45 int t, l, r; 46 int i, j, k; 47 int ans; 48 49 #ifndef ONLINE_JUDGE 50 freopen("data.in", "r", stdin); 51 freopen("data.out", "w", stdout); 52 #endif 53 54 init(); 55 scanf("%d", &t); 56 while (t--) { 57 scanf("%d %d", &l, &r); 58 ans = 0; 59 for (i=0; i<n; ++i) 60 if (l<=a[i] && r>=a[i]) 61 ++ans; 62 printf("%d\n", ans); 63 } 64 65 return 0; 66 }