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. 轻松搭建CAS 5.x系列(1)-使用cas overlay搭建SSO SERVER服务端

    概要说明 cas的服务端搭建有两种常用的方式:   1. 基于源码的基础上构建出来的   2. 使用WAR overlay的方式来安装 官方推荐使用第二种,配置管理方便,以后升级也容易.本文就是使用第 ...

  2. 使用NPOI导出Excel文件

    使用NPOI导出Excel文件,本实例使用了ASP.NET MVC. 1.使用NPOI导出Excel文件 实例:导出商品列表. 要求:1.通过NPOI导出导出商品列表信息: 2.使用Excel函数计算 ...

  3. 使用Docker搭建MySQL服务

    一.安装docker windows 和 mac 版可以直接到官网下载 docker desktop linux 的安装方法可以参考 https://www.cnblogs.com/myzony/p/ ...

  4. VUE实现简单的全选/全不选

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. java中的多态总结

    一.多态的概述 ava作为面向对象的语言,同样可以描述一个事物的多种形态.如Student类继承了Person类,一个Student的对象便既是Student,又是Person. Java中多态的代码 ...

  6. C++ STL 之 string

    #include <iostream> #include <string> using namespace std; // 初始化 void test01() { string ...

  7. ORACLE和SQL查询库数据量

    ORACLE根据账号查询每张表数据量: select t.table_name,t.num_rows from user_tables t ORDER BY NUM_ROWS DESC; SQL SE ...

  8. 【6】Zookeeper脚本及API

    一.客户端脚本 1.1.客户端连接 cd /usr/local/services/zookeeper/zookeeper-3.4.13/bin ##连接本地Zookeeper服务器 sh zkCli. ...

  9. 在线预览word、excel文件

    直接使用微软提供的在线预览服务. 免费 文件必须为网可访问地址,因为微软的服务器需要访问该文件

  10. Win10系统升级更新方式将会更智能

    使用Win10系统的你肯定遇到过在工作时开始自动更新而不得不搁置工作的情况,想必你也已经被Win10系统的自动更新折磨不已,不过这种情况将会马上得到改观. 微软现在已经开始寻找更智能的版本升级更新方式 ...