UESTC 250 windy数(数位DP)
2015-09-02 08:53
871 查看
Description
不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。问在A和B之间,包括A和B,总共有多少个windy数
Input
包含两个整数A和B满足 1 <=A <= B <=2000000000
Output
输出区间[A,B]中的windy数个数
Sample Input
1 10
Sample Output
9
Solution
数位DP,用dp[len][pre]表示当前位为len位,前一位为pre时windy数的个数
Code
不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。问在A和B之间,包括A和B,总共有多少个windy数
Input
包含两个整数A和B满足 1 <=A <= B <=2000000000
Output
输出区间[A,B]中的windy数个数
Sample Input
1 10
Sample Output
9
Solution
数位DP,用dp[len][pre]表示当前位为len位,前一位为pre时windy数的个数
Code
#include<cstdio> #include<cstring> #include<iostream> using namespace std; typedef long long ll; ll dp[12][12]; int num[12]; ll dfs(int len,bool fir,int pre,bool fp) { if(!len) return fir==0; if(!fp&&dp[len][pre]!=-1&&!fir) return dp[len][pre]; ll ret=0; int fpmax=fp?num[len]:9; for(int i=0;i<=fpmax;i++) if(fir||i-pre>=2||pre-i>=2) ret+=dfs(len-1,fir&&i==0,i,fp&&i==fpmax); if(!fir&&!fp) dp[len][pre]=ret; return ret; } ll f(ll n) { int len=0; while(n) { num[++len]=n%10; n/=10; } return dfs(len,1,11,1); } int main() { ll a,b; memset(dp,-1,sizeof(dp)); while(~scanf("%lld%lld",&a,&b)) printf("%lld\n",f(b)-f(a-1)); return 0; }
相关文章推荐
- UI中多线程的创建以及定时器与图片的异步加载
- UI中多线程的创建以及定时器与图片的异步加载
- 11.2.0.4 Patch Set - Availability and Known Issues (Doc ID 1562139.1)
- UI初级第十课  滑动视图基本概念和用法——iOS学习连载25
- UI初级第九课  手势与触摸——iOS学习连载24
- UITabBarController中自定义UITabBar
- [Leetcode]#60 Permutation Sequence
- LeetCode_DP_Unique Binary Search Trees II
- key对应VALUE
- usaco Sorting a Three-Valued Sequence
- hdu5412CRB and Queries
- UITESTLIST
- UIView,UIButton,UIImageView等视图设置圆角,设置阴影,设置边框的方法
- HDOJ 题目3397 Sequence operation(线段树区间覆盖异或合并)
- ios开发之UI基础--应用管理xib-九宫格布局
- C#StringBuilder的使用
- LeetCode_DP_Unique Binary Search Trees
- POJ2299 Ultra-QuickSort(树状数组)
- iOS 7 新特性 - UIKit Dynamics
- Prefer Literal Syntax over the Equivalent methods