题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数。

注意离散化,为了速度更快需存入数组查找。

不要每次memset,记录下已有的长度下符合条件的个数。

数位dp肯定是从高位到低位。

记录数字已经有多大,还有lcm,递归传下去。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
using namespace std;
const double EPS=1e-;
const int SZ=,INF=0x7FFFFFFF;
typedef long long lon;
lon dp[][][],index[];//
vector<lon> ls; lon gcd(lon x,lon y)
{
if(x<y)swap(x,y);
for(;;)
{
lon rem=x%y;
if(rem==)return y;
x=y;
y=rem;
}
} lon lcm(lon x,lon y)
{
return x*y/gcd(x,y);
} void getid()
{
for(lon i=;i<ls.size();++i)
{
index[ls[i]]=i;
}
} void lsh()
{
for(lon i=;i<pow(,);++i)
{
lon cur=;
for(lon j=;j<;++j)
{
if(i&(<<j))cur=lcm(cur,j+);
}
ls.push_back(cur);
}
sort(ls.begin(),ls.end());
ls.erase(unique(ls.begin(),ls.end()),ls.end());
getid();
} lon dfs(lon pos,lon cur,lon prelcm,lon limit,string &str)
{
if(pos==str.size())return cur%prelcm==;
if(!limit&&dp[str.size()-pos-][cur][index[prelcm]]!=-)return dp[str.size()-pos-][cur][index[prelcm]];
lon res=;
lon up=limit?str[pos]-'':;
for(lon i=;i<=up;++i)
{
lon curlcm=prelcm;
if(i)curlcm=lcm(i,curlcm);
res+=dfs(pos+,(cur*+i)%,curlcm,limit&&str[pos]-''==i,str);
}
if(!limit)dp[str.size()-pos-][cur][index[prelcm]]=res;
return res;
} lon work(string &str)
{
return dfs(,,,,str);
} bool chk(string &str)
{
lon num=,curlcm=;
for(lon i=;i<str.size();++i)
{
num=(num*+str[i]-'')%;
if(str[i]!='')curlcm=lcm(curlcm,str[i]-'');
}
return num%curlcm==;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
cin>>casenum;
lsh();
memset(dp,-,sizeof(dp));
for(lon time=;time<=casenum;++time)
{
string ll,rr;
cin>>ll>>rr;
lon res=work(rr)-work(ll);
if(chk(ll))++res;
cout<<res<<endl;
}
return ;
}

codeforces 55d//Beautiful numbers// Codeforces Beta Round #51的更多相关文章

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

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

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

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

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

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

  4. CodeForces 55D Beautiful numbers

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

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

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

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

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

  7. CodeForces 55D Beautiful numbers(数位dp+数学)

    题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道 ...

  8. CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)

    题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...

  9. CodeForces 55D Beautiful numbers(数位dp)

    数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...

随机推荐

  1. Java线程基础知识(状态、共享与协作)

    1.基础概念 CPU核心数和线程数的关系 核心数:线程数=1:1 ;使用了超线程技术后---> 1:2 CPU时间片轮转机制 又称RR调度,会导致上下文切换 什么是进程和线程 进程:程序运行资源 ...

  2. iframe嵌套

    iframe基本内涵 通常我们使用iframe直接直接在页面嵌套iframe标签指定src就可以了. <iframe src="demo_iframe_sandbox.htm" ...

  3. ubuntu14.04无法安装Curl,需要先升级sudo apt-get update

    ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...

  4. CentOS安装JDK的三种办法

    方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ~]# c ...

  5. MySQL Crash Course #14# Chapter 22. Using Views

    索引 视图是啥 为什么需要视图 使用视图的规则 如何使用视图 视图应用实例 别用视图更新数据! 视图是啥 理解视图的最佳方式就是看下面这个例子. SELECT cust_name, cust_cont ...

  6. 立几个flag

    有时候会心血来潮想学一点东西,然后搞别的东西的时候就慢慢忘了.. 这里做个备忘录: 树分块/树上莫队 广义后缀自动机(大概这辈子都不会去学了) 带花树(如果我能学的动那个线代的随机算法就放弃这个) 模 ...

  7. 20145312袁心《网络对抗》Web基础实践

    20145312袁心<网络对抗>Web基础实践 问题回答 1.什么是表单: 表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程 ...

  8. 20145327 《网络对抗》MSF基础应用

    20145327 <网络对抗>MSF基础应用 主动攻击ms08_067 两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版) kali ip地址:192.168.4 ...

  9. Is it bad to rely on foreign key cascading? 外键 级联操作

    Is it bad to rely on foreign key cascading? I'll preface前言 this by saying that I rarely delete rows ...

  10. java画流程图【思路】

    java计算整数输入 <样例 画的第一张流程图 把脑子里的东西能清晰的表现出来 学编程必备