poj 3909 二分

来源:互联网 发布:淘宝代销发货怎么联系 编辑:程序博客网 时间:2024/06/10 04:47

先用两个dfs暴力求出所有的lucky numbers,然后二分lucky number数组的下标,求出答案

View Code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef __int64 lld;
vector<lld> lu,res;
int tot;
void dfs(lld num,int de){
if(de==12) return ;
lu.push_back(num);
dfs(num*10+4,de+1);
dfs(num*10+7,de+1);
}
void dfs1(int x,lld num){
for(int i=x;i<lu.size();i++){
if(1e12/num<lu[i]) return;
res.push_back(num*lu[i]);
dfs1(i,num*lu[i]);
}
}
void init(){
lu.clear();res.clear();
dfs(4,0);
dfs(7,0);
sort(lu.begin(),lu.end());
dfs1(0,1);
sort(res.begin(),res.end());
tot=unique(res.begin(),res.end())-res.begin();
}
int main(){
init();
int t;
lld a,b;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d",&a,&b);
int l=lower_bound(res.begin(),res.begin()+tot,a)-res.begin();
int r=upper_bound(res.begin(),res.begin()+tot,b)-res.begin();
printf("%d\n",r-l);
}
}



原创粉丝点击