1095 - Six and One
Time Limit:1s Memory Limit:128MByte
Submissions:168Solved:45
DESCRIPTION
Given the integers L and R, your task is calculating the count of the integer n which meet the following conditions:
For instance, 1, 6, 16, 36, 61, 3721 are some integers which meet the conditions.
INPUT
The first line contains a positive integer
T, which represents there are
T test cases.The following is test cases. For each test case:The only line contains two positive integer
L and
R.
1≤T≤105,1≤L≤R≤1010
OUTPUT
For each test case, output in one line, contains one integer, which represents the count of the integers which meet the conditions.
SAMPLE INPUT
41 1016 3660 703720 3722
SAMPLE OUTPUT
2221
SOLUTION
---------------------------------------------------------------------------------------------------
所有由6和1组成的<=1e10数 有<3^10个(每位从0,1,6中选择)
果断dfs枚举
由6,1组成的数 除了1外 都>=6
m个数连乘 可以看做m<=log6(1e10)
m也不大 于是再枚举每个数的k次幂
放set里发现居然只有2000+个数
继续暴力 预处理出所有数+2分就过了....
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
const ll N = 1e10;
vector<ll>a;
void init61(ll x){//求出由6和1组成的数
ll x1=x*10+1;
ll x2=x*10+6;
if(x1<=N){
a.push_back(x1);
init61(x1);
}
if(x2<=N){
a.push_back(x2);
init61(x2);
}
}
set<ll>aset;
inline bool greaterThenN(ll a,ll b){//a*b>N? 乘法改除法防溢出
double c=(double)a/N;
return c*b>1;
}
void slove(){
sort(a.begin(),a.end());
for(int i=1;i<a.size();++i){//求出所有t=a[i]^k 满足t<=N
ll x=a[i];
ll t=1;
while(1){
if(greaterThenN(t,x)){
break;
}
t*=x;
aset.insert(t);
}
}
for(auto it1=aset.cbegin();it1!=aset.cend();++it1){//枚举连乘
for(auto it2=aset.cbegin();it2!=it1;++it2){
if(greaterThenN(*it1,*it2)){
break;
}
aset.insert(*it1*(*it2));
}
}
a.clear();
a.push_back(1);
for(auto it=aset.cbegin();it!=aset.cend();++it){
a.push_back(*it);
//cout<<a.back()<<endl;
}
}
int main()
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
//freopen("/home/lu/Documents/w.txt","w",stdout);
init61(0);
slove();
int n;
scanf("%d",&n);
while(n--){
ll l ,r;
scanf("%lld%lld",&l,&r);
auto it1=lower_bound(a.begin(),a.end(),l);
auto it2=upper_bound(a.begin(),a.end(),r);
printf("%d\n",it2-it1);
}
return 0;
}