UESTC 250 windy数
2016-03-16 09:16
337 查看
题目链接:http://acm.uestc.edu.cn/#/problem/show/250
[b]数位DP专题:/article/8966394.html
[/b]
题意:windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。给出一个区间,求区间内有多少windy数。
思路:简单数位DP,递推处理统计或记忆化dfs都可以,但是dfs的时候注意如果前面一直都为0的话,是不影响第一个非零位的取值范围的。
[b]数位DP专题:/article/8966394.html
[/b]
题意:windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。给出一个区间,求区间内有多少windy数。
思路:简单数位DP,递推处理统计或记忆化dfs都可以,但是dfs的时候注意如果前面一直都为0的话,是不影响第一个非零位的取值范围的。
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <iostream> #include <algorithm> #include <stack> #include <map> #include <set> #include <vector> #include <sstream> #include <queue> #include <utility> using namespace std; #define rep(i,j,k) for (int i=j;i<=k;i++) #define Rrep(i,j,k) for (int i=j;i>=k;i--) #define Clean(x,y) memset(x,y,sizeof(x)) #define LL long long #define ULL unsigned long long #define inf 0x7fffffff #define mod %100000007 int dp[20][10]; int A,B; void init() { Clean(dp,0); rep(i,0,9) dp[1][i] = 1; rep(i,2,10) rep(j,0,9) rep(k,0,9) if ( abs( j - k ) > 1 ) dp[i][j]+=dp[i-1][k]; } int cal(int x) { int ans = 1; int bit[12]; bit[0] = 0; int l = 0; while( x ) { bit[++l] = x % 10; x/=10; } //长度小于l的数 rep(i,1,l-1) { rep(j,1,9) ans+=dp[i][j]; } //长度等于l且最高位不到上限 rep(i,1,bit[l]-1) ans+=dp[l][i]; //长度等于l最高位到上限 Rrep(i,l-1,1) { rep(j,0,bit[i] - 1) if ( abs( bit[i+1] - j ) > 1 ) ans+=dp[i][j]; if ( abs( bit[i+1] - bit[i] ) <= 1 ) break; } return ans; } int main() { init(); scanf("%d %d",&A,&B); printf("%d\n" , cal(B+1) - cal(A) ); return 0; }
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <iostream> #include <algorithm> #include <stack> #include <map> #include <set> #include <vector> #include <sstream> #include <queue> #include <utility> using namespace std; #define rep(i,j,k) for (int i=j;i<=k;i++) #define Rrep(i,j,k) for (int i=j;i>=k;i--) #define Clean(x,y) memset(x,y,sizeof(x)) #define LL long long #define ULL unsigned long long #define inf 0x7fffffff #define mod %100000007 int dp[20][11]; //dp[i][j]处理到第i位时,上一位的值为j int digit[20]; int A,B; int dfs(int pos , int x , bool flag) { if ( pos == 0 ) return 1; if ( !flag && dp[pos][x]!=-1 ) return dp[pos][x]; int ed = flag?digit[pos]:9; int ans = 0; rep(i,0,ed) { if ( x < 10 && abs(i-x)<2 ) continue; //x为10表示前面所有位取的都是0,也就是前导零 ans+=dfs( pos-1 , (x==10 && i == 0)?10:i , flag && i == ed ); } if ( !flag ) dp[pos][x] = ans; return ans; } int cal(int x) { int len = 0; while(x) { digit[++len] = x % 10; x /= 10; } return dfs(len,10,1); } int main() { Clean(dp,-1); scanf("%d %d",&A,&B); printf("%d\n" , cal(B) - cal(A-1) ); return 0; }
相关文章推荐
- 基于SSH框架的EasyUI的前端DataGrid实现
- LeetCode-63-Unique Paths II(动规)-Medium
- php关联数组通过key获取value
- UESTC 758 P酱的冒险旅途
- 如何得到Ubuntu UI Toolkit的版本
- POJ 3061 Subsequence (尺取)
- IOS NSOperation&NSOperationQueue
- 如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]
- UGUI动态改变Grid大小
- UVA 11538Chess Queen
- 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!
- 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!
- 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!
- iOS开发笔记12:iOS7上UITextField限制字数输入导致崩溃问题
- Leetcode Longest Consecutive Sequence
- UIWindow的研究
- UIGraphicsBeginImageContext
- UITableView性能优化
- caffe代码阅读3:data_reader、internalthread以及blocking_queue的实现细节-2016.3.15
- iOS-UIDynamic物理仿真-推力-UIPushBehavior