1276 - Very Lucky Numbers
Posted SJY
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于1276 - Very Lucky Numbers的主要内容,如果未能解决你的问题,请参考以下文章
Lucky Numbers (easy) CodeForces - 96B
1380. Lucky Numbers in a Matrix
LeetCode --- 1380. Lucky Numbers in a Matrix 解题报告