把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多。

下面为转载内容:

   a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.
    问一个区间内[l,r]有多少个Beautiful数字
    范围9*10^18
    
    数位统计问题,构造状态也挺难的,我想不出,我的思维局限在用递推去初始化状态,而这里的状态定义也比较难
    跟pre的具体数字有关

问了NotOnlySuccess的,豁然开朗  Orz
    
    一个数字要被它的所有非零位整除,即被他们的LCM整除,可以存已有数字的Mask,但更好的方法是存它们的LCM{digit[i]}
    int MOD = LCM{1,2,9} = 5 * 7 * 8 * 9 = 2520
    按照定义,数字x为Beautiful : 
    x % LCM{digit[xi]} = 0
    即 x % MOD % LCM{digit[xi]} = 0
    所以可以只需存x % MOD,范围缩小了
    而在逐位统计时,假设到了pre***(pre指前面的一段已知的数字,而*是任意变)
        ( preSum * 10^pos + next )  % MOD % LCM(preLcm , nextLcm)
    =  ( preSum * 10 ^ pos % MOD + next % MOD ) % LCM(preLcm , nextLcm)
    == 0
    而next,nextLcm是变量,上面的比较式的意义就是
    在已知pos , preSum , preLcm情况下有多少种(next,nextLcm)满足式子为0
    而这个就是一个重复子问题所在的地方了,需要记录下来,用记忆化搜索
    dfs(pos , preSum , preLcm , doing)
    加一个标记为doing表示目前是在计算给定数字的上限,还是没有上限,即***类型的
    这样就将初始化以及逐位统计写在一个dfs了,好神奇!!!
    
    还有一点,10以内的数字情况为2^3 , 3^2 , 5 , 7
    所以最小公倍数组合的情况只有4*3*2*2 = 48
    可以存起来,我看NotOnlySuccess的写法是
    for(int i = 1 ; i <= MOD ; i ++)
    {
        if(MOD % i == 0)
            index[i] = num++;
    }
    很棒!!

所以复杂度大概为19*2520*48*10(状态数*决策数)

我觉得这题状态的设计不能跟具体数字分开,否则会很难设计吧
    所以用记忆化搜索,存起来
    用具体数字去计算,重复的子问题跟pre关系比较密切
    有一个比较重要的切入点就是LCM,还有%MOD缩小范围,才能存储

还有优化到只需%252的,更快
    不过我觉得%2520比较好理解

代码:

 const int MOD = ;

 LL dp[][MOD][];
int digit[];
int indx[MOD+]; void init() {
int num = ;
for(int i = ; i <= MOD; ++i) {
if(MOD%i == ) indx[i] = num++;
}
CL(dp, -);
} LL gcd(LL a, LL b) {
return b == ? a : gcd(b, a%b);
} LL lcm(LL a, LL b) {
return a/gcd(a, b)*b;
} LL dfs(int pos, int presum, int prelcm, bool edge) {
if(pos == -) return presum%prelcm == ;
if(!edge && dp[pos][presum][indx[prelcm]] != -)
return dp[pos][presum][indx[prelcm]];
int ed = edge ? digit[pos] : ;
LL ans = ;
for(int i = ; i <= ed; ++i) {
int nowlcm = prelcm;
int nowsum = (presum* + i)%MOD;
if(i) nowlcm = lcm(prelcm, i);
ans += dfs(pos - , nowsum, nowlcm, edge && i == ed);
}
if(!edge) dp[pos][presum][indx[prelcm]] = ans;
return ans;
} LL cal(LL x) {
CL(digit, );
int pos = ;
while(x) {
digit[pos++] = x%;
x /= ;
}
return dfs(pos - , , , );
} int main() {
//Read(); init();
int T;
LL a, b;
cin >> T;
while(T--) {
cin >> a >> b;
cout << cal(b) - cal(a - ) << endl;
}
return ;
}

Codeforces 55D Beautiful Number (数位统计)的更多相关文章

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

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

  2. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  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(数位DP,离散化)

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

  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+数论)

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

  7. codeforces 55D. Beautiful numbers 数位dp

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

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

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

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

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

随机推荐

  1. 比较核心的技术了 虚拟ip的一种实现方式(手工添加和C#添加)

    虚拟IP技术在高可用领域像数据库SQLSERVER.web服务器等场景下使用很多,很疑惑它是怎么实现的,偶然,发现了一种方式可以实现虚拟ip.它的原理在于同一个物理网卡,是可以拥有多个ip地址的,至于 ...

  2. Javascript中new Date的坑

    在一段判断是否过期的js代码中是这么写的: if (new Date() < new Date(2014, 9, 25)) { //... } 后来发现过了9月25日竟然不过期,console. ...

  3. 第十七章:android解析JSON

    一.解析JSON数据: 首先引入包import org.json.JSONObject;(android sdk 14以后应该自带了 ) Android端的程序解析JSON和JSON数组: packa ...

  4. C#与数据库访问技术总结(十二)数据阅读器(DataReader)2

    遍历数据阅读器中的记录 当ExecuteReader方法返回DataReader对象时,当前光标的位置在第一条记录的前面. 必须调用阅读器的Read方法把光标移动到第一条记录,然后,第一条记录将变成当 ...

  5. windows下安装mingw

    windows环境下使用gcc MinGw是Minimal GNU on Windows的缩写,允许在GNU/linux和windows平台生成本地的windows程序而不需要第三方运行时库.本文主要 ...

  6. C语言实现冒泡排序-整数排序

    我一直觉得排序算法挺重要的,但是却没有深入的去理解它: 没有深入理解就无法用代码将它实现: 在腾讯的在线模拟考试中就有一题问到冒泡排序: 我几乎是傻眼了!我知道这样的问题是最基础的: 无论过去怎样现在 ...

  7. fir.im Weekly - 如果让你重新做一款APP

    设想下:如果让你重新做一款 APP ,你会用到哪些开发.设计等资源和工具? 本期的 Weekly 为大家分享了最近不错的 APP 开发资源,大部分是关于 iOS 开发. Android 开发.UI设计 ...

  8. 利用jsoup爬虫工具,爬取数据,并利用excel导出

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...

  9. 从此爱上iOS Autolayout

    转:从此爱上iOS Autolayout 这篇不是autolayout教程,只是autolayout动员文章和经验之谈,在本文第五节友情链接和推荐中,我将附上足够大家熟练使用autolayout的教程 ...

  10. Java int to String互转

    Integer.toString Integer.parseInt(lAyaNums);