LightOJ 1276 - Very Lucky Numbers
#include<bits/stdc++.h>
using namespace std;
#define high 1000000000000
#define sf scanf
#define pf printf
typedef long long LL;
typedef vector<LL>vll;
typedef set<LL>sll;
vll l_numbers , ans , tmp;
sll s;
sll::iterator it;
LL ar[500000];
void veryLucky(int indx, LL m)
{
LL val;
int l_size = l_numbers.size();
if(m > high or m <= 0)
{
return;
}
if(m > 1 and m<=high)
{
s.insert(m);
l_numbers.push_back(m);
}
for(int i=indx; i<l_size; i++)
{
val = l_numbers[i] * m;
if(val > high or val <= 0)
{
break;
}
veryLucky(i, val);
}
}
void lucky()
{
s.insert(4);
s.insert(7);
LL sum = 0 , iv=40, vii=70;
int sz = 2 , i , j;
ar[1] = 4;
ar[2] = 7;
while(iv<=high and vii<=high)
{
for(i=1; i<=2 * sz; i++)
{
if(i > sz)
{
sum = vii + ar[i - sz];
s.insert(sum);
tmp.push_back(sum);
}
else
{
sum = iv + ar[i];
s.insert(sum);
tmp.push_back(sum);
}
}
for(i=0, j=1; i<tmp.size(); i++, j++)
{
ar[j] = tmp[i];
}
sz = tmp.size();
tmp.clear();
iv *= 10;
vii *= 10;
}
for(it=s.begin(); it!=s.end(); ++it)
{
l_numbers.push_back(*it);
}
//for(i=0; i<10; i++) cout << l_numbers[i] << "; ";
//cout << l_numbers.size();
//cout << "\n";
veryLucky(0, 1);
//cout << s.size();
l_numbers.clear();
for(it=s.begin(); it!=s.end(); ++it)
{
//ans.push_back(*it);
l_numbers.push_back(*it);
}
//cout << l_numbers[l_numbers.size()-1];
}
LL khoj(LL lo, LL hi, LL itm)
{
LL mid;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if(l_numbers[mid] == itm)
{
return mid;
}
else if(l_numbers[mid] > itm)
{
hi = mid - 1;
}
else if(l_numbers[mid] < itm)
{
lo = mid + 1;
}
}
return lo;
}
int main()
{
lucky();
int t , tc=0 , l , up;
LL a , b;
sf("%d", &t);
while(t--)
{
sf("%lld %lld", &a, &b);
l = khoj(0, l_numbers.size()-1, a);
up = khoj(0, l_numbers.size()-1, b);
if(l_numbers[up] != b)
{
up--;
}
pf("Case %d: %lld\n", ++tc, up - l + 1);
}
return 0;
}
using namespace std;
#define high 1000000000000
#define sf scanf
#define pf printf
typedef long long LL;
typedef vector<LL>vll;
typedef set<LL>sll;
vll l_numbers , ans , tmp;
sll s;
sll::iterator it;
LL ar[500000];
void veryLucky(int indx, LL m)
{
LL val;
int l_size = l_numbers.size();
if(m > high or m <= 0)
{
return;
}
if(m > 1 and m<=high)
{
s.insert(m);
l_numbers.push_back(m);
}
for(int i=indx; i<l_size; i++)
{
val = l_numbers[i] * m;
if(val > high or val <= 0)
{
break;
}
veryLucky(i, val);
}
}
void lucky()
{
s.insert(4);
s.insert(7);
LL sum = 0 , iv=40, vii=70;
int sz = 2 , i , j;
ar[1] = 4;
ar[2] = 7;
while(iv<=high and vii<=high)
{
for(i=1; i<=2 * sz; i++)
{
if(i > sz)
{
sum = vii + ar[i - sz];
s.insert(sum);
tmp.push_back(sum);
}
else
{
sum = iv + ar[i];
s.insert(sum);
tmp.push_back(sum);
}
}
for(i=0, j=1; i<tmp.size(); i++, j++)
{
ar[j] = tmp[i];
}
sz = tmp.size();
tmp.clear();
iv *= 10;
vii *= 10;
}
for(it=s.begin(); it!=s.end(); ++it)
{
l_numbers.push_back(*it);
}
//for(i=0; i<10; i++) cout << l_numbers[i] << "; ";
//cout << l_numbers.size();
//cout << "\n";
veryLucky(0, 1);
//cout << s.size();
l_numbers.clear();
for(it=s.begin(); it!=s.end(); ++it)
{
//ans.push_back(*it);
l_numbers.push_back(*it);
}
//cout << l_numbers[l_numbers.size()-1];
}
LL khoj(LL lo, LL hi, LL itm)
{
LL mid;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if(l_numbers[mid] == itm)
{
return mid;
}
else if(l_numbers[mid] > itm)
{
hi = mid - 1;
}
else if(l_numbers[mid] < itm)
{
lo = mid + 1;
}
}
return lo;
}
int main()
{
lucky();
int t , tc=0 , l , up;
LL a , b;
sf("%d", &t);
while(t--)
{
sf("%lld %lld", &a, &b);
l = khoj(0, l_numbers.size()-1, a);
up = khoj(0, l_numbers.size()-1, b);
if(l_numbers[up] != b)
{
up--;
}
pf("Case %d: %lld\n", ++tc, up - l + 1);
}
return 0;
}
Comments
Post a Comment