当前位置: 首页 > news >正文

Codeforces Beta Round #51 D. Beautiful numbers 数位dp

题目链接:

http://codeforces.com/problemset/problem/55/D

D. Beautiful numbers


time limit per test4 secondsmemory limit per test256 megabytes

问题描述

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 (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri (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 numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).

样例输入

2
1 9
12 15

样例输出

9
2

题意

求[l,r]区间里面满足能被所有自己数位上的非0数整除的数有多少个。

题解

首先有一个想法是把这个数%2,3,4,,,9的余数都当成状态保存下来,但这一显然会爆炸。

那么能不能只选一个数呢?有!那就是2,3,,,9的最小公倍数2920,因为a%(k*b)%b==a%b,所以我们把数压到%2920是完全不会失真的,同时我们可以吧lcm相同的进行归类,lcm相同的放到一起处理,注意:2,,,9的任何组合的lcm总共只有48种!(即2920的约数个数)。所以我们只要吧数位分出48类处理即可,对每一类保留%2920后的值是多少。

dp[len][lc][mod]表示前len位,lcm的值为lc,且取模2920=mod的有多少个。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf

typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;

const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);

//start----------------------------------------------------------------------

LL gcd(LL a,LL b){
    return b==0?a:gcd(b,a%b);
}

LL Lcm(LL a,LL b){
    return a/gcd(a,b)*b;
}

VI lcm;
int mp[2555];
int arr[22],tot;
///ismax标记表示前驱是否是边界值
///iszer标记前驱是否是前导零
LL dp[22][48][2555];
LL dfs(int len,int lc, int mod,bool ismax) {
    if (len == 0) {
        ///递归边界
        return mod%lcm[lc]==0;
    }
    if (!ismax&&dp[len][lc][mod]>=0) return dp[len][lc][mod];
    LL res = 0;
    int ed = ismax ? arr[len] : 9;

    ///这里插入递推公式
    for (int i = 0; i <= ed; i++) {
        res+=dfs(len-1,mp[Lcm(lcm[lc],i==0?1:i)],(mod*10+i)%2520,ismax&&i == ed);
    }
    return ismax ? res : dp[len][lc][mod] = res;
}

LL solve(LL x) {
    tot = 0;
    while (x) { arr[++tot] = x % 10; x /= 10; }
    return dfs(tot, mp[1],0, true);
}

void pre(){
    for(int i=1;i<=2520;i++){
        if(2520%i==0){
            lcm.pb(i);
        }
    }
    for(int i=0;i<lcm.sz();i++){
        mp[lcm[i]]=i;
    }
}


int main() {
    pre();
    clr(dp,-1);
    int tc,kase=0;
    scanf("%d",&tc);
    while(tc--){
        LL L,R;
        scf("%lld%lld",&L,&R);
        prf("%lld\n",solve(R)-solve(L-1));
    }
    return 0;
}

//end-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5906781.html

相关文章:

  • java的RTTI
  • (转)memcache、redis缓存
  • java的编译时多态和运行时多态
  • java多态的实现机制
  • DOM Tree
  • Java 动态代理机制分析
  • powershell递归删除文件
  • Java 静态代理和动态代理
  • 【Python开发】Python之re模块 —— 正则表达式操作
  • 《深入浅出 Java Concurrency》—并发容器 ConcurrentMap
  • 不需内测账号,带你体验微信小程序完整开发过程
  • java synchronized与lock区别
  • 深入剖析Java编程中的中文问题及建议最优解决方法
  • linux系统安装php扩展
  • 从Decorator,Adapter模式看Java/IO库
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • Apache的80端口被占用以及访问时报错403
  • canvas 五子棋游戏
  • Computed property XXX was assigned to but it has no setter
  • gops —— Go 程序诊断分析工具
  • Java编程基础24——递归练习
  • js算法-归并排序(merge_sort)
  • JS题目及答案整理
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • 安装python包到指定虚拟环境
  • 番外篇1:在Windows环境下安装JDK
  • - 概述 - 《设计模式(极简c++版)》
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 如何用vue打造一个移动端音乐播放器
  • 实战|智能家居行业移动应用性能分析
  • 使用parted解决大于2T的磁盘分区
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • ​2021半年盘点,不想你错过的重磅新书
  • "无招胜有招"nbsp;史上最全的互…
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (4.10~4.16)
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (function(){})()的分步解析
  • (javascript)再说document.body.scrollTop的使用问题
  • (Oracle)SQL优化技巧(一):分页查询
  • (solr系列:一)使用tomcat部署solr服务
  • (vue)页面文件上传获取:action地址
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (六)Hibernate的二级缓存
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (一)Dubbo快速入门、介绍、使用
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)linux 命令大全
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • .net快速开发框架源码分享
  • /3GB和/USERVA开关
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @Mapper作用