poj 3909 --Lucky numbers(搜索)

 

 

题源:http://poj.org/problem?id=3909

 

 

题意:very lucky number 定义:我们称只包含 4和 7的为lucky number ,


由一个或多个  lucky number的乘积得到的数我们就叫做 very lucky number  

 

解法:只由4和7组成的 只有8000多个

 

先找出这些数 ( lucky number )

 

再由这些数搜索出  very lucky number  

 

大概有35W个 

 

 

//3520K 125MS #include <iostream> #include <algorithm> using namespace std; __int64 num[1000000]; int cnt=0; __int64 Maxn=1000000000000; __int64 a,b,sum; char str[20]; void dfs(int n) { if(n==12) return ; str[n]='4'; str[n+1]=0; sscanf(str,"%I64d",&num[cnt++]); dfs(n+1); str[n]='7'; str[n+1]=0; sscanf(str,"%I64d",&num[cnt++]); dfs(n+1); } void dfs2(__int64 t,int pos) { for(int i=pos;i<sum;i++) { __int64 temp=t*num[i]; if(temp>0&&temp<=Maxn) { num[cnt++]=temp; dfs2(temp,i); } else break; } } void init() { //search the number only contain '4' and '7' dfs(0); sum=cnt; sort(num,num+cnt); //srarch the all lucky number include 10^12 dfs2(1,0); sort(num,num+cnt); //delete repeated number cnt=unique(num,num+cnt)-num; num[cnt]=10000000000010; // printf("%d/n",cnt); // for(int i=0;i<11;i++) // printf("%I64d/n",num[i]); } //besrarch //the fist number larger or equal than a //the fist number larger than b int Tsearch(__int64 t,int index) { int low=0,high=cnt,mid,i; while (low+1<high) { mid=(low+high)>>1; if(num[mid]<=t) low=mid; else high=mid; } if(index==1) //B { for(int i=low;;i++) if(num[i]>t) return i; } else //A { for( i=low;;i++) if(num[i]>=t) return i; } } int solve() { int ans; ans=Tsearch(b,1)-Tsearch(a,0); if(ans<0) ans=0; return ans; } int main() { init(); int t; scanf("%d",&t); while(t--) { scanf("%I64d%I64d",&a,&b); printf("%d/n",solve()); } }

你可能感兴趣的:(include,Numbers)