【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1026

【题意】

【题解】



数位Dp

设f[i][j]表示长度为i,第一位(也就是最高位,注意不是个位数)为j的windy数的个数;

f[i][j] += ∑f[i-1][k] 这里abs(j-k)>1

这里f[1][0..9]都为0,这里的f[1][0]是为了后面的f[2][1..9]准备的.不然会漏掉20,30,….这些东西.

然后求A..B之间的Windy数;

可以转换为求

1..B之间的Windy数和1..A-1之间的Windy数;

相减就是答案了;

这里对于求1..x之间的windy个数;

设x的十进制长度为len;

则先加上

f[1..len-1][1..9]

然后对于len位数字的数.

可以一个一个枚举地加

注意细节问题就好.



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110; int A, B;
int a[N];
int dp[N][N]; void input_data()
{
rei(A), rei(B);
} void do_dp()
{
rep1(i, 0, 9)
dp[1][i] = 1;
rep1(i, 2, 10)
rep1(j, 0, 9)
rep1(k, 0, 9)
dp[i][j] += dp[i - 1][k] * (abs(j - k) > 1);
} int get_ans(int x)
{
int len = 0,cnt = 0;
while (x)
{
a[++len] = x % 10;
x /= 10;
}
rep2(i, len, 1)
{
if (i<len-1 && abs(a[i + 2] - a[i + 1]) <= 1) break;
rep1(j, 0 + (i == len), a[i] + (i == 1) - 1)
if (i==len||abs(a[i+1]-j)>1)
{
cnt += dp[i][j];
}
} rep1(i, 1, len - 1)
rep1(j, 1, 9)
cnt += dp[i][j];
return cnt;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_data();
do_dp();
printf("%d\n", get_ans(B) - get_ans(A - 1));
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

最新文章

  1. ConcurrentHashMap内存泄漏问题
  2. memset函数详解
  3. iOS 获取User-Agent
  4. 0014 Java学习笔记-集合-HashMap集合
  5. Vcl.FileCtrl.SelectDirectory
  6. CSS3动画变形transition
  7. Linux——搭建PHP开发环境第二步:PHP
  8. 总结showModalDialog在开发中的一些问题
  9. 腾讯測试project师笔试面试记录
  10. position的absolute与fixed共同点与不同点
  11. 算法题:A除以B
  12. Vijos P1786 质因数分解【暴力】
  13. 【mongodb系统学习之四】查看mongodb进程
  14. SUSE12SP3-Mycat(3)Server.xml配置详解
  15. Matplotlib.pyplot 把画图保存为图片
  16. c#属性 ——面向对象
  17. Windows系统Ionic安装教程/Ionic环境配置
  18. oracle字符集查看、修改、版本查看
  19. [Backbone]4. Model &amp; View, toggle between Model and View. -- 1
  20. Android 视频缩放/放大

热门文章

  1. js进阶 14-3 如何接收load函数从后台接收到的返回数据
  2. js中#代表什么
  3. 使用 Python 第三方库 daft 绘制 PGM 中的贝叶斯网络
  4. java中那些已经有的好用轮子
  5. LeetCode Algorithm 07_Reverse Integer
  6. HDU 1018 Big Number 数学题解
  7. 首次使用vim
  8. netty检测系统工具PlatformDependent
  9. pcb过孔盖油
  10. 基于mpvue的小程序项目搭建的步骤一