【DP计划】10.16——[CF]CF55D Beautiful numbers(数位DP) HARD

题目链接:CF55D

题意翻译

题目描述

Volodya是一个很皮的男♂孩。他认为一个能被它自己的每一位数上的数整除的数是很妙的。我们先忽略他的想法的正确性(如需证明请百度“神奇海螺”),只回答在l到r之间有多少个很妙的数字。

输入输出格式

输入:总共有t个询问:

第一行:t;

接下来t行:每行两个数l和r。

注意:请勿使用%lld读写长整型(虽然我也不知道为什么),请优先使用cin(或者是%I64d)。

输出:t行,每行为一个询问的答案。
题目描述

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.
输入输出格式
输入格式:

The first line of the input contains the number of cases t t t ( 1<=t<=10 1<=t<=10 1<=t<=10 ). Each of the next t t t lines contains two natural numbers li l_{i} li​ and ri r_{i} ri​ ( 1<=li<=ri<=9⋅1018 1<=l_{i}<=r_{i}<=9·10^{18} 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 should contain t t t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li l_{i} li​ to ri r_{i} ri​ , inclusively).

输入输出样例
输入样例#1:

1
1 9

输出样例#1:

9

输入样例#2: 复制

1
12 15

输出样例#2: 复制

2


首先这种数据范围肯定是数位DP。然后我们思考怎么进行转移。一般的思路并不是特别容易往下推。
我们分析题目的性质,既然该数被各个位上的数整除,那么它肯定被各个位上的lcm整除,而1~9的lcm只有2520。所以我们可以去枚举这个lcm进行转移。我们设f[i][j][k]表示当前转移到第i位,当前的数为j(%2520意义下),之前所有位数的最大公因数为k,然后进行转移。那我们可以得到转移:
f[i][j][k]=f[i-1][(j*10+x)%2520][lcm(k,x)]  //x∈[1,maxx]
我们发现k的取值只有不到50种,所以我们对k进行离散,然后就可以过了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll read(){
    char c;ll x;while(c=getchar(),c<'0'||c>'9');x=c-'0';
    while(c=getchar(),c>='0'&&c<='9') x=x*10+c-'0';return x;
}
ll T,l,r,top,mp[2521],f[20][2521][50],a[20];
int gcd(int x,int y){
    return y?gcd(y,x%y):x;
}
int lcm(int x,int y){
    if(!y) return x;
    return x*y/gcd(x,y);
}
ll dfs(int p,int r,int g,int lim){
    if(!p) return r%g==0?1:0;
    if(lim&&f[p][r][mp[g]]) return f[p][r][mp[g]];
    ll x=!lim?a[p]:9;ll res=0;
    for(int i=0;i<=x;i++)
      res+=dfs(p-1,(r*10+i)%2520,lcm(g,i),lim|(i<x));  //x=0表示跳过这位
    if(lim) f[p][r][mp[g]]=res;
    return res;
}
ll solve(ll x){
    ll res=0;top=0;
    for(ll t=x;t;t/=10) a[++top]=t%10;
    return dfs(top,0,1,0);
}
int main()
{
    T=read();
    for(int i=1,k=0;i<=2520;i++)if(2520%i==0)mp[i]=++k;
    while(T--){
        l=read();r=read();
        printf("%I64d\n",solve(r)-solve(l-1));
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值