[洛谷P2657][SCOI2009]windy数
题目大意:不含前导零且相邻两个数字之差至少为$2$的正整数被称为$windy$数。问$[A, B]$内有多少个$windy$数?
题解:$f_{i, j}$表示数有$i$位,最高位为$j$(可能为$0$),$f_{i, j} = \sum\limits_{0 \leq k \leq 9, |k - j| > 2} f_{i - 1, k}$
然后求出$[0, A)$和$[0, B]$分别有多少个$windy$数,相减即可
卡点:无
C++ Code:
#include <cstdio>
using namespace std;
int f[11][11], a, b;
int num[11], cnt, ans;
int abs(int x) {return x > 0 ? x : -x;};
int work(int x) {
cnt = ans = 0;
while (x) num[++cnt] = x % 10, x /= 10;
for (int i= cnt - 1; i; i--)
for (int j = 1; j < 10; j++)
ans += f[i][j];
num[cnt+1] = -2;
for (int i = cnt; i; i--) {
for (int j = 0; j < num[i]; j++)
if (abs(num[i+1] - j) >= 2) ans += f[i][j];
if (abs(num[i] - num[i+1]) < 2) break;
}
return ans;
}
int main() {
scanf("%d%d", &a, &b);
for (int i = 1; i <= 10; i++)
for (int j = 0; j < 10; j++)
if (i == 1) f[i][j] = 1;
else for (int k = 0; k < 10; k++) if (abs(j - k) >= 2) f[i][j] += f[i - 1][k];
int res = work(b + 1) - work(a);
printf("%d\n", res);
return 0;
}
[洛谷P2657][SCOI2009]windy数的更多相关文章
- 洛谷 P2657 [SCOI2009]windy数 解题报告
P2657 [SCOI2009]windy数 题目描述 \(\tt{windy}\)定义了一种\(\tt{windy}\)数.不含前导零且相邻两个数字之差至少为\(2\)的正整数被称为\(\tt{wi ...
- 洛谷——P2657 [SCOI2009]windy数
P2657 [SCOI2009]windy数 题目大意: windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和 ...
- C++ 洛谷 P2657 [SCOI2009]windy数 题解
P2657 [SCOI2009]windy数 同步数位DP 这题还是很简单的啦(差点没做出来 个位打表大佬请离开(包括记搜),我这里讲的是DP!!! 首先Cal(b+1)-Cal(a),大家都懂吧(算 ...
- 洛谷P2657 [SCOI2009]windy数 [数位DP,记忆化搜索]
题目传送门 windy数 题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个win ...
- 洛谷P2657 [SCOI2009]windy数 题解 数位DP
题目链接:https://www.luogu.com.cn/problem/P2657 题目大意:找区间 \([A,B]\) 范围内 不含前导零 且 相邻两个数字之差至少为2 的正整数的个数. 题目分 ...
- 洛谷 P2657 [SCOI2009]windy数
题意简述 求l~r之间不含前导零且相邻两个数字之差至少为2的正整数的个数 题解思路 数位DP 代码 #include <cstdio> #include <cstring> # ...
- BZOJ1026或洛谷2657 [SCOI2009]windy数
BZOJ原题链接 洛谷原题链接 简单的数位\(DP\),套模板就好. #include<cstdio> #include<cstring> using namespace st ...
- luogu P2657 [SCOI2009]windy数 数位dp 记忆化搜索
题目链接 luogu P2657 [SCOI2009]windy数 题解 我有了一种所有数位dp都能用记忆话搜索水的错觉 代码 #include<cstdio> #include<a ...
- P2657 [SCOI2009]windy数
P2657 [SCOI2009]windy数 题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B ...
随机推荐
- php GD图片四角圆形处理
<?php /** * blog:http://www.zhaokeli.com * 处理四角圆图片 * @param string $imgpath 源图片路径 * @param intege ...
- 在Pycharm中导入第三方模块库(诸如:matplotlib、numpy等)
在Pycharm中导入第三方模块库 一.打开pycharm: 二.点击菜单上的“file” -> “setting”: 三.步骤二完成后出现界面如下所示.选中你的项目(比如thisyan Pro ...
- C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂
C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...
- Python3 列表,元组,字典,字符串知识小结
一.知识概要 1. 列表,元组,字典,字符串的创建方式 2. 列表,元组,字典,字符串的方法调用 3. 列表,元组,字典,字符串的常规用法 二.列表 # 列 表 # 列表基础 list_1 = ['a ...
- vue发布之后会出现白屏现象主要几种原因和解决办法
第一种:由于把路由模式mode设置成history了,默认是hash. 解决方法:路由里边router/index.js路由配置里边默认模式是hash,如果你改成了history模式的话,打开也会是一 ...
- jquery validation remote进行唯一性验证时只使用自定义参数,不使用默认参数
在使用validation进行唯一性验证时,想各个模块写一个统一的方法,相统一参数名称,但是remote方法会默认把对应的参数传过去 如: 会把role.roleName默认作为变量提交过去 所以想自 ...
- c# string.format和tostring()
字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0:D3}", ...
- C++11中std::function的使用
class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...
- Django信号的使用
https://www.cnblogs.com/renpingsheng/p/7566647.html
- 『JavaScript』模仿接口
JavaScript中并没有内置的创建或实现接口的方法.这里将利用JavaScript的灵活性,来实现与接口意义相同的功能. 什么是接口? 接口的好处: 接口提供了一种用以说明一个对象应该具有哪些方法 ...