#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

const int MAXN = 5000000;

int cnt,cnt_;
__int64 lucky_num[MAXN];

void dfs1(__int64 sum,int dep)
{
    if ( dep == 12 )
        return;
    
    lucky_num[cnt++] = sum * 10 + 4;
    dfs1(sum * 10 + 4,dep + 1);

    lucky_num[cnt++] = sum * 10 + 7;
    dfs1(sum * 10 + 7,dep + 1);
}

void dfs2(__int64 pro,int s)
{
    for (int i = s; i < cnt_; i++)
    {
        __int64 tmp = lucky_num[i] * pro;
        if ( tmp > 0 && tmp <= 1000000000000 )
        {
            lucky_num[cnt++] = tmp;
            dfs2(tmp,i);
        }
        else
            break;
    }
}

int bsearch(int l,int r,__int64 key)
{
    while ( l <= r )
    {
        int mid = ( l + r ) >> 1;
        if ( lucky_num[mid] == key )
            return mid;
        else if ( lucky_num[mid] > key )
            r = mid - 1;
        else
            l = mid + 1;
    }
    return l;   //finally,l > r
}

int main()
{
//    freopen("1.txt","r",stdin);

    dfs1(0,0);
    sort(lucky_num,lucky_num + cnt);
    cnt_ = cnt;
    dfs2(1,0);
    sort(lucky_num,lucky_num + cnt);
    cnt = unique(lucky_num,lucky_num + cnt) - lucky_num;

    int T;
    scanf("%d",&T);
    while ( T-- )
    {
        __int64 a,b;
        scanf("%I64d%I64d",&a,&b);
        int l = bsearch(0,cnt - 1,a);    
        int r = bsearch(0,cnt - 1,b);
        int ans = r - l;
        if ( b == lucky_num[r] )
            ans++;
        printf("%d\n",ans);
    }
}

 

Description

John has recently arrived in Bucharest for the South Eastern European Regional Contest. John is famous for his theory of lucky numbers. That’s why all the contestants and spectators are very happy.
According to that 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. Of course, numbers A and B are given by John.

Input

The first line of the input contains a single integer T – a number of test cases. Each of the next T lines contains two integers separated by a single space – A and B.

Output

Output must contain T lines – answers for the test cases.
posted on 2012-12-25 20:32  Sinker  阅读(451)  评论(0编辑  收藏  举报