题目链接

题解

一个数能被一些数整除,那么一定被这些数的\(lcm\)整除

那么我们容易想到根据\(lcm\)设状态

我们可以发现有用的\(lcm\)只有\(48\)个

那么按照一般的数位\(dp\)

设出状态:\(f_{i,j,k,0/1}\)表示前\(i\)位,\(lcm=j\),模\(lcm\)的余数是\(k\),是否达到上界

但是这样子是无法转移的(因为新添加一个数模数可能会产生变化)

那么我们把模数统一成\(2520\)

复杂度\(O(T*L*48*2500*2)\)

其中\(L\)是输入数的位数

然后就会\(TLE\)

考虑稍微优化一下。

因为是多组询问。

如果之前已经询问过一个很大的数了,

这一次询问其实有很多答案我们已经知道了。

我们可以去掉状态的\(01\)那一维。

这样复杂度就可以去掉那个询问组数了。

具体看第二个代码。

Code

int gcd(int x, int y) {
return !y ? x : gcd(y, x % y);
}
int lcm(int x, int y) {
if (!x || !y) return x | y;
return x * y / gcd(x, y);
}
LL dfs(int x, int n, int m, int op) {
if (!x) return !num[n] || m % num[n] == 0;
if (f[x][n][m][op] != -1) return f[x][n][m][op];
LL &res = f[x][n][m][op]; res = 0;
for (int i = 0; i <= (op ? a[x] : 9); i++)
res += dfs(x - 1, M[lcm(num[n], i)], (m * 10 + i) % 2520, op & i == a[x]);
return res;
}
LL calc(LL x) {
if (!x) return 1;
l = 0;
while (x) a[++l] = x % 10, x /= 10;
for (int i = 1; i <= l; i++)
memset(f[i], -1, sizeof(f[i]));
return dfs(l, 0, 0, 1);
}
void solve() {
LL l = gi<LL>(), r = gi<LL>();
printf("%lld\n", calc(r) - calc(l - 1));
return ;
}
int main() {
for (int i = 1; i < (1 << 9); i++) {
int d = 0;
for (int j = 0; j < 9; j++)
if (i >> j & 1)
d = lcm(d, j + 1);
if (!M[d]) num[++cnt] = d, M[d] = cnt;
}
int T = gi<int>();
while (T--) solve();
return 0;
}
LL dfs(int x, int n, int m, int op) {
if (!x) return !num[n] || m % num[n] == 0;
if (!op && f[x][n][m] != -1) return f[x][n][m];
LL res = 0;
for (int i = 0; i <= (op ? a[x] : 9); i++)
res += dfs(x - 1, M[lcm(num[n], i)], (m * 10 + i) % 2520, op & i == a[x]);
if (!op) f[x][n][m] = res;
return res;
}

CF55D Beautiful numbers (数位dp)的更多相关文章

  1. cf55D. Beautiful numbers(数位dp)

    题意 题目链接 Sol 看到这种题就不难想到是数位dp了. 一个很显然的性质是一个数若能整除所有位数上的数,则一定能整除他们的lcm. 根据这个条件我们不难看出我们只需要记录每个数对所有数的lcm(也 ...

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

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

  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 Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

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

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

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

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

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

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

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

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

  9. codeforces 55D. Beautiful numbers 数位dp

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

随机推荐

  1. vue中设置全局的css样式

    只需在main.js    ====import './style.less'   main.js =>>   import Vue from 'vue' import App from ...

  2. c++学习---迭代器

    迭代器类型: begin和end的返回值的类型由对象是否为常量所决定 无论对象是都为常量,cbegin和cend都将都到一个const_iterator

  3. Unity UGUI动态生成控件

    一. 首先你得先清楚RectTransform组件的一些程序控制 1. 先得到UGUI控件上面的RectTransform组件 RectTransform rtr = gameObject.GetCo ...

  4. Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'

    An unhandled exception occurred while processing the request. InvalidOperationException: Unable to r ...

  5. Xamarin开发综述

    https://blog.csdn.net/qq_41647999/article/details/84844357 一. 前言这十来天对Xamarin的学习踩了很多的坑,说来也是一把心酸泪,下面为大 ...

  6. React实现顶部固定滑动式导航栏(导航条下拉一定像素时显示原导航栏样式)

    摘要 基于react的框架开发一个顶部固定滑动式的酷炫导航栏,当导航栏置顶时,导航栏沉浸在背景图片里:当鼠标滑动滚轮时,导航栏固定滑动并展示下拉样式. JS部分 相关技术栈:react.antd.re ...

  7. java轻松玩转httpget和httppost

    废话不多少说,直接上代码 //get请求 public static void HttpClientGet(String url) throws Exception { // 获取http客户端 Cl ...

  8. Array + two points leetcode.16 - 3Sum Closest

    题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...

  9. 一步步用ABAP Development Tools连接SAP云平台上的ABAP编程环境

    使用ABAP Development Tools的项目创建向导: New->ABAP Cloud Project: Service Instance Connection,选择SAP Cloud ...

  10. win10关闭防火墙和其通知

    Win10电脑在关闭防火墙后,防火墙的通知会不定期提醒,如果误点后,防火墙就悄悄的开启了,导致好多功能就用不了了,所以比较有效的方法是:关闭防火墙,并关闭防火墙通知 1.关闭防火墙 在控制面板中,选择 ...