POJ 3909 Lucky Numbers...

#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.

转载于:https://www.cnblogs.com/jiongjiong-mengmeng/archive/2012/12/25/2833016.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值