我的wa代码 wa到吐血,找不到错。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 using namespace std;
5 __int64 num[1000000],sum,cnt;
6 const __int64 maxn=1000000000100;
7 void dfs(__int64 u,__int64 d)
8 {
9 if(d>12) return;
10 dfs(num[sum++]=u*10+4,d+1);
11 dfs(num[sum++]=u*10+7,d+1);
12 }
13 void Dfs(__int64 t,int pos)
14 {
15 for(int i=pos;i<sum;i++)
16 {
17 __int64 tem=t*num[i];
18 if(tem>0&&tem<=maxn) {
19 num[cnt++]=tem;
20 Dfs(tem,i);
21 }
22 else break;
23 }
24 }
25 void init()
26 {
27 sum=0;
28 dfs(0,1);cnt=sum;
29 sort(num,num+cnt);
30 Dfs(1,0);
31 sort(num,num+cnt);
32 cnt=unique(num,num+cnt)-num;
33 num[cnt]=maxn+1;
34 }
35 __int64 sch(__int64 u)
36 {
37 __int64 low=0,high=cnt,mid;
38 while(low<high)
39 {
40 mid=(low+high)>>1;
41 if(num[mid]<u)
42 low=mid+1;
43 else if(num[mid]>u) high=mid-1;
44 else {high=low=mid; break;}
45 }
46 mid=(high+low)>>1;
47 if(num[mid]!=u)
48 return mid-1;
49 else return mid;
50 }
51 int main()
52 {
53 init();__int64 cas,x,y;
54 scanf("%I64d",&cas);
55 while(cas--)
56 {
57 scanf("%I64d%I64d",&x,&y);
58 printf("%I64d\n",sch(y)-sch(x-1));
59 }
60 return 0;
61 }
网上大牛解法:类似,不过运用了vector
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<algorithm>
5 #include<vector>
6 using namespace std;
7 typedef __int64 lld;
8 vector<lld> lu,res;
9 int tot;
10 void dfs(lld num,int de){
11 if(de==12) return ;
12 lu.push_back(num);
13 dfs(num*10+4,de+1);
14 dfs(num*10+7,de+1);
15 }
16 void dfs1(int x,lld num){
17 for(int i=x;i<lu.size();i++){
18 if(1e12/num<lu[i]) return;
19 res.push_back(num*lu[i]);
20 dfs1(i,num*lu[i]);
21 }
22 }
23 void init(){
24 lu.clear();res.clear();
25 dfs(4,0);
26 dfs(7,0);
27 sort(lu.begin(),lu.end());
28 dfs1(0,1);
29 sort(res.begin(),res.end());
30 tot=unique(res.begin(),res.end())-res.begin();
31 }
32 int main(){
33 init();
34 int t;
35 lld a,b;
36 scanf("%d",&t);
37 while(t--){
38 scanf("%I64d%I64d",&a,&b);
39 int l=lower_bound(res.begin(),res.begin()+tot,a)-res.begin();
40 int r=upper_bound(res.begin(),res.begin()+tot,b)-res.begin();
41 printf("%d\n",r-l);
42 }
43 }
ac代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream>
2 #include<algorithm>
3 #include<vector>
4 #include<cstdio>
5 using namespace std;
6 vector<__int64>num;
7 __int64 sum,cnt;
8 const __int64 maxn=1000000000100;
9 void dfs(__int64 u,__int64 d)
10 {
11 if(d>12) return;
12 num.push_back(u*10+4);
13 num.push_back(u*10+7);
14 dfs(u*10+4,d+1);
15 dfs(u*10+7,d+1);
16 }
17 void Dfs(__int64 t,int pos)
18 {
19 for(int i=pos;i<sum;i++)
20 {
21 __int64 tem=t*num[i];
22 if(tem>0&&tem<=maxn) {
23 num.push_back(tem);
24 Dfs(tem,i);
25 }
26 else break;
27 }
28 }
29 void init()
30 {
31 num.clear();
32 dfs(0,1);sum=num.size();
33 sort(num.begin(),num.end());
34 Dfs(1,0);
35 sort(num.begin(),num.end());
36 cnt=unique(num.begin(),num.end())-num.begin();
37 }
38 int main()
39 {
40 init();__int64 cas,x,y;
41 scanf("%I64d",&cas);
42 while(cas--)
43 {
44 scanf("%I64d%I64d",&x,&y);
45 int l=lower_bound(num.begin(),num.begin()+cnt,x)-num.begin();
46 int r=upper_bound(num.begin(),num.begin()+cnt,y)-num.begin();
47 printf("%d\n",r-l);
48 }
49 return 0;
50 }
终于a掉了:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 using namespace std;
5 __int64 num[1000000],sum,cnt;
6 const __int64 maxn=1000000000100;
7 void dfs(__int64 u,__int64 d)
8 {
9 if(d>12) return;
10 dfs(num[sum++]=u*10+4,d+1);
11 dfs(num[sum++]=u*10+7,d+1);
12 }
13 void Dfs(__int64 t,int pos)
14 {
15 for(int i=pos;i<sum;i++)
16 {
17 __int64 tem=t*num[i];
18 if(tem>0&&tem<=maxn) {
19 num[cnt++]=tem;
20 Dfs(tem,i);
21 }
22 else break;
23 }
24 }
25 void init()
26 {
27 sum=0;
28 dfs(0,1);cnt=sum;
29 sort(num,num+cnt);
30 Dfs(1,0);
31 sort(num,num+cnt);
32 cnt=unique(num,num+cnt)-num;
33 num[cnt]=maxn+1;
34 }
35 __int64 sch(__int64 u)
36 {
37 __int64 low=0,high=cnt,mid;
38 while(low<high)
39 {
40 mid=(low+high)>>1;
41 if(num[mid]<u)
42 low=mid+1;
43 else if(num[mid]>u) high=mid-1;
44 else {high=low=mid; break;}
45 }
46 mid=(high+low)>>1;
47 if(num[mid]>u)
48 return mid-1;
49 else return mid;
50 }
51 int main()
52 {
53 init();__int64 cas,x,y;
54 scanf("%I64d",&cas);
55 while(cas--)
56 {
57 scanf("%I64d%I64d",&x,&y);
58 printf("%I64d\n",sch(y)-sch(x-1));
59 }
60 return 0;
61 }