Codeforces Beta Round #51 D. Beautiful numbers(数位dp)
题目链接:https://codeforces.com/contest/55/problem/D
1
1 9
9
1
12 15
2 解题思路:很明显的数位dp,首先我们可以想一个数要整除它的每一位除0意外,那我们可以转化成可以整除它的每一位的最小公倍数即可。而我们可以直接求出1-9的最小公倍数为2520,取模不改变它们间的倍数关系,因为数很大所以我们每次可以对2520取模就可以了。所以我们可以很容易想到定义一个数组dp[20][2525][2525],dp[pos][mul][sta]表示的是第搜索到第pos位数值为mul(模2520后)每一位的最小公倍数为sta的合法数的个数,但我们发现这样状态数很多是会超时的,所以我们必须想办法进行优化,我们认真想想发现比2520小的数中并不是每一个数都有可能是1-9中某些数字的倍数,我们把2520中可能是1-9某些数字的倍数的数筛出来就好了,发现只有48个,我们把dp数组改为dp【20】【2525】【50】,这样就不会超时了,相当于做了一个离散化处理。
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int a[],k[];
ll l,r,dp[][][];
ll dfs(int pos,int mul,int sta,int limit){
if(pos==) return mul%sta==;
if(!limit&&dp[pos][mul][k[sta]]!=-)
return dp[pos][mul][k[sta]];
int up=limit?a[pos]:;
ll ans=;
for(int i=;i<=up;i++){
if(i==){ //当该位为0时,不求最小公倍数
ans+=dfs(pos-,mul*%,sta,limit&&i==a[pos]);
}else{
int tmp=lcm(sta,i);
ans+=dfs(pos-,(mul*+i)%,tmp,limit&&i==a[pos]);
}
}
if(!limit&&dp[pos][mul][k[sta]]==-)
dp[pos][mul][k[sta]]=ans;
return ans;
}
ll solve(ll x){
int pos=;
while(x){
a[++pos]=x%;
x/=;
}
return dfs(pos,,,);
}
int main(){
memset(dp,-,sizeof(dp));
int t,cnt=;
cin>>t;
for(int i=;i<=;i++){
if(%i==)k[i]=++cnt; //2520%i不为0表示i一定不是1-9中某些数字的最小公倍数
}
while(t--){
cin>>l>>r;
cout<<solve(r)-solve(l-)<<endl;
}
return ;
}
Codeforces Beta Round #51 D. Beautiful numbers(数位dp)的更多相关文章
- 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 ...
- Codeforces Beta Round #51 D. Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)
Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...
- codeforces 55d//Beautiful numbers// Codeforces Beta Round #51
题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数. 注意离散化,为了速度更快需存入数组查找. 不要每次memset,记录下已有的长度下符合条件的个数. 数位dp肯定是从高位到低位. 记 ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- 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 ...
随机推荐
- $符号报not defing 报错
https://blog.csdn.net/weixin_37969488/article/details/84250833 最近因为工作问题,需要我把别的项目上的一些jsp网页copy到新项目上.放 ...
- 取消导航栏navigationBar的半透明/毛玻璃效果
iOS 7.0以上的系统,导航栏默认有毛玻璃效果,遮住了颜色 原因是7.0以上的系统,导航栏默认有毛玻璃效果,遮住了颜色,取消掉这个效果就行了. if( ([[[UIDevice currentDev ...
- 【PAT】B1014 福尔摩斯的约会
因为前面两字符串中第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母D,代表星期四: 第 2 对相同的字符是 E ,那是第 5 个英文字母,代表一天里的第 14 个钟头(于是一天的 0 点到 ...
- 周一01.4安装PyCharm步骤
安装集成开发工具 步骤一 步骤二 步骤三 步骤四
- Django REST framework框架介绍和基本使用
Django REST framework介绍 Django REST framework是基于Django实现的一个RESTful风格API框架,能够帮助我们快速开发RESTful风格的API. 官 ...
- Vector与ArrayList区别
1)Vector的方法都是同步的(Synchronized),是线程安全的: ArrayList的方法是线程不安全的. 由于线程同步必然会影响性能,因此,ArrayList的性能比Vector好. 请 ...
- CSS--字体|垂直居中|background
一,字体的设置 二,垂直居中 2.1,单行文本垂直居中 2.2,多行文本垂直居中 2.3,绝对定位元素垂直居中 三.颜色的表示法 四.background ---------------------- ...
- Java注解开发与应用案例
Java注解开发与应用案例 Annotation(注解)是JDK5.0及以后版本引入的,可以对包.类.属性.方法的描述,给被述对象打上标签,被打上标签后的类.属性.方法将被赋予特殊的“功能”:打个比喻 ...
- 报错:[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop bei
项目中遇到父组件传值 activeIndex <Tabs :tabs="tabs" :activeIndex="activeIndex" >< ...
- Arduino 串口测试 电脑发数据接收后立马返回
String comdata = ""; void setup() { Serial.begin(9600); while(Serial.read()>= 0){} //cl ...