CodeForces 55D Beautiful numbers(数位dp)

E - Beautiful numbers
Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.

Input

The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri (1 ≤ li ≤ ri ≤ 9 ·1018).

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).

Output

Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).

Sample Input

Input
1
1 9
Output
9
Input
1
12 15
Output
2

 

/*
CodeForces 55D Beautiful numbers(数位dp)

problem:
问[l,r]之间有多少个能被它所包含的非零数整除

solve:
如果能这些数整除,则能被他们的最小公倍数整除. 1~9的最小公倍数为2520,所以在过程中维护lcm和这个数对2520的取余
而且可以处理出1~9的所有lcm,离散化处理.

hhh-2016-08-25 16:56:23
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <map>
#define lson  i<<1
#define rson  i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfl(a) scanf("%I64d",&a)
#define key_val ch[ch[root][1]][0]
#define inf 0x3f3f3f3f
using namespace std;
const ll mod = 2520;
const int maxn = 100010;
int tot,t[maxn];
ll dp[20][3000][50];
int bin[3000];
int cnt = 0;
int lcm(int a,int b)
{
    if(!a && !b)
        return 0;
    if(!a)  return b;
    if(!b)  return a;
    int ta=a,tb=b;
    while(a % b != 0)
    {
        int p = a % b;
        a = b;
        b = p;
    }
    return ta/b*tb;
}

int fin(int pos)
{
    int l = 0,r = cnt-1;
    while(l < r)
    {
        int mid = (l+r) >> 1;
        if(bin[mid] > pos)
        {
            r = mid -1;
        }
        else if(bin[mid] == pos)
        {
            return mid;
        }
        else
            l = mid + 1;
    }
}

ll dfs(int len,int nex,int div,int flag)
{
    if(len < 0 && div == 0)
        return 0;
    if(len < 0)
        return nex % div == 0;
    int tp = bin[div];
    if(dp[len][nex][tp] != -1 && !flag)
        return dp[len][nex][tp] ;
    ll ans = 0;
    int n = flag ? t[len] : 9;
    for(int i = 0; i <= n; i++)
    {
        int lc;
        lc = lcm(div,i);
//        cout << lc <<" " << i <<endl;
        int ta = (nex*10+i) % mod;
        ans += dfs(len-1,ta,lc,flag && i == n);
    }
//    cout << ans<<" "<<div<<endl;
    if(!flag)
        dp[len][nex][tp] = ans;
    return ans;
}

ll cal(ll a)
{
    tot = 0;
    while(a)
    {
        int p = a%10LL;
        t[tot++] = p;
        a /= 10LL;
    }
    ll ans = 0;
    ans += dfs(tot-1,0,0,1);
    return ans;
}
ll a,b;
int main()
{
    int T;
    cnt = 0;
    bin[cnt++] = 0;
    for(int i = 1;i <= mod;i++)
    {
        if(mod % i == 0)
           bin[i] = cnt++;
    }
//    freopen("in.txt","r",stdin);
    memset(dp,-1,sizeof(dp));
    scanfi(T);

    while(T--)
    {
        scanfl(a),scanfl(b);
        printf("%I64d\n",cal(b) - cal(a-1));
    }
}

  

转载于:https://www.cnblogs.com/Przz/p/5812363.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值