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 ...
随机推荐
- CPP笔记_函数返回局部变量
本篇笔记记录的是关于返回函数中的局部值. 我们知道,在函数中创建的局部变量会随着函数的调用过程的结束,也即其对应函数栈帧的清除,而结束其生命周期.那么,如果我们把这个局部变量返回,就有可能存在该变量对 ...
- Bootstrap-table 部分浏览器显示不出来
一.问题 近日,写了一个ASP.Net项目,但是bootstrap-table在别人的电脑上显示不出来,在自己的电脑上能显示,有些浏览器也是能显示,但部分浏览器就是显示不出来.找了很多原因,最后有个老 ...
- 在coding或者github建立个人站点域名绑定
coding:静态的在域名服务器与是填写pages.coding.me,不是填写{你的名字}.coding.me A型的ip是ping 该域名ip github:上传大项目可能报错(报错名字忘记了), ...
- 【原】Java学习笔记007 - 流程控制
package cn.temptation; public class Sample01 { public static void main(String[] args) { // for循环 // ...
- 高德地图 Service 创建服务 USERKEY_PLAT_NOMATCH
在使用高的地图 创建服务的时候 { "errmsg": "USERKEY_PLAT_NOMATCH", "errcode": 10009, ...
- Failed to initialize component [org.apache.catalina.webresources.JarResource
去本地仓库里删除掉对应出错的jar包,然后回到pom里面加个空行 让他重新下载(最好把maven仓库全部都删了 重新下载一次 ) 先备份 在复制回来 完美解决
- Bootstrap -- 导航栏样式、分页样式、标签样式、徽章样式
Bootstrap -- 导航栏样式.分页样式.标签样式.徽章样式 1. 使用图标的导航栏 使用导航栏样式: <!DOCTYPE html> <html> <head&g ...
- 【Teradata UDF】中文按字符查找chs_instr
一.场景描述 数据库为ASCII编码单字节存储,在查询中文时可能会出现错误结果.例如查询like“房”字,会查询出不含“房”,含“朔科”的结果. select * from Tablename01 w ...
- vue 路由元信息
官方文档:路由meta元信息 前言: 在设置面包屑导航还有菜单栏的时候,路由的meta字段可以自定义我们需要的信息,然后路由跳转的时候,提供我们判断条件 文档: 定义路由的时候可以配置 meta 字段 ...
- vue2.0阻止事件冒泡
<!--picker弹窗--><transition name="fade"> <div class="picker_wrap" ...