Codeforces Beta Round #51 D. Beautiful numbers

链接

https://codeforces.com/contest/55/problem/D

题意

T ( 1 ≤ t ≤ 10 ) T(1\le t \le 10) T(1t10) 次询问,求 [ l , r ] ( 1 ≤ l ≤ r ≤ 9 e 18 ) [l,r](1 \le l \le r \le 9e18) [l,r](1lr9e18) 内多少数能被其自身每位非零数字整除。

思路

数位 DP。

若一个数能被其自身每位非零数字整除,那么其必能被其每位非零数字的最小公倍数整除。

离散化所有最小公倍数,总数不超过 50 50 50 个。

如果枚举最小公倍数,那么我们需要多加一维数组或者每次枚举前初始化数组,会导致 MLE 或 TLE。

我们找到 2520 2520 2520 是所有最小公倍数的最小公倍数。

易发现 当 x ∣ y x\mid y xy 时,如果 a % x = 0 a \% x=0 a%x=0,那么 a % y % x = 0 a \% y \% x =0 a%y%x=0

所以我们将模数定为 2520 2520 2520

d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k] 为从高到低的第 j j j 位,此前缀中出现的每位非零数的最小公倍数为 l c m i lcm_i lcmi 时,此前缀模 2520 2520 2520 余数为 k k k 的方案数。

代码

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const LL MOD=2520;
LL dp[48][20][2520];
VI lim,d;
void init() {
    for(int i=2;i<1<<10;i++) {
        int t=1;
        for(int j=1;j<10;j++) {
            if(i&(1<<j)) {
                t=t*j/__gcd(t,j);
            }
        }
        d.PB(t);
    }
    sort(ALL(d));
    d.resize(unique(ALL(d))-d.begin());
    memset(dp,-1,sizeof dp);
}
LL dfs(int x,int mod,int st,bool a,bool b) {
    if(!x) return !a&&!(mod%st);
    int t=lower_bound(ALL(d),st)-d.begin();
    if(!a&&!b&&dp[t][x][mod]!=-1) return dp[t][x][mod];
    int mx=b?lim[x]:9;
    LL ret=0;
    for(int i=0;i<=mx;i++) ret+=dfs(x-1,(mod*10+i)%MOD,a?i:(i>1?st*i/__gcd(st,i):st),a&(!i),b&(i==mx));
    if(!a&&!b) dp[t][x][mod]=ret;
    return ret;
}
LL solve(LL x) {
    lim.clear();
    lim.PB(-1);
    while(x) lim.PB(x%10),x/=10;
    LL ret=0;
    return dfs(SZ(lim)-1,0,0,true,true);
}
int main() {
    init();
    int tt;
    scanf("%d",&tt);
    while(tt--) {
        LL l,r;
        scanf("%lld%lld",&l,&r);
        printf("%lld\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、付费专栏及课程。

余额充值