Codeforces 55D. Beautiful numbers

题意

求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等)。

思路

一开始以为是数位DP的水题,觉得只需要记录搜到当前位出现了哪些数字作为状态即可,明显是假算法...感觉这是一道数位DP好题。可以这样思考:一个数要想被其各位数字分别都整除,等价于它被那些数字的LCM整除。因此记录当前位,当前数对(1~9的LCM)取模的结果,当前出现的数字的LCM这三个值作为状态才合理,即dp[pos][sum][lcm]。不过有一点需要注意,1-9的LCM的值会达到2520,直接存的空间复杂度是不对的,而1-9的数字任意组合的LCM明显没有那么多个,因此考虑离散化这些LCM,即可压缩空间。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<cmath>
#include<functional>
#include<string>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=5e3+10,mod=1e9+7,INF=0x3f3f3f3f;
int LCM,dig[30],id[maxn];
ll dp[30][maxn][100];
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll Lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}
void init()
{
int num=0;
LCM=1;
for (int i=1;i<=9;++i)
LCM=Lcm(LCM,i);
for (int i=1;i<=LCM;++i)
if (LCM%i==0)
id[i]=num++;
memset(dp,-1,sizeof(dp));
}
ll dfs(int pos, int sum, int lcm, int lit)
{
if (pos==-1)
return sum%lcm==0;
if (!lit&&dp[pos][sum][id[lcm]]!=-1)
return dp[pos][sum][id[lcm]];
int up=lit?dig[pos]:9;
ll cnt=0;
for (int i=0;i<=up;++i)
{
int ns=(sum*10+i)%LCM,nl=i==0?lcm:Lcm(lcm,i);
cnt+=dfs(pos-1,ns,nl,lit&&i==dig[pos]);
}
if (!lit)
dp[pos][sum][id[lcm]]=cnt;
return cnt;
}
ll count(ll n)
{
int pos=0;
while (n)
{
dig[pos++]=n%10;
n/=10;
}
return dfs(pos-1,0,1,1);
}
int main()
{
init();
int T;
cin>>T;
while (T--)
{
ll l,r;
cin>>l>>r;
cout<<count(r)-count(l-1)<<endl;
}
return 0;
}

Codeforces 55D. Beautiful numbers(数位DP,离散化)的更多相关文章

  1. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  3. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  4. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  5. codeforces 55D. Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

  6. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

  7. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

  8. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. spring-boot-plusV1.2.3发布,CentOS快速安装环境/构建/部署/启动项目

    spring-boot-plusV1.2.3发布,CentOS快速安装环境/构建/部署/启动项目 [V1.2.3-RELEASE] 2019.09.09

  2. c++11 原生字符串字面值

    c++11 原生字符串字面值 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #in ...

  3. Linux学习(五)-Linux组管理和权限管理

    Linux组基本介绍 在Linux中的每一个用户必须属于一个组,不能独立与组外.在Linux中每个文件有所有者,所在组,其他组的概念 所有者 所在组 其它组 改变用户所在组 1.文件/目录 所有者 文 ...

  4. F - Star SPOJ - STARSBC

    Fernando won a compass for his birthday, and now his favorite hobby is drawing stars: first, he marks ...

  5. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  6. Educational Codeforces Round 40 G. Castle Defense (二分+滑动数组+greedy)

    G. Castle Defense time limit per test 1.5 seconds memory limit per test 256 megabytes input standard ...

  7. IP段的正则表达式

    IPv4 prefix格式:比如: 192.168.1.0/24 ^(?=(\b|\D))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{ ...

  8. cas多方式登录相关知识点的总结

    知识点: cas多表单登录(在用户名,密码的基础上,增加短信验证码登录) 自定义认证策略 自定义字段添加为空校验的错误信息 Controller层接口的调用 一:场景 项目涉及到的业务是,在原cas用 ...

  9. git远程分支创建,本地分支关联远程分支,第一次发布、更新分支

    git远程分支创建,本地分支关联远程分支,第一次发布.更新分支 github托管服务器地址为https://github.com git提交更新代码示意图: 本地与远程进行免密码配置(本地与远程关联) ...

  10. 少有人知的 GitHub 使用技巧

    GitHub 大家常上吧?可是使用 GitHub 的各种小窍门你就不一定知道了.本文将各种使用 GitHub 的小窍门分享给大家. diff时忽略空格 有些修改只是增减了空格,在URL中添加?w=1就 ...