Codeforces 55D - Beautiful numbers

CodeForces 55D-Beautiful numbers【数位DP+思维】 题目链接:https://codeforces.com/problemset/problem/55/D 题意: Volodya 是一个古怪的男孩,他的品味与众不同。对他来说,一个正整数是漂亮数,当且仅当它能够被自身的各非零数字整除。我们不必与之争辩,只需计算给定范围中有多少个漂亮数。 输入 输入的第一行包含了测试用例的数目t(1 ≤ t ≤ 10)。接下来的t行,每行包含两个自... 阅读详情

能注意到1~9的最小公倍数是2520,

所以我们只需要维护一下到dex位前的数字组成的lcm,和num

num需要控制在2520之内,因为2520是lcm的整数倍

所以num%lcm=(num%2520)%lcm

所以num=(num*10+i)%2520;

最后只需要判断num是否被lcm整除就可以了

但是DP[20][2555][2555] 是要超内存的

所以我们需要离散一下第二维,因为1~9组成的lcm的数量其实很有限(思考怎么算出所有的lcm)

所以需要新建Hash[50]数组压DP[20][50][2555];

还要此题有一个最坑点!!!!  DP初始化要为-1,如果初始为0就算重了

因为DP[][][]本身有可能为0

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>  
#include <iostream>  
#include <cstdlib>  
#include <cstring>  
#include <string>
#include <cstdio>  
#include <climits>
#include <cmath> 
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <sstream>
#include<time.h>
#define INF 0x3f3f3f3f
#define LL long long
#define fora(i,a,n) for(int i=a;i<=n;i++)
#define fors(i,n,a) for(int i=n;i>=a;i--)
#define sci(x) scanf("%d",&x);
#define scl(x) scanf("%lld",&x);
#define MAXN 100024
#define MOD 2520
const double eps = 1e-8;
using namespace std;
LL t, l, r, k;
LL digits[22], dp[22][50][2555];
LL Hash[2555], v = 0;
void hhash() {
	for (int i = 1; i <= 2520 / 2; i++) {
		if (2520 % i == 0) {
			Hash[i] = ++v;
		}
	}
}
LL gcd(LL a, LL b) {
	return b == 0 ? a : gcd(b, a%b);
}

LL dfs(LL dex, LL lcm, LL num, LL p) {
	LL sum = 0;
	if (dex == 0) return num%lcm == 0;
	if (dp[dex][Hash[lcm]][num]!=-1 && !p) return dp[dex][Hash[lcm]][num];
	LL u = p ? digits[dex] : 9;
	fora(i, 0, u) {
		LL Lcm = lcm;
		if (i) Lcm = lcm*i / gcd(lcm, i);
		sum += dfs(dex - 1, Lcm, (num * 10 + i) % MOD, p&&i == u);
	}
	if (!p) dp[dex][Hash[lcm]][num] = sum;
	return sum;
}
LL digit(LL num) {
	LL nnum = num;
	k = 0;
	while (num) {
		digits[++k] = num % 10;
		num /= 10;
	}
	return dfs(k, 1, 0, 1);
}
int main() {
#ifdef DID
	freopen("in.txt", "r", stdin);
	//freopen("out.txt","w",stdout);
#endif
	/*clock_t s = clock();*/
	scl(t);
	memset(dp, -1, sizeof(dp));
	hhash();
	while (t--) {
		scl(l); scl(r);
		LL num1 = digit(r);
		LL num2 = digit(l - 1);
		printf("%lld\n", num1 - num2);
	}
	/*clock_t e = clock();
	printf("执行完此代码需要%lfs\n", double(e - s) / 1000);*/
	return 0;
}


【数位DP】Beautiful numbers CodeForces - 55D 阅读详情

相关推荐

手把手教你用手机号注册的Apple ID成功登录Developer账户(避坑指南)

本文详细解析了使用手机号注册的Apple ID登录开发者账户时常见的'不符合使用条件'报错问题,并提供了从添加可验证邮箱到设置主账号的四步解决方案。特别针对Apple Developer账户的验证机制,帮助开发者避免因邮箱缺失导致的登录失败,提升账户激活成功率。

z5a6b的博客 1221

CodeForces - 55D Beautiful numbers(数位dp)

题目链接:点击这里 题目大意: 求 [l,r][l,r][l,r] 有多少种漂亮数字,其定义为能被他所有非零数字整除的数字 题目分析: 数位dp 如果一个数能整除的他的所有非零数位,相当于他能整除所有非零数位的最小公倍数,所以在记忆化是我们可以维护一下数位前缀的 lcmlcmlcm 。 我们发现 lcm(1,2,...,9)=2520lcm(1,2,...,9)=2520lcm(1,2,...,9)=2520 ,所以可以利用取余的性质:若 amod  b=0a\mod b=0amodb=0 那么 (am

羊驼的博客 193

大红球预警版-精选+自定义板块监控

这是一款简单易用的股票大单资金监控插件,兼容通达信软件,可嵌入分时图或定制版面。它以红绿圆球直观显示主力买卖情况(红球为买,绿球为卖),支持300万以上大单语音播报。插件提供实时资金流向分析、多股同时监控、自定义预警条件等功能,无需L2账号。用户可自由切换图形/列表模式,监控特定板块,并配合幻影软件实现自动交易。虽然上涨股通常红球占优,但需结合其他指标综合判断。该插件完全免费,包含详细视频教程,适合新手使用。

guleba的博客 1794

CodeForces 55D Beautiful numbers(数位dp&&离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers题意ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少。 求一个区间内的Beautiful numbers有多少个。Beautiful numbers指:一个数能整除所有组成它的非0数字。 例如15可以被1和5整除,所以15是Beautiful numbers。 思路 Be

YiShi's Blog 5094

归因分析计算因子贡献度常见的方法

在归因分析中,我们一般都需要计算出每个因子的贡献度是多少,比如产品DAU上升,对年龄段维度进行拆解,看是不同年龄段的用户对DAU上升的贡献度是多少,一般根据指标的类型,计算贡献度的方法也不一样,下面就列出一些常见的归因分析贡献度的计算方法。

Hubans的博客 4万+

SAP ERP 物料主数据同步外围系统

w_input-beskz = i_marc_new-beskz."采购类型 " BESKZ--- ADD BY HX_FHJ 20210106。w_input-beskz = lw_marc-beskz."采购类型 " BESKZ--- ADD BY HX_FHJ 20210106。w_input-sobsl = i_marc_new-sobsl."特殊采购类型-虚拟物料标识。

ChampaignWolf的专栏 1745

codeforces 55D D. Beautiful numbers(数位dp+数论)

题目链接:codeforces 55D题目大意:求在[l,r]中能够整除自己每个数位上的数字的数的个数。题目分析: 首先我们能够知道如果这个数能够整除它的每个数位上的数字,那么它一定能够整除他们的最小公倍数,是充要的。 那么我们定义状态dp[i][j][k]代表i位在任意组合下得到的所有数位的数字的最小公倍数为j的每个数位上的数字之积%2520为k的方案数。 我们可以知道所有的公倍数最大不会超过25

llin-黎辰 1874

继电器的选型规范

触点是继电器中最重要的结构件,触点的使用寿命受触点材料、触点上的电压及电流值(特别是接通时及断开时的电压、电流波形)、负载种类、切换频率、环境情况、接触形式、触点回跳现象等的影响,触点失效多以触点的材料转移、粘连、异常消耗、接触电阻增大等故障现象出现,使用时需要注意。为了稳定工作, DC 型继电器的线圈两端所加电压推荐使用波纹变化率小于±5%的线圈额定电压,否则继电器会工作不稳定,引起触点的粘接或异常损耗,特别是在继电器的驱动电路上连接有电动机、螺线管、变压器等器件时,这种情况更为明显。

qq_43416206的博客 1881

codeforces 55D Beautiful numbers(数位DP)

Beautiful numbers Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We w...

黑大帅的博客 275

滤波算法-呼吸心跳信号检测方法(四)

呼吸心跳信号检测系列文章:信号滤波处理

yjh_2019的博客 6269

Xilinx PCIe IP核示例工程仿真流程与关键任务解析

本文详细解析了Xilinx PCIe IP核示例工程的仿真流程与关键任务。通过搭建仿真环境、解读初始化日志,并深入剖析TSK_TX_MEMORY_WRITE_32等核心任务的TLP报文生成与交互过程,帮助开发者掌握PCIe IP核的仿真调试方法,为实际硬件设计提供可靠的验证基础。

weixin_28314575的博客 507

CodeForces 55D Beautiful numbers(数位dp)

CodeForces 55D题目求给定区间内的能被自己的每一非零位整除的数的个数思路http://www.cnblogs.com/algorithms/archive/2012/09/02/2668021.html dp[pos][mod][lcm],pos表示当前位,mod表示对2520取模,lcm表示位数的最小公倍数,离散化,取2520等处理优化的很好。代码#include<iostream

天道酬勤 898

OSG 核心详解

OpenSceneGraph (OSG) 的核心架构、场景图管理以及几何体的绘制

qq_45859163的博客 729

codeforces 55D Beautiful numbers

D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Volodya is an odd boy and his taste is

libing923的专栏 1623

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【codeforces】55D. Beautiful numbers 数位DP

传送门:【codeforces】55D. Beautiful numbers 题目分析:

poursoul 1063

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【数位dp】Beautiful numbers CodeForces - 55D

Beautiful numbers  CodeForces - 55D 碰到的第二类数位dp的问题:求被整除的个数 这一种不同的特征在于余数,所以将余数作为dp的变量。 这题比较麻烦,题意简单的来说是:找(非0)的所有位数都能整除这个数的数。 要使所有非0位数整除这个数,那么 这个数%这些非0位数的最小公倍数==0。 那么不同的特征不仅在余数,还有最小公倍数上。 因为最小公

学无纸巾 328

Codeforces-55D Beautiful numbers

数位DP 数学

a free man 660

Codeforces 55D Beautiful numbers 数位dp

http://codeforces.com/problemset/problem/55/D 题意: 要你求在A到B区间内的所有beautiful数,beautifil数的定义是这个数能被它自身数位上的所有数整除。 思路: 很好的一道数位dp的题目,大致的想法是这样的:首先我们可以知道一个数要能被它的所有数位上的非零数整除,那么这个数就必须要能被这个数中出现过的所有数字的最小公倍数整除,这样

Chris_Home 1323

CodeForces - 55D A - Beautiful numbers

CodeForces - 55D A - Beautiful numbers(数位dp) 题目链接:https://cn.vjudge.net/contest/163023#problem/A 题目大意:求L~R范围内能被本身每个非零位整除的数的个数。(1 ≤ l ≤ r ≤ 9 ·1e18) 题目分析: 第一个要知道的知识点: sum%(x*n)%x == sum%x; 证明:...

deerly_的博客 334

CodeForces 55D - Beautiful numbers

Beautiful numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Volodya is an odd boy and his taste is strange as well. It seems to him that a p

AcIsFun的博客 536

CodeForces - 55D Beautiful numbers

题目链接:http://codeforces.com/problemset/problem/55/D 题意:求区间[L,R]有多少个Beautiful numbers。Beautiful numbers为能被组成该数字的各个数字整除的数字。 思路:由于所有1-9数字的Lcm为2520。我们用f[i][j][k]表示到达第i位,前面的数字模2520的余数为j,前面数字的Lcm为k,那么最后j%k...

weixin_34161032的博客 122

Beautiful numbers CodeForces - 55D (数位dp)

Beautiful numbers CodeForces - 55D 题意:给出t个样例,每样例给出L和R问在L和R中间有多少数为漂亮数,漂亮数的定义为:数里面的每一位都可以被该数整除。 思路:数位dp,首先将数分解为位数,然后进行dfs搜索。对于dfs里面每一位参数以作解释了。对于dp,2520是1-9的最小公倍数。50是1-2520里面有多少是2520的因数。 #include&l...

DeathYmz的博客 490

CodeForces - 55D Beautiful numbers —— 数位DP

题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard ou...

alince20008的博客 179

CodeForces 55D Beautiful numbers

Beautiful numbers 题目链接 Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number isbeautifulif and only if it is divisible ...

dexicuo5991的博客 84

CodeForces55D Beautiful numbers

D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Volodya is an odd boy and his taste is

Jelly_acm的博客 505

A - Beautiful numbers CodeForces - 55D

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with...

cherish_lin的博客 250
上一篇: HDU 3652 B-number
下一篇: 数位DP总结
DIDCJS
博客等级 码龄11年 5粉丝 31原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值