1276 - Very Lucky Numbers
Time Limit: 3 second(s) Memory Limit: 32 MB

According to some theory 4 and 7 are lucky digits, and all the other digits are not lucky. A lucky number is a number that contains only lucky digits in decimal notation. A very lucky number is a number that can be expressed as a product of several lucky numbers. A lucky number by itself is considered to be very lucky. For example, numbers 47, 49, 112 are very lucky.

Your task is to calculate the number of very lucky numbers that are not less than A and not greater than B.

Input

Input starts with an integer T (≤ 8000), denoting the number of test cases.

Each case starts with a line containing two integers A and B (1 ≤ A ≤ B ≤ 1012).

Output

For each case, print the case number and the result.

Sample Input

Output for Sample Input

4

1 2

88 99

112 112

1 100

Case 1: 0

Case 2: 0

Case 3: 1

Case 4: 10

Note

Very lucky numbers for the last sample input are 4, 7, 16, 28, 44, 47, 49, 64, 74 and 77.

题意:只由4和7构成的数是幸运数,然后由这些数数构成的数是非常幸运数字,然后问你在某个区间里有多少个这样的数;

思路:先dfs打表出幸运数字,然后再dfs打出非常幸运数字,dfs时用map去重,最后二分求解。

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stdlib.h>
7 #include<math.h>
8 #include<stack>
9 #include<vector>
10 #include<map>
11 using namespace std;
12 typedef long long LL;
13 map<LL,int>my;
14 LL ans[10000];
15 LL bns[1000000];
16 int N;
17 int M;
18 void dfs1(LL u,int k);
19 void dfs(int n,int m,LL u);
20 int main(void)
21 {
22 int i,j,k;
23 N=0;
24 M=0;
25 dfs(0,12,0);
26 sort(ans,ans+N);
27 dfs1(1,0);
28 sort(bns,bns+M);
29 scanf("%d",&k);
30 int s;
31 LL n,m;
32 for(s=1; s<=k; s++)
33 {
34 scanf("%lld %lld",&n,&m);
35 n-=1;
36 int l=0;
37 int r=M-1;
38 int id=-1;
39 while(l<=r)
40 {
41 int mid=(l+r)/2;
42 if(bns[mid]<=n)
43 {
44 id=mid;
45 l=mid+1;
46 }
47 else r=mid-1;
48 }
49 int ic=-1;
50 l=0;
51 r=M-1;
52 while(l<=r)
53 {
54 int mid=(l+r)/2;
55 if(bns[mid]<=m)
56 {
57 ic=mid;
58 l=mid+1;
59 }
60 else r=mid-1;
61 }
62 printf("Case %d: ",s);
63 printf("%d\n",ic-id);
64 }
65 return 0;
66 }
67 void dfs(int n,int m,LL u)
68 {
69 if(n==m)
70 return ;
71 ans[N++]=u*10+4;
72 dfs(n+1,m,u*10+4);
73 ans[N++]=u*10+7;
74 dfs(n+1,m,u*10+7);
75 }
76 void dfs1(LL u,int k)
77 {
78 int i;
79 if(!my[u])
80 {
81 my[u]=1;
82 for(i=k; i<N; i++)
83 {
84 if(ans[i]<=1e12/u)
85 {
86 if(my[ans[i]*u]==0)
87 {
88 bns[M++]=ans[i]*u;
89 dfs1(ans[i]*u,i);
90 }
91 }
92 else
93 {
94 return ;
95 }
96 }
97 }
98 }

最新文章

  1. Struts1.x 中的 Validate 框架
  2. select下拉框插件(转)
  3. out参数,ref参数,params参数数组
  4. ASP.NET并发处理
  5. 练习PYTHON之EPOLL
  6. CentOS 修改默认语言
  7. Codechef Nuclear Reactors 题解
  8. usaco月赛,2017.1总结
  9. (3)打造简单OS-MBR引导区转移加载简单程序(突破512限制)
  10. HTTP 协议常见的状态码
  11. Java模拟斗地主发牌和洗牌
  12. Mac CLion下OpenGL环境配置
  13. 浏览器输入URL按回车后都经历了什么?
  14. 小强学渲染之OpenGL的CPU管线
  15. 2-SAT超入门讲解
  16. ubuntu16.04之sudo问题
  17. 对SQL SERVER数据类型理解最好的一篇文章
  18. Netty 源码(一)服务端启动
  19. 以太网的 MAC 层
  20. PHP学习5——异常处理

热门文章

  1. C++栈溢出
  2. 使用systemd将iptables规则在docker启动后自动导入
  3. sed 修改文件
  4. javaSE高级篇3 — 网络编程 — 更新完毕
  5. 日常Java 2021/10/13
  6. 修改linux文件权限命令:chmod 转载至 Avril 的随笔
  7. 剑指 Offer 10- I. 斐波那契数列
  8. Python3的类注意事项
  9. 【Java基础】JAVA中优先队列详解
  10. AJAX - Http 中 post 和 get 的区别