CF 55D Beautiful numbers (数位DP)
题意:
如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018)
思路:
数字很大,不能暴力。但是想要知道一个数是否为Beautiful number时,至少得等到它的所有位都出现吧?不然如何确定其实可以被整除的呢?
分析一下,类似2232和3232等这样的数字,这两个只是出现了2和3而已,他们的lcm都是6,所以有可以压缩统计的地方就是lcm,开一维来存储。接下来考虑前缀部分有没有什么可以压缩的地方,由于1~9的lcm最大是2520,那可以将前缀先模2520,最后再模那个数位的真正lcm就行了(2520必定是所有1~9中的任一组合的lcm的倍数,所以先取余是不会影响结果的),那么再开一维。所以状态为dp[位数][数位lcm][余2520的结果],就可以将所有数字给归类到这3维里面了。数位lcm可以优化,打表发现仅有47位可能的lcm而已,所以不必开2520的大小。
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x7f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
const int mod=;
int p[]={,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,}; int _lcm(int m,int n){return (m*n)/__gcd(m, n);}
LL f[N][][mod+], bit[N], has[mod+]; LL dfs(int i,int lcm,int left, bool e)
{
if(i==) return lcm&&left%p[lcm]== ;
if(!e && ~f[i][lcm][left]) return f[i][lcm][left]; LL ans=;
int u= e? bit[i]: ;
for(int d=; d<=u; d++)
{
int t= lcm? has[_lcm(p[lcm], max(d,))]: max(d,);
ans+=dfs(i-, t, (left*+d)%mod, e&&d==u);
}
return e==true? ans: f[i][lcm][left]=ans;
} LL cal(LL n)
{
int len=;
while(n) //拆数
{
bit[++len]=n%;
n/=;
}
return dfs(len,,,true);
} int main()
{
//freopen("input.txt","r",stdin);
memset(f,-,sizeof(f));
for(int i=; i<; i++) has[p[i]]=i; LL L, R, t;cin>>t;
while( t-- )
{
cin>>L>>R;
cout<<cal(R)-cal(L-)<<endl;
}
return ;
}
AC代码
CF 55D Beautiful numbers (数位DP)的更多相关文章
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- 【数位dp】CF 55D Beautiful numbers
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- 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 ...
随机推荐
- YOLO3训练widerface数据集
因为YOLO3速度精度都很棒,所以想训练一下人脸模型,废话不多,进入正题 1写所有的配置文件 1.1 YOLO3-face.cfg 个人感觉YOLO的配置文件骑士和caffe差不多 在cfg/YOLO ...
- java 获取路径与各文件目录的…
java 获取路径 博客分类: MyJava JavaJSPWebTomcat编程 转至:http://geeksun.iteye.com/blog/356339 (1).request.getRe ...
- Spring入门第十四课
基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性) 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath ...
- JS实现页面刷新方法
下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.loc ...
- Git查询
Git查询 查询分支 git branch # 查询本地分支 git branch -a # 查询所有分支 $ git branch -a * master remotes/origin/HEAD - ...
- DIV垂直水平居中
方法一:使用定位的方法 .parent { width: 300px; height: 200px; border: 1px solid red; position:relative; } .chil ...
- Linux之vim基本命令操作
安装vim(yum -y install vim) 三种工作模式(命令模式.输入模式.编辑模式) 打开文件( vi ) 查找文本( / ) 替换文本 删除文本( dd 删除光标所在行 ) 复制文本 去 ...
- FZU 2218【状压】
题意: 给出长为n的字符串,含有前k种小写字母,求两个不含重复元素的连续子串,使得他们的长度乘积最大. 思路: 字符种类16 ->(套路) 状压 暴力2000*2000得所有连续子串的长度,得每 ...
- java基础第十篇之异常
1.1接口概念 类:具有相同属性和功能的事物集合 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于 ...
- php对数组操作的函数
array_reverse 以相反的顺序返回数组 array_unique 数组元素去重(只对一维数组有效) array_intersect两个或多个数组取交集 implode和explode也 ...