首页 > 其他分享 >CF55D Beautiful numbers

CF55D Beautiful numbers

时间:2022-09-02 22:01:07浏览次数:57  
标签:Beautiful 2520 int ll dep numbers CF55D lcm mod

求 \([l,r]\) 中满足 \(x\) 能被 \(x\) 数位中所有非 \(0\) 位整除的数的个数。 \(l,r\leq 9\times10^{18}\) 。


\(lcm(1\sim9)=2520\) 。

标准的数位DP。用 \(dp[i][j][k]\) 表示统计到第 \(i\) 位,前 \(i\) 位数字 \(mod\,2520\) 的余数为 \(j\) ,现在所有存在的数位组成的 \(lcm\) 为 \(k\) 。问题在于这样数组大小会达到 \(18\times2520\times2520\) ,显然会 \(MLE\) 。

发现其实 \(k\) 能取到的值并不多,可以考虑用离散化的方法把 \(2520\) 的所有因数都离散化,这样就可以将 \(k\) 这一维大大缩减到 \(50\) ,然后按照常规的数位DP来处理即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll dp[20][2521][50];
ll num[20];
ll l,r;
int mp[2521],fmp[50],mcnt;
int t;

int gcd(int x,int y){
    return (y==0)?x:gcd(y,x%y);
}



ll dfs(int dep,int mod,int lcm,bool flag){
    if(dep==-1)
        return (mod%fmp[lcm]==0)?1:0;
    if(dp[dep][mod][lcm]!=-1 && flag==0)
        return dp[dep][mod][lcm];
    int limit=flag?num[dep]:9;
    ll ret=dfs(dep-1,(mod*10)%2520,lcm,(flag && limit==0));
    for(int i=1;i<=limit;++i)
        ret+=dfs(dep-1,(mod*10+i)%2520,mp[fmp[lcm]*i/gcd(fmp[lcm],i)],(flag && limit==i));
    if(flag==0)
        dp[dep][mod][lcm]=ret;
    return ret;
}



ll solve(ll x){
    int cnt=-1;
    while(x){
        num[++cnt]=x%10;
        x/=10;
    }
    return dfs(cnt,0,1,1);
}


int main(){
    memset(dp,-1,sizeof dp);
    for(int i=1;i<=2520;++i){
        if(2520%i==0){
            mp[i]=++mcnt;
            fmp[mcnt]=i;
        }
    }
    scanf("%d",&t);
    while(t--){
        scanf("%lld %lld",&l,&r);
        printf("%lld\n",solve(r)-solve(l-1));
    }
    return 0;
}

标签:Beautiful,2520,int,ll,dep,numbers,CF55D,lcm,mod
From: https://www.cnblogs.com/zhouzizhe/p/16651493.html

相关文章

  • CF446C DZY Loves Fibonacci Numbers
    CF446CDZYLovesFibonacciNumbers题目大意在本题中,我们用\(f_i\)来表示第\(i\)个斐波那契数(\(f_1=f_2=1,f_i=f_{i-1}+f_{i-2}(i\ge3)\))。维护一个序列\(a\),长......
  • Python爬虫-BeautifulSoup基本用法(三)
    BeautifulSoup基本用法BeautifulSoup是Python的一个HTML或XML的解析库,可以用它来方便地从网页提取数据(以下为崔庆才的爬虫书的学习笔记)一.安装方式#安装beautifulsoup......
  • 2. Add Two Numbers
    2.AddTwoNumbers题目Youaregiventwonon-emptylinkedlistsrepresentingtwonon-negativeintegers.Thedigitsarestoredinreverseorderandeachofthe......
  • python爬虫之BeautifulSoup4使用
    钢铁知识库,一个学习python爬虫、数据分析的知识库。人生苦短,快用python。上一章我们讲解针对结构化的html、xml数据,使用Xpath实现网页内容爬取。本章我们再来聊另一个高效......
  • CF1715B Beautiful Array 题解
    思路根据题意,不难看出,当\(b>\dfrac{s}{k}\)时,一定无解,因为无论怎样分配\(s\),最终的结果一定不会比\(\dfrac{s}{k}\)更大。然后再来考虑当\(b\le\dfrac{s}{k}\)时,......
  • PAT Advanced 1023 Have Fun with Numbers(20)
    题目描述:Noticethatthenumber123456789isa9-digitnumberconsistingexactlythenumbersfrom1to9,withnoduplication.Doubleitwewillobtain2469135......
  • 【pip】pip3安装BeautifulSoup
    1、pipinstallBeautifulSoup报错Lookinginindexes:https://pypi.tuna.tsinghua.edu.cn/simpleCollectingBeautifulSoupUsingcachedhttps://pypi.tuna.tsinghua......
  • 《初等数学概览,第一卷,实数与函数》习题选做 An Excursion through Elementary Mathema
    最近在看AntonioCaminhaMunizNeto的AnExcursionthroughElementaryMathematics,VolumeIRealNumbersandFunctions这本书,在这里随便写点课后练习。英语水平......
  • hdu7215 Weighted Beautiful Tree
    problem一个点的点权的可能为不变或者变为连着的边的边权。然后dp、dp[u][0]表示变成大于等于w[u]边的最小代价。dp[u][1]表示变成小于等于w[u]边的最小代价。然后对......